forked from subinkim/CS121FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-admin.py
More file actions
1164 lines (1091 loc) · 46.2 KB
/
app-admin.py
File metadata and controls
1164 lines (1091 loc) · 46.2 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Student name(s): Eshani Patel, Rachael Kim
Student email(s): ejpatel@caltech.edu, subinkim@caltech.edu
High-level program overview:
- `app-admin.py` allows the admin user of our service to manage the store.
- admin user will be allowed to perform following operations:
1) add or delete product/user/brand
2) update inventory
3) view relevant statistics
"""
import sys # to print error messages to sys.stderr
import os
# check if mysql.connector is installed
# if not, run pip3 install to install
try:
import mysql.connector
except ImportError as e:
os.system('pip3 install mysql.connector')
# To get error codes from the connector, useful for user-friendly
# error-handling
import mysql.connector.errorcode as errorcode
# Debugging flag to print errors when debugging that shouldn't be visible
# to an actual client. ***Set to False when done testing.***
DEBUG = True
# ----------------------------------------------------------------------
# SQL Utility Functions
# ----------------------------------------------------------------------
def get_conn():
""""
Returns a connected MySQL connector instance, if connection is successful.
If unsuccessful, exits.
"""
try:
conn = mysql.connector.connect(
host='localhost',
user='admin',
# Find port in MAMP or MySQL Workbench GUI or with
# SHOW VARIABLES WHERE variable_name LIKE 'port';
port='3306', # this may change!
password='adminpwd',
database='cosmeticsdb' # replace this with your database name
)
print('Successfully connected.')
return conn
except mysql.connector.Error as err:
# Remember that this is specific to _database_ users, not
# application users. So is probably irrelevant to a client in your
# simulated program. Their user information would be in a users table
# specific to your database; hence the DEBUG use.
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR and DEBUG:
sys.stderr('Incorrect username or password when connecting to DB.')
elif err.errno == errorcode.ER_BAD_DB_ERROR and DEBUG:
sys.stderr('Database does not exist.')
elif DEBUG:
sys.stderr(err)
else:
# A fine catchall client-facing message.
sys.stderr('An error occurred, please contact the administrator.')
sys.exit(1)
# ----------------------------------------------------------------------
# Functions for Command-Line Options/Query Execution
# ----------------------------------------------------------------------
def execute_query(sql):
cursor = conn.cursor()
# Remember to pass arguments as a tuple like so to prevent SQL
# injection.
try:
cursor.execute(sql)
rows = cursor.fetchall()
return rows
except mysql.connector.Error as err:
# If you're testing, it's helpful to see more details printed.
if DEBUG:
sys.stderr(err)
sys.exit(1)
else:
# returns -1 here if the execution of the query fails
# so we can handle error separately at the caller
sys.stderr('Error! Unable to execute SQL query. Check your inputs again.')
return -1
# used when we have to do .fetchone() instead of .fetchall()
def execute_query_single(sql):
cursor = conn.cursor()
# Remember to pass arguments as a tuple like so to prevent SQL
# injection.
try:
cursor.execute(sql)
row = cursor.fetchone()
return row
except mysql.connector.Error as err:
# If you're testing, it's helpful to see more details printed.
if DEBUG:
sys.stderr(err)
sys.exit(1)
else:
# returns -1 here if the execution of the query fails
# so we can handle error separately at the caller
sys.stderr('Error! Unable to execute SQL query. Check your inputs again.')
return -1
# used when we have to commit changes
# insert/delete/update queries
def execute_insert_delete_update(sql):
cursor = conn.cursor()
# Remember to pass arguments as a tuple like so to prevent SQL
# injection.
try:
# commit the result, and if successful, return 1
cursor.execute(sql)
conn.commit()
return 1
except mysql.connector.Error as err:
# If you're testing, it's helpful to see more details printed.
if DEBUG:
sys.stderr(err)
sys.exit(1)
else:
# returns -1 here if the execution of the query fails
# so we can handle error separately at the caller
sys.stderr('Error! Unable to execute SQL query. Check your inputs again.')
return -1
# Runs the SELECT query to get the list of product_id and
# product_name from product table with an optional condition
def get_products(cond = ''):
sql = 'SELECT product_id, product_name FROM product %s;' %cond
rows = execute_query(sql)
return rows
# Runs the SELECT query to get the list of brand info
# from product table with an optional condition
def get_brands(cond = ''):
sql = 'SELECT brand_id, brand_name FROM brand %s;' % cond
rows = execute_query(sql)
return rows
# Runs a MySQL query to get the brand_name, product_name and
# the current inventory count of the corresponding product
def get_curr_inventory(conds = ''):
sql = 'SELECT brand_name, product_name, inventory FROM brand \
JOIN store ON brand.brand_id = store.brand_id \
JOIN product ON product.product_id = store.product_id %s\
ORDER BY brand_name ASC;' % conds
rows = execute_query(sql)
return rows
# Runs a MySQL query to get the brand_name, the sum of
# the total inventory value of each product under that brand
# (computes the total inventory value for each brand)
def get_inventory_per_brand(limits = ''):
sql = 'SELECT brand_name, SUM(calculate_inventory_value(product_id))\
AS total_inventory_value FROM brand NATURAL JOIN store \
NATURAL JOIN product GROUP BY brand_name %s;' % limits
rows = execute_query(sql)
return rows
# Runs a MySQL query to get brand, product_id, product_name from store
# given the optional conditions
def get_curr_store(conds = ''):
sql = 'SELECT brand_name, product.product_id, \
product_name FROM product NATURAL JOIN store \
NATURAL JOIN (SELECT * FROM brand %s) AS p;' % conds
rows = execute_query(sql)
return rows
# Runs a MySQL query to compute the total sales of the entire store
def get_all_sales():
sql = 'SELECT SUM(num_items * price) \
AS total_sales FROM purchase_history AS h \
NATURAL JOIN product;'
rows = execute_query(sql)
return rows
# Runs a MySQL query to get the total sales for a brand, given
# the brand_id
def get_brand_sales(brand_id):
sql = 'SELECT brand.brand_name, SUM(num_items * price) \
AS total_sales FROM purchase_history AS h \
JOIN product AS p ON h.product_id = p.product_id \
JOIN store AS s ON p.product_id = s.product_id \
JOIN brand ON brand.brand_id = s.brand_id\
WHERE brand.brand_id = %s\
GROUP BY brand.brand_id ORDER BY brand.brand_name ASC;'\
% brand_id
rows = execute_query(sql)
return rows
# Runs a MySQl query to get the total sales for a product, given
# the product_id
def get_product_sales(prod_id):
sql = 'SELECT product.product_name, SUM(num_items * price) \
AS total_sales FROM purchase_history JOIN product \
ON purchase_history.product_id = product.product_id \
WHERE product.product_id = %s\
GROUP BY product.product_id \
ORDER BY product.product_name ASC;' % prod_id
rows = execute_query(sql)
return rows
# Runs a MySQL query to compute the total sales for all of
# the brands on our service
def get_all_brand_sales():
sql = 'SELECT brand.brand_name, SUM(num_items * price) \
AS total_sales FROM purchase_history AS h \
JOIN product AS p ON h.product_id = p.product_id \
JOIN store AS s ON p.product_id = s.product_id \
JOIN brand ON brand.brand_id = s.brand_id\
GROUP BY brand.brand_id ORDER BY brand.brand_name ASC;'
rows = execute_query(sql)
return rows
# Runs a MySQL query to compute the total sales for all of the
# products on our service
def get_all_product_sales():
sql = 'SELECT product.product_name, SUM(num_items * price) \
AS total_sales FROM purchase_history JOIN product \
ON purchase_history.product_id = product.product_id \
GROUP BY product.product_id ORDER BY product.product_name ASC;'
rows = execute_query(sql)
return rows
# Get the top 5 highest-sales brands
# "top selling" here is determined by the dollar amount of total sales
def get_top_selling_brand():
sql = 'SELECT brand.brand_name, SUM(num_items * price) \
AS total_sales FROM purchase_history AS h \
JOIN product AS p ON h.product_id = p.product_id \
JOIN store AS s ON p.product_id = s.product_id \
JOIN brand ON brand.brand_id = s.brand_id\
GROUP BY brand.brand_id ORDER BY total_sales \
DESC LIMIT 5;'
rows = execute_query(sql)
return rows
# Get the top 5 highest-sales products for the given brand
# "top selling" here is determined by the dollar amount of total sales
def get_top_selling_prod_per_brand(brand_id):
sql = 'SELECT p.product_name, SUM(num_items * price) \
AS total_sales FROM purchase_history AS h \
JOIN product AS p ON h.product_id = p.product_id \
JOIN store AS s ON p.product_id = s.product_id \
JOIN brand ON brand.brand_id = s.brand_id\
WHERE brand.brand_id = %s\
GROUP BY p.product_name ORDER BY total_sales \
DESC LIMIT 5;' % brand_id
rows = execute_query(sql)
return rows
# Get the top 5 highest-sales products
# "top selling" here is determined by the dollar amount of total sales
def get_top_selling_product():
sql = 'SELECT product.product_name, SUM(num_items * price) \
AS total_sales FROM purchase_history JOIN product \
ON purchase_history.product_id = product.product_id \
GROUP BY product.product_id \
ORDER BY total_sales DESC LIMIT 5;'
rows = execute_query(sql)
return rows
# Get the top 5 best-selling products for the given product type
# "best selling" here is determined by the units sold
def get_product_type_best_sale(prod_type):
sql = 'SELECT product_name, SUM(num_items) AS total_units_sold\
FROM purchase_history NATURAL JOIN product \
WHERE product_type=\'%s\' GROUP BY product_name\
ORDER BY total_units_sold DESC, product_name ASC LIMIT 5;'\
% prod_type
rows = execute_query(sql)
return rows
# Get the top 5 best-selling products for the given brand
# "best selling" here is determined by the units sold
def get_brand_best_sale(brand_id):
sql = 'SELECT product_name, SUM(num_items) AS total_units_sold\
FROM purchase_history NATURAL JOIN product NATURAL JOIN store\
NATURAL JOIN brand WHERE brand_id=%s GROUP BY product_name\
ORDER BY total_units_sold DESC, product_name ASC LIMIT 5;' % brand_id
rows = execute_query(sql)
return rows
# Get the top 5 best-selling products amongst all brands
def get_best_selling_product():
sql = 'SELECT p.product_name, SUM(num_items) AS total_units_sold\
FROM purchase_history AS h JOIN product AS p ON \
h.product_id = p.product_id GROUP BY p.product_name\
ORDER BY total_units_sold DESC, p.product_name ASC LIMIT 5;'
rows = execute_query(sql)
return rows
# Get the top 5 best-selling brands, by computing the total units of
# all products sold under each brand
def get_best_selling_brand():
sql = 'SELECT brand_name, SUM(num_items) AS total_units_sold \
FROM purchase_history AS h NATURAL JOIN product \
NATURAL JOIN store NATURAL JOIN brand \
GROUP BY brand_id ORDER BY total_units_sold \
DESC LIMIT 5;'
rows = execute_query(sql)
return rows
# ----------------------------------------------------------------------
# Command-Line Functionality
# ----------------------------------------------------------------------
#############################
# Result formatters #
#############################
# Formats a list of brands
def format_brand_list(brands):
print()
print("Brand ID | {bname:100s}".format(bname="Brand Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (brand_id, brand_name) in brands:
print("{brand_id:3d} | {brand_name:100s}".format(brand_id=brand_id, brand_name=brand_name))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the result of SELECT query on product
# Prints the list of product id and product name
def format_product_list(products):
print()
print("Product ID | Product Name")
print("-----------------------------------------------------------------------------------------------------------------------------")
for (product_id, product_name) in products:
print("{product_id:4d} | {product_name:100s}"
.format(product_id=product_id, product_name=product_name))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats a list of (brand_id, product_id, product_name)
# to show product_id and product_name in a nice format
def format_store_products(stores):
print()
print("Product ID | Product Name")
print("-----------------------------------------------------------------------------------------------------------------------------")
for (_, product_id, product_name) in stores:
print("{product_id:4d} | {product_name:100s}"
.format(product_id=product_id,product_name=product_name))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats a list of (brand_name, product_name, inventory)
# to only show the brand_name and product_name in a nice format
def format_store(inventory):
print()
print("{b:30s} | {name:100s}".format(b="Brand Name", name="Product Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (brand_name, product_name, _) in inventory:
print("{brand_name:30s} | {product_name:100s}"
.format(brand_name=brand_name, product_name=product_name))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the result of inventory calculation
# Prints the list of product names and the
# names for the corresponding brands
def format_inventory_res(curr_inventory):
print()
print("{b_name:20s} | {p_name:80s} | Inventory"
.format(b_name="Brand Name", p_name="Product Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (brand_name, product_name, inventory) in curr_inventory:
print("{brand_name:20s} | {product_name:80s} | {inventory:.0f}"
.format(brand_name=brand_name, product_name=product_name, inventory=inventory))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the list of tuples (brand_name, inventory_value)
# where inventory_value is the total value of inventory
# owned by each brand, computed by doing
# inventory (amount of the given product available on store) *
# the price of each item
def format_inventory_val(inventory_val):
print()
print("{name:30s} | Inventory Value".format(name="Brand Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (brand_name, inventory_value) in inventory_val:
print("{brand_name:30s} | ${inventory_value:.2f}"
.format(brand_name=brand_name, inventory_value=inventory_value))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the list of tuples (name, sales_value)
# where name might be either product_name or brand_name
# and sales_value is the sum of all products that are sold
def format_sales(sales):
print()
print("{n:100s} | Total Sales Value".format(n="Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (name, sales_value) in sales:
print("{name:100s} | ${sales_value:.2f}"
.format(name=name, sales_value=sales_value))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the list of tuples (name, num_units)
# where name might be either product_name or brand_name
# and num_units is the number of units sold
def format_sales_units(sales):
print()
print("{name:100s} | Number of Units Sold".format(name="Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (name, num_units) in sales:
print("{name:100s} | {num_units:.0f}"
.format(name=name, num_units=num_units))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the list of tuples (name, num_units, purchase_time)
# to show recent purchase_histories
def format_recent_sales(sales):
print()
print("{name:100s} | Purchase Time | Number of Units".format(name="Name"))
print("-----------------------------------------------------------------------------------------------------------------------------")
for (name, num_units, purchase_time) in sales:
print("{name:100s} | {purchase_time} | {num_units:.0f} "
.format(name=name, num_units=num_units, purchase_time=purchase_time))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
# Formats the list of users stored in our database
def format_user(users):
print()
print("Username")
print("-----------------------------------------------------------------------------------------------------------------------------")
for user in users:
print("{username:20s} ".format(username=user[0]))
print("-----------------------------------------------------------------------------------------------------------------------------")
print()
#############################
# Helper functions #
#############################
# checks if the given product_id is valid my verifying if a query
# against product table returns a result
def product_id_is_valid(product_id):
if not product_id.isnumeric():
return format_sales_units
sql = 'SELECT * FROM product WHERE product_id=%s;' % product_id
rows = execute_query(sql)
return len(rows) > 0
# checks if the given brand_id is valid my verifying if a query
# against brand table returns a result
def brand_id_is_valid(brand_id):
if not brand_id.isnumeric():
return format_sales_units
sql = 'SELECT * FROM brand WHERE brand_id=%s;' % brand_id
rows = execute_query(sql)
return len(rows) > 0
# a Python function that walks the admin user through all the
# possible options to get the id of the product that they
# want to reference
def get_product_id():
# helper function to get product_id given the list of
# products under a chosen brand_id
def get_prod_id_from_brand_list():
ans = input("Enter brand_id: ")
while not brand_id_is_valid(ans):
print("Your brand_id is not valid.")
ans = input("Enter your brand_id again: ")
rows = get_curr_store("WHERE brand_id=%s" % ans)
format_store_products(rows)
ans = input("Now, enter your product_id: ")
while not product_id_is_valid(ans):
print("Your product_id is not valid.")
ans = input("Enter your product_id again: ")
return ans
# if users know the precise product_id that they want to
# choose, then we simply verify that it's a valid id
# and return the value
print("Choose an option")
print(" (a) I know a precise product_id I want")
print(" (b) I want to see the list of products")
ans = input("Enter: ").lower()
if ans == "a":
ans = input("Enter product_id: ")
while not product_id_is_valid(ans):
print("Your product_id is not valid.")
ans = input("Enter your product_id again: ")
return ans
# if not, users are presented with an option to see
# the list of product_ids for each brand
elif ans == "b":
print("What products would you want to see?")
print(" (a) products of specific brand")
print(" (b) choose a brand for your product first from full list of products")
print()
ans = input("Enter: ").lower()
if ans == "a":
# if users don't know the exact brand_id, they can also
# query by inputting part of the name, which will then be
# queried to see if there is any brand name that matches
# the regex pattern including the useri nput
print("Do you know the exact brand_id?")
print(" (y) - yes, I do")
print(" (n) - maybe, I know part (or all) of the name")
ans = input("Enter: ").lower()
if ans == "y":
return get_prod_id_from_brand_list()
elif ans == "n":
ans = input("Enter (part of) the name of the brand: ").lower()
sql = f'WHERE brand_name LIKE \'%{ans}%\''
rows = get_brands(sql)
if len(rows) < 1:
print("Invalid input. Let's start this again.")
get_product_id()
else:
format_brand_list(rows)
return get_prod_id_from_brand_list()
else:
print("Invalid input. Let's retry.")
return get_product_id()
# full list of brands are presented in groups of 25, so the
# users don't have to scroll through a long list of brands
elif ans == "b":
rows = get_brands()
max_len = len(rows)
ans = "n"
range = [0, 25]
print(f"Brands from id={range[0]+1} to id={range[1]+1}")
# continue showing the brands until the user finds the
# brand that they were looking for
while ans == "n" and range[0] < max_len:
format_brand_list(rows[range[0]:range[1]])
range[0] += 25
range[1] += 25
ans = input("Would you like to choose from the given list? \
Enter (y) for yes, (n) for no.").lower()
return get_prod_id_from_brand_list()
else:
print("Invalid input. Let's retry.")
return get_product_id()
else:
print("Invalid input. Let's retry.")
return get_product_id()
# a Python function that walks the admin user through all the
# possible options to get the id of the brand that they
# want to reference
def get_brand_id():
print("Choose an option")
print(" (a) I know a precise brand_id I want")
print(" (b) I want to see the list of brands")
ans = input("Enter: ").lower()
if ans == "a":
ans = input("Enter brand_id: ")
while not brand_id_is_valid(ans):
print("Your brand_id is not valid.")
ans = input("Enter your brand_id again: ")
return ans
elif ans == "b":
rows = get_brands()
max_len = len(rows)
ans = "n"
range = [0, 25]
print(f"Brands from id={range[0]+1} to id={range[1]+1}")
# continue showing the brands until the user finds the
# brand that they were looking for
while ans == "n" and range[0] < max_len:
format_brand_list(rows[range[0]:range[1]])
range[0] += 25
range[1] += 25
ans = input("Would you like to choose from the given list? \
Enter (y) for yes, (n) for no.").lower()
ans = input("Enter your brand_id: ")
while not brand_id_is_valid(ans):
ans = input("Your brand_id is invalid. Try again: ")
return ans
else:
print("Invalid input. Let's try again.")
return get_brand_id()
# helper function to ask the user for their
# desired product type filter
def get_prod_type():
print("Choose product type")
print(" (a) Moisturizer")
print(" (b) Cleanser")
print(" (c) Treatment")
print(" (d) Face Mask")
print(" (e) Eye Cream")
print(" (f) Sun Protect")
prod_type = input("Enter: ").lower()
while prod_type not in "abcdef":
prod_type = input("Invalid option. Re-enter: ").lower()
mapping = {
"a": "Moisturizer",
"b": "Cleanser",
"c": "Treatment",
"d": "Face Mask",
"e": "Eye cream",
"f": "Sun protect"
}
return mapping[prod_type]
# helper function ask the user for their
# desired skin type filter
def get_skin_type():
print("Choose skin type")
print(" (a) Combination")
print(" (b) Dry")
print(" (c) Normal")
print(" (d) Oily")
print(" (e) Sensitive")
skin_type = input("Enter: ").lower()
while skin_type not in "abcde":
skin_type = input("Invalid option. Re-enter: ").lower()
mapping = {
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4
}
res = [0 for _ in range(5)]
res[mapping[skin_type]] = 1
return res
#############################
# (a) Inventory Functions #
#############################
# handles the case where the user wants to check the
# current inventory
def check_inventory():
print("What would you want to do?")
print(' (a) View inventory for given product')
print(' (b) View inventory for given brand')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
# show inventory for chosen product
prod_id = get_product_id()
rows = get_curr_inventory(f"WHERE product.product_id={prod_id} ")
if rows == -1:
print("Invalid product_id.")
check_inventory()
else:
format_inventory_res(rows)
elif ans == 'b':
brand_id = get_brand_id()
rows = get_curr_inventory(f"WHERE brand.brand_id = {brand_id}")
if rows == -1:
print("Invalid brand_id.")
check_inventory()
else:
format_inventory_res(rows)
else:
print("INVALID INPUT! Choose from the given options")
check_inventory()
# handles update inventory event
# user may either change the inventory of a specific product
# or change the inventory of all products of a brand
def update_inventory():
print("What would you want to do?")
print(" (a) Update inventory of a product")
print(" (b) Update inventory of all products of a brand")
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
prod_id = get_product_id()
(_, _, curr_inv) = get_curr_inventory('WHERE product.product_id=%s'
% prod_id)[0]
print(f"The current inventory of {prod_id} is {curr_inv}")
print("What would you want to update this inventory to?")
new_inv = int(input('Enter a number: '))
sql = 'UPDATE store SET inventory=%s WHERE product_id=%s'\
% (new_inv, prod_id)
rows = execute_insert_delete_update(sql)
if rows == -1:
print("Invalid inputs. Retry.")
update_inventory()
else:
print("Success!")
elif ans == 'b':
brand_id = get_brand_id()
curr_inv = get_curr_inventory('WHERE brand.brand_id=%s' % brand_id)
print(f"The current inventory of products of {brand_id} is:")
format_inventory_res(curr_inv)
print("What's the inventory count you would like to add or subtract?")
new_inv = int(input('Enter a number: '))
# user may add to or subtract from the current inventory
if new_inv > 0:
sql = 'UPDATE store SET inventory=inventory+%s WHERE brand_id=%s'\
% (new_inv, brand_id)
else:
sql = 'UPDATE store SET inventory=inventory-%s WHERE brand_id=%s'\
% (abs(new_inv), brand_id)
rows = execute_query(sql)
if rows == -1:
print("Invalid inputs. Retry.")
update_inventory()
else:
print("Success!")
else:
print("INVALID INPUT! Choose from the given options")
update_inventory()
# handler for updating inventory
# should call relevant functions depending on user choice
def handle_update_inventory():
print("What would you want to do?")
print(' (a) Check inventory')
print(' (b) Update inventory')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
check_inventory()
elif ans == 'b':
update_inventory()
else:
print("INVALID INPUT! Choose from the given options")
show_options()
#############################
# (b) Product List Funcs #
#############################
# get the largest product_id in product table
# so that we can figure out the product_id for the item we are inserting
def get_largest_product_id():
sql = 'SELECT MAX(product_id) FROM product ORDER BY product_id DESC;'
res = execute_query_single(sql)
return int(res[0])
# handler for updating product table
# user may either add more product
# or delete an existing product from the table
def handle_update_product():
print("What would you want to do?")
print(' (a) Add more products')
print(' (b) Delete products')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
# user should input all the required attributes for product table
print("Ok! Then we need the following information from you:")
name = input("First, enter the name of this new product: ")
print("Now, what type is this new product?")
prod_type = get_prod_type()
print("What is the brand of this product?")
print("Follow the instructions to input correct brand.")
print("If the brand doesn't exist, create a new brand first.")
brand_id = get_brand_id()
price = float(input("What is the price of your new product? Enter number:"))
skin_type = get_skin_type()
product_id = get_largest_product_id() + 1
sql = f'INSERT INTO product (product_id, product_name, product_type, \
price, rating, is_combination, is_dry, is_normal, is_oily, \
is_sensitive) VALUES ({product_id}, \'{name}\', \'{prod_type}\',\
{price}, 0.0, {skin_type[0]}, {skin_type[1]}, {skin_type[2]}, \
{skin_type[3]}, {skin_type[4]});'
execute_insert_delete_update(sql)
# make sure to update store table as well! since each product is
# always linked to a brand
sql2 = 'INSERT INTO store (product_id, brand_id, inventory) \
VALUES (%d, %s, %d);' % (product_id, brand_id, 0)
execute_insert_delete_update(sql2)
elif ans == 'b':
# get the product_id of the product to delete
product_id = int(get_product_id())
print(f"Confirming that you want to delete product with id {product_id}")
# get user confirmation, since this cannot be undone
ans = input("Enter (y) for yes, other keys for no: ").lower()
if ans == "y":
sql = 'DELETE FROM product WHERE product_id=%d' % product_id
execute_insert_delete_update(sql)
print("Successfully removed the product!")
else:
print("Ok! Taking you back to main page.")
print()
show_options()
else:
print("INVALID INPUT! Choose from the given options")
handle_update_product()
#############################
# (c) Brand List Funcs #
#############################
# get the largest brand_id in the brand table
# so we can figure out the brand_id for the item we
# are about to insert
def get_largest_brand_id():
sql = 'SELECT MAX(brand_id) FROM brand ORDER BY brand_id DESC;'
res = execute_query_single(sql)
return int(res[0])
# handler for updating brand event
# users may either add a new brand
# or delete an existing brand
def handle_update_brand():
print("What would you want to do?")
print(' (a) Add a brand')
print(' (b) Delete a brand')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
# need brand_name to create a new brand
print("Ok! Then we need one more piece of information from you:")
name = input("First, enter the name of this new brand: ")
brand_id = get_largest_brand_id() + 1
sql = f'INSERT INTO brand (brand_id, brand_name) VALUES \
({brand_id}, \'{name}\');'
execute_insert_delete_update(sql)
print("Successfully executed the query!")
print()
show_options()
elif ans == 'b':
# id of the brand to be deleted
brand_id = int(get_brand_id())
print(f"Confirming that you want to delete brand with id {brand_id}")
# get user confirmation, since this cannot be undone
ans = input("Enter (y) for yes, other keys for no: ").lower()
if ans == "y":
sql = 'DELETE FROM brand WHERE brand_id=%d' % brand_id
execute_insert_delete_update(sql)
print("Successfully removed the product!")
else:
print("Ok! Taking you back to main page.")
print()
show_options()
else:
print("INVALID INPUT! Choose from the given options")
handle_update_brand()
#############################
# (d) User Info Funcs #
#############################
# checks if the username already exists in the user_info table
def username_exists(username):
sql = 'SELECT * FROM user_info WHERE username = \'%s\'' % username
result = execute_query_single(sql)
if result[0] != None:
return True
return False
# handles all user update events
# should be able to add a user
# delete an existing user from the table
# or view the entire list of users for the service
def handle_update_user():
print("What would you want to do?")
print(' (a) Add a user')
print(' (b) Delete a user')
print(' (c) View a list of all users')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
# get all the necessary fields to call sp_add_user procedure
print("Ok! Then we need a couple more information from you: ")
name = input("First, enter username of this new user: ")
password = input("Now, enter the user's password: ")
sql = f'CALL sp_add_user(\'{name}\', \'{password}\');'
execute_insert_delete_update(sql)
print("Successfully executed the query!")
print()
show_options()
elif ans == 'b':
# since this is an admin user, we don't require them to be
# authenticated to delete the user from the website
name = input("Enter username of the user you would like to remove: ")
while not username_exists(name):
name = input("Username doesn't exist in our database. Re-enter: ")
print(f"Confirming that you want to delete user: {name}")
# get user confirmation, since this cannot be undone
ans = input("Enter (y) for yes, other keys for no: ").lower()
if ans == "y":
sql = 'DELETE FROM user_info WHERE username=\'%s\'' % name
execute_insert_delete_update(sql)
print("Successfully removed the user!")
else:
print("Ok! Taking you back to main page.")
print()
show_options()
elif ans == 'c':
# shows the list of all users
sql = 'SELECT username FROM user_info;'
rows = execute_query(sql)
format_user(rows)
print()
show_options()
else:
print("INVALID INPUT! Choose from the given options")
handle_update_brand()
#############################
# (e) Statistics for Admin #
#############################
# handles the event of checking inventory value
# user can either get the total inventory value per brand
# or see the top 5 brands with the highest or the least
# remaining in inventory
def check_inventory_value():
print("What would you want to do?")
print(' (a) - Total inventory value per brand')
print(' (b) - 5 brands with the highest remaining inventory')
print(' (c) - 5 brands with the least remaining inventory')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
rows = get_inventory_per_brand()
if len(rows) > 0:
format_inventory_val(rows)
else:
print('No data to show.')
handle_view_statistics()
elif ans == 'b':
rows = get_inventory_per_brand('ORDER BY total_inventory_value DESC LIMIT 5')
if len(rows) > 0:
format_inventory_val(rows)
else:
print('No data to show.')
handle_view_statistics()
elif ans == 'c':
rows = get_inventory_per_brand('ORDER BY total_inventory_value ASC LIMIT 5')
if len(rows) > 0:
format_inventory_val(rows)
else:
print('No data to show.')
handle_view_statistics()
else:
print('INVALID INPUT! Retry.')
handle_view_statistics()
# handles all statistics options related to
# the sales of products and brands
def check_sales():
print("What would you want to do?")
print(' (a) - Check total sales')
print(' (b) - Check top sales (i.e. highest dollar amount in sales)')
print(' (c) - Check best sales (i.e. highest number of units sold)')
print(' (d) - Check the most recent sales')
print()
ans = input('Enter an option: ').lower()
if ans == 'a':
# handles checking for the total sales event
print("Would you want to check the total sales of...")
print(" (a) a brand?")
print(" (b) a product?")
print(" (c) each brand?")
print(" (d) each product?")
print(" (e) overall?")
print("Any brand/product with no sales will not be shown.")
ans = input("Enter an option: ").lower()
if ans not in "abcde":
print("Invalid input. Retry.")
check_sales()
else:
if ans == "a":
brand_id = get_brand_id()
rows = get_brand_sales(brand_id)
elif ans == "b":
prod_id = get_product_id()
rows = get_product_sales(prod_id)
elif ans =="c":
rows = get_all_brand_sales()
elif ans == "d":
rows = get_all_product_sales()
elif ans == "e":
rows = get_all_sales()
# handle the case where we have not enough data
if len(rows) == 0:
print("Not enough sales info yet! :(")
else:
format_sales(rows)
print()
show_options()
elif ans == 'b':
# handles checking for the top selling events
# here, top-selling = has generated biggest revenue from sales
print("Would you want to see the top selling")
print(" (a) brand?")
print(" (b) product?")
print(" (c) products for a brand?")
print("Any brand/product with no sales will not be shown.")
ans = input("Enter an option: ").lower()
if ans not in "abc":
print("Invalid input given. Retry.")
check_sales()
else:
if ans == "a":
rows = get_top_selling_brand()
elif ans == "b":
rows = get_top_selling_product()
elif ans == "c":