-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
337 lines (268 loc) · 9.02 KB
/
db.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""Hi Audience"""
import random
import string
import smtplib
from datetime import datetime
import pytz
from pymongo.mongo_client import MongoClient
from user import User
country_time_zone = pytz.timezone('Asia/Kolkata')
CLIENT_URL = "mongodb+srv://test:test@cluster0.nrvnm.mongodb.net/"
client = MongoClient(CLIENT_URL+"myFirstDatabase?retryWrites=true&w=majority")
ecom = client.get_database("GamesTrade")
users_collection = ecom.get_collection("users")
products_collection = ecom.get_collection("products")
orders_collection = ecom.get_collection("orders")
info_collection = ecom.get_collection("info")
track_collection = ecom.get_collection("track")
updates_collection = ecom.get_collection("updates")
def get_user(email):
"""Hi Audience"""
user_data = users_collection.find_one(
{'_id': email.split("@")[0], 'email': email})
return User(user_data['email']) if user_data else None
def get_cart(email):
"""Hi Audience"""
_id = email.split("@")[0]
for temp_var in users_collection.find({'_id': _id}):
cart = temp_var.get('cart')
return cart
def user_cart_prod(email, temp):
"""Hi Audience"""
cart = get_cart(email)
for i in cart:
if i['_id'] == temp:
return i['cqty']
return 0
def get_product(tag):
"""Hi Audience"""
for temp_var in products_collection.find({}):
name = temp_var.get("_id")
if tag == name:
temp_var['image'] = [temp_var['image'], temp_var['image'], temp_var['image']]
return temp_var
def save_user(email):
"""Hi Audience"""
users_collection.insert_one(
{'_id': email.split("@")[0], 'email': email, 'cart': []})
def get_product_id(tag):
"""Hi Audience"""
for temp_var in products_collection.find({}):
name = temp_var.get("_id")
if tag == name:
return temp_var
def add_into_cart(email, item):
"Add item to cart"
_id = email.split("@")[0]
cart = get_cart(email)
cart.append({'_id': item, 'cqty': 1})
users_collection.update_one({'_id': _id}, {"$set": {'cart': cart}})
def remove_from_cart(email, item):
"Add item to cart"
_id = email.split("@")[0]
cart = get_cart(email)
for i in cart:
if str(i['_id']) == item:
cart.remove(i)
break
users_collection.update_one({'_id': _id}, {"$set": {'cart': cart}})
def set_qty(email, item):
"""Hi Audience"""
_id = email.split("@")[0]
cart = get_cart(email)
qty = item[:-16]
for i in cart:
if i['_id'] == item[-16:]:
if int(qty) == 0:
cart.remove(i)
else:
i['cqty'] = int(item[:-16])
else:
pass
users_collection.update_one({'_id': _id}, {"$set": {'cart': cart}})
def prod_names():
"""Hi Audience"""
temp_list = []
for temp_var in products_collection.find({}):
temp_list.append(temp_var.get('category') + " " + temp_var.get('name'))
return temp_list
def prod_id():
"""Hi Audience"""
temp_list = []
for temp_var in products_collection.find({}):
temp_list.append(temp_var.get('_id'))
return temp_list
def all_prod():
"""Hi Audience"""
temp_list = []
for i in products_collection.find({}):
temp_list.append(i)
return temp_list
def all_products(category):
"""Hi Audience"""
temp_list = []
for i in products_collection.find({'category': category}):
if i.get('quantity') > 0:
temp_list.append(i)
for i in range(0, len(temp_list), 20):
yield temp_list[i:i + 20]
def test_all():
"""Hi Audience"""
all_p = [[],[],[],[]]
for i in range(1,4):
all_p[i] = list(all_products('PS4'))[0]
all_p[0] = list(all_products('PS5'))[0]
all_p[2] = list(all_products('PS5'))[0]
return all_p
def get_total(email):
"""Hi Audience"""
cart = get_cart(email)
total = 0
for i in cart:
product = get_product_id(i['_id'])
total += product['srp']*i['cqty']
return total
def gen_id(temp_varl):
"""Hi Audience"""
temp_var = ''.join((random.choice(string.ascii_uppercase) for x in range(temp_varl)))
return temp_var
def bill(email):
"""Hi Audience"""
for i in info_collection.find({'_id': email}):
info_collection.delete_one({'_id': email})
idt = gen_id(16)
temp_order = {'_id': idt, 'name': i.get('name'), 'phone': i.get('phone'), 'address': i.get(
'address'), 'cart': get_cart(email), 'email': email, 'amount': get_total(email)}
orders_collection.insert_one(temp_order)
return idt, i['email']
def prod_qty(idt):
"""Hi Audience"""
cart = orders_collection.find_one({'_id': idt})
cart = cart.get('cart')
for i in cart:
prod = products_collection.find_one({'_id': i.get('_id')})
new_qty = prod.get('quantity') - i.get('cqty')
products_collection.update_one({'_id': i.get('_id')}, {
"$set": {'quantity': new_qty}})
def empty_cart(email):
"""Hi Audience"""
_id = email.split("@")[0]
cart = get_cart(email)
for i in cart:
cart.remove(i)
users_collection.update_one({'_id': _id}, {"$set": {'cart': cart}})
def add_info(curr_id, email, name, phone, address):
"""Hi Audience"""
temp_y = 0
temp_x = 0
for i in info_collection.find({}):
temp_y += 1
if email == i.get('_id'):
temp_x += 1
if temp_x != temp_y:
info_collection.delete_one({'_id': curr_id})
info_collection.insert_one({'_id': curr_id, 'email': email, 'name': name, 'phone': phone, 'address': address})
else:
info_collection.insert_one({'_id': curr_id, 'email': email, 'name': name, 'phone': phone, 'address': address})
def mail(email, message):
"""Hi Audience"""
sender_email = "granthbagadia2004@gmail.com"
rec_email = email
password = "fokejjwavrejtchi"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, rec_email, message)
# mail("granthbagadia2004@gmail.com", "cbdhjcdbnh")
def save_product(category, name, quantity, mrp, srp, image, info):
"""Hi Audience"""
_id = gen_id(16)
if f"{category} {name}" not in prod_names():
item = {'_id': _id, 'category': category, 'name': name,
'quantity': quantity, 'mrp': mrp, 'srp': srp, 'image': image, 'info': info}
products_collection.insert_one(item)
return "Item Added"
return "Item Already Present"
def update_product(_id, mrp, srp, quantity):
"""Hi Audience"""
if mrp >= 0 and srp >= 0 and quantity >= 0:
products_collection.update_one(
{'_id': _id}, {"$set": {'mrp': mrp, 'srp': srp, 'quantity': quantity}})
def delete_users():
"""Hi Audience"""
users_collection.delete_many({})
def delete_products():
"""Hi Audience"""
products_collection.delete_many({})
def ord_track(idt, temp_status):
"""Hi Audience"""
order = orders_collection.find_one({'_id': idt})
if temp_status is None:
order['Status'] = "None"
else:
order['Status'] = temp_status
order['Time'] = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
message = f"""From: From granthbagadia2004@gmail.com
Subject: Tracking Details for {idt}
These are the tracking details for your order on Games-Trade India.
For Your Order: {idt}
Current Status: {order['Status']}
"""
email = orders_collection.find_one({'_id': idt}).get('email')
mail(email, message)
orders_collection.delete_one({'_id': idt})
if order['Status'] == 'Delivered':
track_collection.insert_one(order)
else:
orders_collection.insert_one(order)
def all_orders():
"""Hi Audience"""
temp_var = []
for i in orders_collection.find({}):
temp_var.append(i)
return temp_var
def track_all():
"""Hi Audience"""
temp_var = []
all_col = track_collection.find({})
for i in all_col:
temp_var.append(i)
return temp_var
def total_items(email):
"""Hi Audience"""
temp_var = 0
cart = get_cart(email)
for i in cart:
temp_var += i.get('cqty')
return temp_var
def latest_prod(email=None):
"""Hi Audience"""
temp_var = []
for i in products_collection.find({}):
i['cqty'] = 0
if email != None:
cart = get_cart(email)
for j in cart:
if j['_id'] == i['_id']:
i['cqty'] = j['cqty']
temp_var.append(i)
temp_var = temp_var[-4:]
return temp_var
def all_updates():
"""Hi Audience"""
temp_var = []
for i in updates_collection.find({}):
temp_var.append(i)
temp_var = temp_var[-4:]
return temp_var
def search_prod(tag):
"""Hi Audience"""
temp = products_collection.find({})
filtered = []
for i in temp:
if tag.upper() in i.get('name').upper():
filtered.append(i)
if tag.upper() in i.get('category').upper():
filtered.append(i)
for i in range(0, len(filtered), 20):
yield filtered[i:i + 20]