-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
415 lines (345 loc) · 11.5 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""
/******************************************************************
* *
* Main file of the Marmita web application. *
* This file contains all the routes for the web application. *
* It is used to launch the server locally. *
* *
* Copyright Marmit@ - HowlingByte © 2023 *
* Mozilla Public License 2.0 *
* *
******************************************************************/
"""
__author__ = "HowlingByte"
__version__ = "1.15"
__license__ = "MPL 2.0"
import sqlite3
from bottle import (
request,
route,
error,
get,
post,
run,
view,
static_file,
redirect,
abort,
HTTPResponse,
)
DATABASE: str = "database/marmita.db"
class Famille: # pylint: disable=R0903
"""
Class representing a family of recipes. A family has the following attributes:
- famille_id: the family identifier in the database
- nom: the name of the family
- image: the image of the family
"""
def __init__(self, famille_id: int, nom: str, image: str):
self.famille_id: int = famille_id
self.nom: str = nom
self.image: str = image
class Ingredient: # pylint: disable=R0903
"""
Class representing an ingredient. An ingredient has the following attributes:
- ingredient_id: the identifier of the ingredient in the database
- nom: the name of the ingredient
- quantite: the quantity of the ingredient
- unite: the unit of measurement for the quantity of the ingredient
"""
def __init__(
self, ingredient_id: int, nom: str, quantite: int, unite: str | None = None
):
self.ingredient_id: int = ingredient_id
self.nom: str = nom
self.quantite: int = quantite
self.unite: str | None = unite
class Etape: # pylint: disable=R0903
"""
Class that presents a recipe step. A step has the following attributes:
- num: the number of the step in the recipe
- texte: the text of the step
"""
def __init__(self, num: int, texte: str):
self.num: int = num
self.texte: str = texte
class Recette: # pylint: disable=R0903, R0902
"""
Class representing a recipe. A recipe has the following attributes:
- recette_id: the recipe identifier in the database
- nom: the name of the recipe
- image: the image of the recipe
- cuisson: the recipe cooking time
- nbpers: the number of people for whom the recipe is intended
- diff: the difficulty of the recipe
- ingredients: the list of ingredients in the recipe
- etapes: the list of steps in the recipe
- famille_recette: the recipe family
"""
def __init__( # pylint: disable=R0913
self,
recette_id: int,
nom: str,
image: str,
cuisson: int | None,
nbpers: int | None,
diff: int | None,
ingredients: list[Ingredient] | None,
etapes: list[Etape] | None,
famille_recette: int | Famille,
):
self.recette_id: int = recette_id
self.nom: str = nom
self.image: str = image
self.cuisson: int | None = cuisson
self.nombre_de_personnes: int | None = nbpers
self.difficulte: int | None = diff
self.ingredients: list[Ingredient] | None = ingredients
self.etapes: list[Etape] | None = etapes
self.famille: int | Famille = famille_recette
def open_sql(database: str = DATABASE) -> tuple[sqlite3.Connection, sqlite3.Cursor]:
"""
Function used to open a connection to the database.
:return: the connector and the database cursor
"""
conn: sqlite3.Connection = sqlite3.connect(database)
cur: sqlite3.Cursor = conn.cursor()
return conn, cur
def close_sql(cur: sqlite3.Cursor):
"""
Function used to close a connection to the database.
:param cur: the database cursor
"""
cur.close()
@route("/")
@view("template/accueil.tpl")
def accueil() -> dict[str, list[Famille]]:
"""
Function used to display the home page.
"""
_, cur = open_sql()
cur.execute("SELECT id, nom, image FROM famille")
liste_familles = []
for row in cur:
famille_id = row[0]
famille_nom = row[1]
famille_image = row[2]
famille_obj = Famille(famille_id, famille_nom, famille_image)
liste_familles.append(famille_obj)
close_sql(cur)
return {"listeFamille": liste_familles}
@get("/famille")
@view("template/famille.tpl")
def famille() -> dict[str, list[Recette] | str | int] | None:
"""
Function used to display a family page.
"""
try:
id_request: int = request.query.id # type: ignore # pylint: disable=no-member
conn, cur = open_sql()
# SQL query to retrieve a family's recipes
cur.execute("SELECT * FROM recettes WHERE id_famille = ?", (id_request,))
liste_recettes = []
for row in cur:
recette_id = row[0]
recette_nom = row[1]
recette_image = row[2]
recette_famille = row[6]
recette = Recette(
recette_id,
recette_nom,
recette_image,
None,
None,
None,
None,
None,
recette_famille,
)
liste_recettes.append(recette)
cur.execute("SELECT nom FROM famille WHERE ID = ?", (id_request,))
nom = cur.fetchone()
conn.commit()
close_sql(cur)
return {"listeRecettes": liste_recettes, "nom": nom[0], "id": id_request}
except TypeError:
abort(404)
return None
@route("/recettes/<id_request>")
@view("template/recette.tpl")
def recettes(id_request: int) -> dict[str, Recette] | None: # pylint: disable=R0914
"""
Function used to display a recipe page.
"""
try:
conn, cur = open_sql()
# Query 1 (attributes from the Recipes table)
cur.execute("SELECT * FROM Recettes WHERE ID=?", (id_request,))
infos_recette = cur.fetchone()
conn.commit()
recette_id = infos_recette[0]
recette_nom = infos_recette[1]
recette_image = infos_recette[2]
recette_nb_pers = infos_recette[3]
recette_cuisson = infos_recette[4]
recette_difficulte = infos_recette[5]
recette_famille = infos_recette[6]
# Query to retrieve the family name
cur.execute("SELECT Nom FROM Famille WHERE ID=?", (recette_famille,))
nom_famille = cur.fetchone()
conn.commit()
# Query to retrieve recipe ingredients
cur.execute(
"SELECT Ingredients.ID, Ingredients.Nom, Quantite, Unite \
FROM IngredientsDeRecette \
INNER JOIN Ingredients ON Ingredients.ID=IngredientsDeRecette.ID_ingredients \
WHERE ID_recettes=?",
(recette_id,),
)
liste_ingredients = []
for row in cur:
ingredient_id = row[0]
ingredient_nom = row[1]
ingredient_quantite = row[2]
ingredient_unite = row[3]
if ingredient_quantite.is_integer():
ingredient_quantite = int(ingredient_quantite)
ingredient = Ingredient(
ingredient_id, ingredient_nom, ingredient_quantite, ingredient_unite
)
liste_ingredients.append(ingredient)
conn.commit()
# Query to retrieve recipe steps
cur.execute(
"SELECT Numero, Descriptif FROM EtapesDeRecette \
WHERE ID_recettes=?",
(recette_id,),
)
etapes_recette = []
for row in cur:
etape_num = row[0]
etape_texte = row[1]
etape = Etape(etape_num, etape_texte)
etapes_recette.append(etape)
conn.commit()
close_sql(cur)
famille_recette = Famille(recette_famille, nom_famille[0], "")
recette = Recette(
recette_id,
recette_nom,
recette_image,
recette_cuisson,
recette_nb_pers,
recette_difficulte,
liste_ingredients,
etapes_recette,
famille_recette,
)
return {"recette": recette}
except TypeError:
abort(404)
return None
@post("/chercheRecettes")
@view("template/chercheRecettes.tpl")
def rechercher() -> dict[str, list[Recette] | str] | None:
"""
Function used to display the recipe search page.
"""
# Retrieve data from the form
recette_recherchee: str = request.forms.getunicode("recette") # type: ignore # pylint: disable=no-member
if recette_recherchee != "":
mots_cles = recette_recherchee.split(" ")
condition = "LIKE '%" + mots_cles[0] + "%'"
for i in range(1, len(mots_cles)):
condition += " AND nom LIKE '%" + mots_cles[i] + "%'"
else:
condition = "LIKE '%%'"
# Début de l'ester egg
if (
recette_recherchee.lower() == "apple"
or recette_recherchee.lower() == "🍎"
or recette_recherchee.lower() == "pomme"
):
redirect("https://apple.com")
return None
# Fin de l'ester egg
_, cur = open_sql()
# SQL query to retrieve a family's recipes
cur.execute("SELECT * FROM recettes WHERE nom " + condition)
liste_recettes = []
for row in cur:
recette_id = row[0]
recette_nom = row[1]
recette_image = row[2]
recette_famille = row[6]
recette = Recette(
recette_id,
recette_nom,
recette_image,
None,
None,
None,
None,
None,
recette_famille,
)
liste_recettes.append(recette)
close_sql(cur)
return {"listeRecettes": liste_recettes, "recherche": recette_recherchee}
@route("/contact")
@view("static/html/contact.html")
def contact() -> None:
"""
Function used to display the contact page.
"""
return None
@route("/mentions")
@view("static/html/mentions.html")
def mentions() -> None:
"""
Function used to display the legal information page.
"""
return None
@error(404)
@view("static/html/404.html")
def on_error404(_) -> None:
"""
Function used to display the 404 error page.
"""
return None
@route("/images/<filepath:path>")
def server_static_image(filepath: str) -> HTTPResponse:
"""
Function used to display images.
"""
return static_file(filepath, root="static/images/")
@route("/fonts/<filepath:path>")
def server_static_fonts(filepath: str) -> HTTPResponse:
"""
Function used to display fonts.
"""
return static_file(filepath, root="static/fonts/")
@route("/css/<filepath:path>")
def server_static_css(filepath: str) -> HTTPResponse:
"""
Function used to display css files.
"""
return static_file(filepath, root="static/css/")
@route("/js/<filepath:path>")
def server_static_js(filepath: str) -> HTTPResponse:
"""
Function used to display js files.
"""
return static_file(filepath, root="static/js/")
def start_server() -> None:
"""
Function used to start the server.
"""
run(host="0.0.0.0", port=80)
def main() -> None:
"""
Main function.
"""
run(host="localhost", port=8080, debug=True)
if __name__ == "__main__":
main()