-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
346 lines (247 loc) · 9.64 KB
/
app.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
import json
import logging
from os import getenv
import pyodbc
from dotenv import load_dotenv
from flask import Flask, abort, redirect, render_template, request, url_for
from services import *
# Load environment variables
load_dotenv()
app = Flask(__name__)
# Connection to the SQL Server
server = getenv("DB_SERVER", ".") # Fallback to '.' if not set
database = getenv("DB_NAME", "recipe") # Fallback to 'recipe' if not set
conn = pyodbc.connect(
f"Driver={{SQL Server}};Server={server};Database={database};Trusted_Connection=yes;"
)
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
logging.basicConfig(level=logging.DEBUG)
# Main App View
@app.route("/")
def home():
result = get_all_recipes(cursor)
return render_template("home/home.html", recipes=result)
# region Recipe
@app.route("/create")
def create_recipe():
users = get_all_users(cursor)
categories = get_all_categories(cursor)
ingredients = get_all_ingredients(cursor)
return render_template(
"recipe/create.html",
users=users,
categories=categories,
ingredients=ingredients,
)
@app.route("/submit_newrecipe", methods=["POST"])
def submit_newrecipe():
if request.method == "POST":
name = request.form["name"]
description = request.form["description"]
cooking_time = request.form["cooking_time"]
user_id = request.form["user_id"]
category_id = request.form["category_id"]
ingredients_data = request.form["ingredients"]
ingredients = json.loads(ingredients_data)
# logging.debug(f"Selected ingredients: {ingredients_data}")
# Insert recipe first
recipe_id = insert_recipe(
cursor, name, description, cooking_time, user_id, category_id
)
# Insert recipe-ingredient relationships
for ingredient in ingredients:
insert_recipe_ingredient(
cursor,
recipe_id,
ingredient["id"],
ingredient["quantity"],
ingredient["unit"],
)
return redirect(url_for("home"))
@app.route("/edit/<int:recipe_id>")
def edit_recipe(recipe_id):
recipe = get_recipe(cursor, recipe_id)
if not recipe:
abort(404)
users = get_all_users(cursor)
categories = get_all_categories(cursor)
selected_ingredients = get_recipe_ingredients(cursor, recipe_id)
ingredients = get_all_ingredients(cursor)
return render_template(
"recipe/edit.html",
recipe=recipe,
users=users,
categories=categories,
selected_ingredients=selected_ingredients,
ingredients=ingredients,
)
@app.route("/submit_updaterecipe", methods=["POST"])
def update():
if request.method == "POST":
# Update recipe info
recipe_id = request.form["recipe_id"]
name = request.form["name"]
description = request.form["description"]
cooking_time = request.form["cooking_time"]
user_id = request.form["user_id"]
category_id = request.form["category_id"]
update_recipe(
cursor, name, description, cooking_time, user_id, category_id, recipe_id
)
# Update ingredients
new_ingredients_data = request.form["ingredients"]
new_ingredients = json.loads(new_ingredients_data)
old_ingredients = get_recipe_ingredients(cursor, recipe_id)
# Convert old ingredients to a set of names for easy comparison
old_ingredient_names = {ingredient[1] for ingredient in old_ingredients}
# Insert new ingredients not in old ingredients
for ingredient in new_ingredients:
if ingredient["name"] not in old_ingredient_names:
insert_recipe_ingredient(
cursor,
recipe_id,
ingredient["id"],
ingredient["quantity"],
ingredient["unit"],
)
# Remove old ingredients not in new ingredients
new_ingredient_names = {ingredient["name"] for ingredient in new_ingredients}
for ingredient in old_ingredients:
ingredient_name = ingredient[1]
ingredient_id = ingredient[0]
if ingredient_name not in new_ingredient_names:
delete_recipe_ingredient(cursor, recipe_id, ingredient_id)
conn.commit()
return redirect(url_for("home"))
@app.route("/delete/<int:recipe_id>", methods=["POST"])
def delete_recipe(recipe_id):
recipe = get_recipe(cursor, recipe_id)
if not recipe:
abort(404)
query2 = "DELETE FROM Recipes WHERE recipe_id = ?;"
cursor.execute(query2, (recipe_id,))
conn.commit()
if cursor.rowcount > 0:
return redirect(url_for("home"))
else:
return "Error: Recipe not found or could not be deleted", 404
# Recipe details route
@app.route("/recipe_details/<int:recipe_id>")
def recipe_details(recipe_id):
recipe = get_recipe(cursor, recipe_id)
ingredients = get_recipe_ingredients(cursor, recipe_id)
if not recipe:
abort(404)
# Fetch reviews for the recipe
reviews = get_reviews_for_recipe(cursor, recipe_id)
return render_template(
"home/recipe_details.html",
recipe=recipe,
ingredients=ingredients,
reviews=reviews,
)
@app.route("/review/delete/<int:recipe_id>/<int:review_id>", methods=["POST"])
def delete_review(review_id, recipe_id):
delete_review_by_id(cursor, review_id)
return redirect(url_for("recipe_details", recipe_id=recipe_id))
@app.route("/review/edit/<int:recipe_id>/<int:review_id>", methods=["POST"])
def edit_review(review_id, recipe_id):
if request.method == "POST":
reviewer_name = request.form["user_name"]
review_text = request.form["review_text"]
star_rating = request.form["star_rating"]
update_review_by_id(cursor, review_id, reviewer_name, review_text, star_rating)
return redirect(url_for("recipe_details", recipe_id=recipe_id))
@app.route("/submit_search", methods=["GET"])
def search():
if request.method == "GET":
keyword = request.args.get("keyword")
results = search_recipe(cursor, keyword)
return render_template("home/search_result.html", search_results=results)
# endregion
# region User
@app.route("/all_user")
def all_users_with_recipes():
users = get_all_users(cursor)
authors_with_recipes = get_authors_with_recipes(cursor)
logging.debug(f"Recipes with authors: {authors_with_recipes}")
return render_template(
"alluser/all_user.html", users=users, authors_with_recipes=authors_with_recipes
)
@app.route("/create_authour")
def create_author():
return render_template("autour/create_authour.html")
@app.route("/submit_newauthor", methods=["POST"])
def submit_newauthor():
if request.method == "POST":
name = request.form["name"]
age = request.form.get("age")
email = request.form.get("email")
query = """INSERT INTO Users(full_name,age,email)
VALUES (?,?,?);"""
cursor.execute(query, (name, age, email))
conn.commit()
return redirect(url_for("all_users_with_recipes"))
@app.route("/user/delete/<int:user_id>")
def delete_user(user_id):
delete_user_by_id(cursor, user_id)
return redirect(url_for("all_users_with_recipes"))
@app.route("/user/edit/<int:user_id>")
def edit_user(user_id):
new_name = request.args.get("name")
new_age = request.args.get("age")
new_email = request.args.get("email")
update_user_by_id(cursor, user_id, new_name, new_age, new_email)
return redirect(url_for("all_users_with_recipes"))
# endregion
# region Ingredients
@app.route("/ingredients")
def show_ingredients():
ingredients = get_all_ingredients(cursor)
return render_template("/ingredient/all_ingredients.html", ingredients=ingredients)
@app.route("/ingredient/create/")
def create_ingredient():
name = request.args.get("name")
name_exists = get_ingredient_id(cursor, name)
if name_exists is not None:
return "Ingredient Already Exists", 400
# create new ingredient
insert_ingredient(cursor, name)
return redirect(url_for("show_ingredients"))
@app.route("/ingredient/delete/<int:ingredient_id>")
def delete_ingredient(ingredient_id):
delete_ingredient_by_id(cursor, ingredient_id)
return redirect(url_for("show_ingredients"))
@app.route("/ingredient/edit/<int:ingredient_id>")
def edit_ingredient(ingredient_id):
# Get query parameter 'name'
new_name = request.args.get("name")
if not new_name:
return "Name parameter is required", 400
# Update ingredient name
update_ingredient_name(cursor, ingredient_id, new_name)
return redirect(url_for("show_ingredients"))
# Route to add a new review
@app.route("/review/<int:recipe_id>", methods=["POST"])
def add_review(recipe_id):
if request.method == "POST":
reviewer_name = request.form["user_name"]
review_text = request.form["review_text"]
star_rating = request.form["star_rating"]
insert_review(cursor, recipe_id, review_text, star_rating, reviewer_name)
return redirect(url_for("recipe_details", recipe_id=recipe_id))
# endregion
@app.route("/submit_newcategory", methods=["POST"])
def submit_newcategory():
# get category name from request
data = request.get_json()
category_name = data.get("category_name", "").strip()
if category_name:
category_exists = get_category_by_name(cursor, category_name)
# See if category already exists before inserting
if not category_exists:
insert_category(cursor, category_name)
app.run(debug=True)
cursor.close()
conn.close()