/
functions.py
199 lines (157 loc) · 6.37 KB
/
functions.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
# -*- coding: Utf-8 -*-
'''
Here are all the main functions used into our software
'''
from database import create_db, create_tables, fill_products
from constants import cat_size, template, no_subs
from sql_requests import read_cat, show_prod, select_prod, find_substitute, cat_name
from sql_requests import show_subs_list, show_favorite, show_original_prod
from classes import Substitute
def update_database(mycursor, mydb):
'''We update the database'''
create_db(mycursor)
create_tables(mycursor)
fill_products(mycursor, mydb)
#_________________________________________________
#
# CATEGORY CHOICE
#_________________________________________________
def category_choice(mycursor):
'''Display category list'''
cat_choice = "\n"
#sql request
results = read_cat(mycursor)
for result in results:
cat_choice += (str(result[0]) + ": " + result[1] + "\n")
cat_choice += '\nChoisissez votre catégorie :\n'
return cat_choice, results[-1][0]
def cat_loop(mycursor, mydb):
'''While user has not chose a category with a valid input, the loop is true'''
choice, nb_cat = category_choice(mycursor)
while True:
user_cat = input(choice)
if user_cat.isdigit() and 1 <= int(user_cat) <= nb_cat:
prod_loop(mycursor, user_cat, mydb)
break
else:
print("\nERREUR: Commande inconnue\n")
#_________________________________________________
#
# PRODUCT CHOICE
#_________________________________________________
def product_choice(mycursor, user_answer):
'''Display product list from the category chosen'''
#sql request
results = show_prod(mycursor, user_answer)
prod_choice = ""
i = 1
for result in results:
prod_choice += (str(i) + ": " + result[0] + "\n")
i += 1
prod_choice += '\nChoisissez votre produit:\n'
return prod_choice, i - 1
def prod_loop(mycursor, user_cat, mydb):
'''While user has not chose a product with a valid input, the loop is true'''
choice, nb_prod = product_choice(mycursor, user_cat)
while True:
user_prod = input(choice)
if user_prod.isdigit() and 1 <= int(user_prod) <= nb_prod:
result, subs_id, prod_id = show_result(mycursor, user_cat, user_prod)
print(result)
save_subs_loop(mycursor, subs_id, prod_id, mydb)
break
else:
print("\nERREUR: Commande inconnue\n")
#_________________________________________________
#
# SHOW PRODUCT AND SUBSTITUTE
#_________________________________________________
def show_product(mycursor, user_cat, user_prod):
'''We formate the chosen product, and return the result'''
prod_id = (int(user_cat) - 1) * cat_size + int(user_prod)
#sql request
result = select_prod(mycursor, prod_id)
prod_grade = result[1]
product = formate_item(result)
return product, prod_grade, prod_id
def show_substitute(mycursor, user_cat):
'''We formate the chosen substitute, and return the result'''
#sql request
result = find_substitute(mycursor, user_cat)
subs_grade = result[1]
subs_id = result[4]
substitute = formate_item(result)
return substitute, subs_grade, subs_id
def formate_item(result):
'''Allows us to formate chosen products'''
text = "----------\nNom : {}\nNutriscore : {}\nMagasin : {}\nLien internet : {}\n----------\n"
formated_text = text.format(result[0], result[1], result[2], result[3])
return formated_text
def show_result(mycursor, user_cat, user_prod):
'''We display chosen product and substitute in the console'''
category = cat_name(mycursor, user_cat)
show_cat = "\nCatégorie : {}\n".format(category[0])
product, prod_grade, prod_id = show_product(mycursor, user_cat, user_prod)
substitute, subs_grade, subs_id = show_substitute(mycursor, user_cat)
#Text is different if chosen product has not better substitute
if prod_grade != subs_grade:
result = show_cat + template.format(product, substitute)
else:
result = show_cat + template.format(product, no_subs)
subs_id = prod_id
return result, subs_id, prod_id
#_________________________________________________
#
# SAVE SUBSTITUTE
#_________________________________________________
def save_subs_loop(mycursor, subs_id, prod_id, mydb):
'''User has to chose if he wants to saved the result as favorite'''
while True:
user_answer = input("Désirez-vous ajouter ce produit à vos favoris ? [o/n]\n")
if user_answer == "o":
new_subs = Substitute(subs_id, prod_id)
new_subs.add_to_db(mycursor, mydb)
print("\nProduit enregistré\n")
break
elif user_answer == "n":
break
else:
print("\nERREUR: Commande inconnue\n")
#_________________________________________________
#
# SAVE SUBSTITUTE
#_________________________________________________
def favorite_list(mycursor):
'''We display the favorite list'''
#sql request
results = show_subs_list(mycursor)
subs_list = "\n"
i = 1
for result in results:
subs_list += (str(i) + ": " + result[0] + "\n")
i += 1
subs_list += '\n\nChoisissez un produit à consulter :\n'
return subs_list, i - 1
def display_result(favorite, original_prod):
'''We formate chosen favorite and display the result in our console'''
formated_fav = formate_item(favorite)
formated_prod = formate_item(original_prod)
print(("\nNotre suggestion :\n{}").format(formated_fav))
print(("\nVotre choix initial :\n{}\n").format(formated_prod))
def favorite_loop(mycursor):
'''User has to which favorite he wants to read'''
subs_list, nb_subs = favorite_list(mycursor)
if nb_subs != 0:
while True:
user_answer = input(subs_list)
if user_answer.isdigit() and 1 <= int(user_answer) <= nb_subs:
favorite = show_favorite(mycursor, user_answer)
original_prod = show_original_prod(mycursor, user_answer)
display_result(favorite, original_prod)
go_back = input("\nPressez \'1\' pour consulter un autre favori, \'2\' pour revenir au menu principal\n")
if go_back == '2':
break
else:
print("\nERREUR: Commande inconnue\n")
else:
print("\nAuncun favori enregistré\n")