forked from Neria-A/BotFersal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
67 lines (49 loc) · 1.8 KB
/
mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
from Shovar import Shovar
import appSettings as appsec
from pymongo import MongoClient
from ShovarFromMongo import ShovarFromMongo
amounts = ['15', '30', '40', '50', '100', '200']
client = MongoClient(appsec.mongo_connection_string)
mydb = client["bot_fersal"]
mycol = mydb["shovarim"]
def insert_to_mongo(code):
mycol.insert_one(code)
def check_if_exist(message):
return mycol.find_one({"_id": message})
def find_barcode(amount):
result = mycol.find_one({"amount": amount, "is_used": False})
if result == 0:
return None
else:
return result
def update_db(shovar):
myquery = mycol.find_one({"_id": shovar.code})
new_value = {"$set": {"is_used": True, "date_used": datetime.datetime.now()}}
mycol.update_one(myquery, new_value)
def check_how_much_money():
amounts_dict = {}
for amount in amounts:
amounts_dict[amount] = 0
for amount in amounts:
coupons = mycol.find({"amount": amount, "is_used": False})
for coupon in coupons:
new_shovar = convert_mongo_to_shovar(coupon)
amounts_dict[new_shovar.amount] = amounts_dict[new_shovar.amount] + 1
return amounts_dict
def coupons_sum(coupons):
sum_coupons = 0
for key, value in coupons.items():
for _ in range(value):
sum_coupons += int(key)
return sum_coupons
def convert_mongo_to_shovar(barcode):
shovar = ShovarFromMongo.dict_to_shovar(barcode)
new_shovar = Shovar(shovar._id,
shovar.code,
shovar.amount,
shovar.expiry_date,
shovar.is_used,
shovar.date_added,
shovar.date_used)
return new_shovar