-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
274 lines (248 loc) · 7.64 KB
/
controller.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
'''
Controller of Foodstitute
'''
import settings
import model
import view
import argparse
import pdb
from random import randint
KEYS = ['code', 'product_name', 'categories', 'nutrition_grade_fr']
#####################################################
# RUN & CHOOSE ACTION #
#####################################################
def run():
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"Pass",
help="Password of the SQL DB foodstitute for the account" +
settings.USER
)
# Get args
args = parser.parse_args()
# Define password
Pass = args.Pass
# Connect to DB
db = model.DataBase(Pass)
view.start_view()
# history = [choose_action]
# function, parameter = history[-1]('', db)
choose_action('', db)
def choose_action(_, db):
'''
chose a basic action
Arguments:
_ unused
db {database}
'''
question = 'What do you want?'
answers = ['Substitute a product.', 'Manage favourites.']
res = {
'0': choose_category,
'1': manage_favourites,
'B': quit_app,
'Q': quit_app,
}
choice = view.get_choice(question, answers)
res.get(choice)(_, db)
#####################################################
# CHOOSE PRODUCTS IN A CATEGORY #
#####################################################
def choose_category(_, db):
'''
Chose a category
Arguments:
_ unused
db {database}
'''
question = 'Which category of product do you want to use?'
answers = model.CATEGORIES
res = {
'B': choose_action,
'Q': quit_app,
}
choice = view.get_choice(question, answers)
# Default is choose_product, else choose_action or quit_app
if choice.isdigit():
next_parameter = answers[int(choice)]
else:
next_parameter = choice
# call chosen function with next_parameter
res.get(choice, choose_product)(next_parameter, db)
def choose_product(category, db, warning=''):
'''
chose a product in a category
Arguments:
category {string}
db {database}
'''
question = 'Which product do you want to substitute?'
res = {
'B': choose_category,
'Q': quit_app,
}
# list of Product
products = db.get_products(category)
# list of codes
products_codes = [product.code for product in products]
# list of names
products_names = [
prod.name + ' (' + prod.nutrition_grade.upper() + ')'
for prod in products
]
choice = view.get_choice(question, products_names, warning)
if choice.isdigit():
next_parameter = products[int(choice)]
else:
next_parameter = choice
# By default, return random_healthier
res.get(choice, random_healthier)(next_parameter, db)
def random_healthier(product, db):
'''
Pick a random healthier product in a category
Arguments:
product {Product}
db {Database}
'''
def OK_for_substitute(substitute_list, database, answer='1'):
question = "Do you want to save it?"
answers = ['Yes', 'No']
res = {
'0': save_it,
'B': choose_product,
'Q': quit_app
}
while answer == '1':
print('This product is healthier')
# pick a random substitute
substitute = substitute_list[
randint(0, len(substitute_list) - 1)
]
print(substitute)
answer = view.get_choice(question, answers)
if answer == 'B':
next_parameter = substitute.category
else:
next_parameter = substitute
res.get(answer)(next_parameter, database)
def save_it(substitute, database, prod=product):
database.save_favourite(prod, substitute)
print(substitute)
print("****REPLACES:")
print(prod)
choose_action(prod.code, database)
code = product.code
category = product.category
# get product candidtes to be substitutes
potential_substitutes = db.find_healthier(product)
# check if there are candidates
if not potential_substitutes:
warning = "NO HEALTHIER PRODUCT IN CATEGORY!\n"
choose_product(product.category, db, warning)
else:
OK_for_substitute(potential_substitutes, db)
# def choose_healthier(product, db):
# '''
# Choose a healthier product in a category
# Arguments:
# product {Product}
# db {Database}
# '''
# # Note this function is not used.
# # The program will now pick a random healthier product
# code = product.code
# category = product.category
# # get product candidtes to be substitutes
# potential_substitutes = db.find_healthier(product)
# # check if there are candidates
# if not potential_substitutes:
# warning = "NO HEALTHIER PRODUCT IN CATEGORY!\n"
# choose_product(product.category, db, warning)
# else:
# question = (
# "Those products are healthier. Which one do you want to save?")
# # make a choice amongst candidates names
# styled_list = [
# prod.name + ' (' + prod.nutrition_grade.upper() + ')'
# for prod in potential_substitutes
# ]
# choice = view.get_choice(
# question, styled_list
# )
# # get the one and then save it.
# substitute = potential_substitutes[int(choice)]
# db.save_favourite(product, substitute)
# print(substitute)
# print("****REPLACE:")
# print(product)
# choose_action(product.code, db)
#####################################################
# MANAGE FAVOURITES #
#####################################################
def manage_favourites(_, db):
'''
Manage favourites
Arguments:
_ unused
db {database}: database
'''
question = 'Which favourite to delete?\n'
res = {
'B': choose_action,
'Q': quit_app,
}
# list of favourites
favourites = db.list_favourites()
# build the fav view of all favourites
fav_view = []
for fav in favourites:
product = fav[0]
substitute = fav[1]
fav_view.append(
product.category + ': ' +
substitute.name + ' (' + substitute.purchase + ')\n ' +
' REPLACE: ' + product.name
)
choice = view.get_choice(question, fav_view, 'Yes, ' + question)
if choice.isdigit():
next_parameter = favourites[int(choice)]
else:
next_parameter = choice
# By default, return confirm
res.get(choice, confirm_delete_favourite)(next_parameter, db)
def confirm_delete_favourite(favourite, db):
'''
Confirm deletion a favourite in db
Arguments:
favourite {(Product, Product)}
db {Database}
'''
question = 'Are you sure you want to delete this favourite?'
res = {
'1': manage_favourites,
'B': manage_favourites,
'Q': quit_app,
}
choice = view.get_choice(question, ['Yes', 'No'], '')
# breakpoint()
if choice == '0':
db.delete_favourite(favourite)
choose_action(favourite, db)
else:
res.get(choice)(favourite, db)
#####################################################
# QUIT APP #
#####################################################
def quit_app(_, db):
'''
Exit app with 1 fake arguments
'''
connection = db.connection
# Commit changes
db.connection.commit()
# Finally close connection
connection.close()
exit
if __name__ == "__main__":
run()