-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
396 lines (336 loc) · 12.4 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from database import Database
DATE_NOW = "2020-02-07"
DEFAULT_CREDITS = 1500
class Session:
def __init__(self) -> None:
self.user = None
session = Session()
database = Database()
# helper functions ------------------------------
def authenicate(username, password, is_admin=False):
user = database.get_user(username)
if user:
if user.password == password:
if (user.access and is_admin) or (not user.access and not is_admin):
return user
return None
def is_product_id_exists(product_id):
return database.get_product(product_id)
def add_product(id, name, price, stock):
return database.add_product(id, name, price, stock)
def change_stock(product_id, new_stock):
product = database.get_product(product_id)
database.update_product(product_id, product.price, new_stock)
return True
def change_price(product_id, new_price):
product = database.get_product(product_id)
database.update_product(product_id, new_price, product.stock)
return True
def is_password_strong(password):
if len(password)<8:
return (False, "short password...")
return True
def change_password(username, new_password):
database.change_password(username, new_password)
return True
def is_username_available(username):
return not database.get_user(username)
def signup_user(username, password):
return database.add_user(username, password, DEFAULT_CREDITS)
def products_list():
return database.get_all_products()
def is_product_in_stock(product_id):
return database.get_product(product_id)
def discount_code_status(code):
code = database.get_discount_code(code)
if code:
if code.used or code.is_expired():
print("not a valid code.")
return 0
else:
return code.discount_percentage
print("not a valid code.")
return 0
def new_order(product_id, ordered_count, total_price, username, date):
database.add_order(product_id, total_price, date, username)
product = database.get_product(product_id)
database.update_product(product_id, product.price, product.stock-ordered_count)
return True
# pages ----------------------
def start():
print("in each step you can enter 'back' command to back to the previous page.")
command = input("are you an admin or a customer? [1 for admin 2 for customer]\n")
if command == "1":
login_page(is_admin=True)
elif command == "2":
customer_first_page()
else:
print("wrong command please just enter 1 or 2...")
start()
def login_page(is_admin):
command = input("username: ")
if command == "back":
start()
return None
username = command
command = input("password: ")
if command == "back":
start()
return None
password = command
user = authenicate(username, password, is_admin)
if user:
print("you logged in successfully.")
session.user = user
if is_admin:
admin_menu_page()
else:
customer_menu_page()
else:
print("wrong username or password... try again")
login_page(is_admin)
return None
def signup_page():
command = input("username: ")
if command == "back":
start()
return None
username = command
if is_username_available(username):
while True:
command = input("password: ")
if command == "back":
start()
return None
password = command
if is_password_strong(password):
status = signup_user(username, password)
if status == True:
print("done successfully.")
customer_first_page()
else:
print("an error occured... please try again.")
signup_page()
return None
else:
print("password is weak... it has to contain 8 charactor and...")
else:
print("username is not available... try again.")
signup_page()
return None
def admin_menu_page():
command = input("1: add product \n2: change stock \n3: change price \n4: change password \n")
if command == "back":
login_page(is_admin=True)
return None
if command == "1":
add_product_page()
elif command == "2":
change_stock_page()
elif command == "3":
change_price_page()
elif command == "4":
change_password_page(is_admin=True)
else:
print("wrong command... try again.")
admin_menu_page()
def add_product_page():
command = input("enter the product id: ")
if command == "back":
admin_menu_page()
return None
product_id = command
if not is_product_id_exists(product_id):
command = input("product name: ")
if command == "back":
admin_menu_page()
return None
name = command
command = input("product price: ")
if command == "back":
admin_menu_page()
return None
price = command
command = input("product stock: ")
if command == "back":
admin_menu_page()
return None
stock = command
status = add_product(product_id, name, price, stock)
if status == True:
print("product added successfully.")
print("add new product or 'back' to back to the admin dashboard.")
add_product_page()
return None
else:
print("an error occurred... please try again.")
add_product_page()
return None
else:
print("we have this product already... please try again.")
add_product_page()
def change_stock_page():
command = input("enter the product id: ")
if command == "back":
admin_menu_page()
return None
product_id = int(command)
if is_product_id_exists(product_id):
command = input("new stock: ")
if command == "back":
admin_menu_page()
return None
new_stock = int(command)
status = change_stock(product_id, new_stock)
if status == True:
print("done successfully.")
print("change another product stock or 'back' to back to the admin dashboard.")
change_stock_page()
else:
print("an error occured... please try again.")
change_stock_page()
else:
print("wrong product id... please try again.")
change_stock_page()
def change_price_page():
command = input("enter the product id: ")
if command == "back":
admin_menu_page()
return None
product_id = command
if is_product_id_exists(product_id):
command = input("new price: ")
if command == "back":
admin_menu_page()
return None
new_price = int(command)
status = change_price(product_id, new_price)
if status == True:
print("done successfully.")
print("change another product price or 'back' to back to the admin dashboard.")
change_price_page()
else:
print("an error occured... please try again.")
change_price_page()
else:
print("wrong product id... please try again.")
change_price_page()
def change_password_page(is_admin):
command = input("enter new password: ")
if command == "back":
if is_admin:
admin_menu_page()
else:
customer_menu_page()
return None
new_password = command
if is_password_strong(new_password):
status = change_password(session.user.username, new_password)
if status == True:
print("done successfully.")
if is_admin:
admin_menu_page()
else:
customer_menu_page()
return None
else:
print("an error occured... please try again.")
change_password_page(is_admin)
else:
print("password is weak... it has to contain 8 charactor and...")
change_password_page(is_admin)
def customer_first_page():
command = input("1: login \n2: signup \n")
if command == "back":
start()
return None
if command == "1":
login_page(is_admin=False)
elif command == "2":
signup_page()
else:
print("wrong command... try again.")
customer_first_page()
def customer_menu_page():
print(f"you have {database.get_user(session.user.username).credits} credits left.")
command = input("1: new order \n2: history of orders \n3: change password \n")
if command == "back":
login_page(is_admin=False)
return None
if command == "1":
new_order_page()
elif command == "2":
history_of_orders_page()
elif command == "3":
change_password_page(is_admin=False)
else:
print("wrong command... try again.")
admin_menu_page()
def new_order_page():
print("all products id: ")
for product in products_list():
print(f"product id: {product.id} | price : {product.price} | stock: {product.stock}")
command = input("product id: ")
if command == "back":
customer_menu_page()
return None
product_id = command
if is_product_id_exists(product_id):
while True:
product = is_product_in_stock(product_id)
if product.stock > 0:
command = input("count: ")
if command == "back":
customer_menu_page()
return None
ordered_count = int(command)
if ordered_count <= product.stock:
command = input("do you have discount code? [y or n]\n")
discount = 0
if command == "back":
customer_menu_page()
return None
if command == "y":
command = input("discount code: ")
if command == "back":
customer_menu_page()
return None
discount = discount_code_status(command)
total_price = product.price*ordered_count*(100-discount)/100
print(f"total cost is: {total_price}")
command = input("do you want to pay with your credits? [y or n]\n")
if command == "back":
customer_menu_page()
return None
if command == "y":
status = new_order(product_id, ordered_count, total_price, session.user.username, DATE_NOW)
if status == True:
print("your order registered successfully.")
print("do you have new order?")
new_order_page()
return None
else:
print("some thing went wrong... order didn't register.")
else:
new_order_page()
return None
else:
print("there are limited counts of this product...")
print(f"you can choose up to {product.stock} instances of this product.")
else:
print("sorry all instances of this product are sold out... please choose another one.")
new_order_page()
return None
else:
print("wrong product id... try again.")
new_order_page()
def history_of_orders_page():
for order in database.get_history_of_orders(session.user.username):
print(f"product: {order.product.id}")
print(f"total price: {order.total_price}")
print(f"date: {order.date}")
print("-----------")
if not database.get_history_of_orders(session.user.username):
print("there were no orders...")
customer_menu_page()
start()