generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
646 lines (542 loc) · 23.2 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
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
import os
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_paginate import Pagination, get_page_args
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
if os.path.exists("env.py"):
import env
# Configurations
app = Flask(__name__)
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
mongo = PyMongo(app)
# Creates admin user
def admin():
""" Admin function to distinguish admin user """
admin_user = mongo.db.users.find_one(
{"username": session["user"]})["admin"]
return admin_user
# Variables
search_query = None
allergen_search_query = None
# Pagination functions
# Pagination used from
# https://github.com/timmorrisdev/MS3-sustainable-supper-club/blob/main/app.py
def paginated(recipes):
PER_PAGE = 6
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page')
offset = page * PER_PAGE - PER_PAGE
return recipes[offset: offset + PER_PAGE]
def pagination_args(recipes):
PER_PAGE = 6
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page')
total = len(recipes)
return Pagination(
page=page, per_page=PER_PAGE, total=total, css_framework="materialize")
# Pages
@app.route("/")
def index():
""" Displays the home page """
return render_template("index.html")
@app.route("/about")
def about():
""" Displays content to user about the site """
allergens = mongo.db.allergens.find().sort("allergen_name", 1)
return render_template("about.html", allergens=allergens)
@app.route("/contact")
def contact():
""" Display contact form and populate with user information
if logged in """
if "user" in session:
first_name = mongo.db.users.find_one(
{"username": session["user"]})["first-name"]
surname = mongo.db.users.find_one(
{"username": session["user"]})["surname"]
name = first_name + " " + surname
email = mongo.db.users.find_one({"username": session["user"]})["email"]
return render_template("contact.html", name=name, email=email)
else:
return render_template("contact.html")
@app.route("/get_recipes")
def get_recipes():
""" Gets recipes from db and displays """
recipes = list(mongo.db.recipes.find())
allergens = mongo.db.allergens.find().sort("allergen_name", 1)
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
# if user not logged in renders recipe page
if "user" not in session:
return render_template(
"recipes.html", recipes=recipes_paginated,
pagination=pagination, allergens=allergens)
# if user logged in renders recipe page with tailored buttons
else:
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
return render_template(
"recipes.html", recipes=recipes_paginated,
pagination=pagination, allergens=allergens, favourites=favourites)
@app.route("/search", methods=["GET", "POST"])
def search():
""" Creates search function based on input and or checkboxes selected """
global search_query
global allergen_search_query
page = 1
# If its not the first search query
if request.method == 'GET':
query = search_query
allergen_query = allergen_search_query
page = request.args.get('page')
# If it is the first search query
elif request.method == 'POST':
query = request.form.get("query")
allergen_query = request.form.getlist("allergen-query")
# Save for next search query
search_query = query
allergen_search_query = allergen_query
# If input and checkbox filled in
if query and allergen_query:
recipe_results = list(mongo.db.recipes.find({
"$and": [
{"allergen_list": {"$all": allergen_query}},
{"$text": {"$search": query}}]
}))
# If just search input
elif query:
recipe_results = list(mongo.db.recipes.find(
{"$text": {"$search": query}}))
# If just checkbox selected
elif allergen_query:
recipe_results = list(mongo.db.recipes.find(
{"allergen_list": {"$all": allergen_query}}))
else:
flash("Please select an allergen or type to search")
return redirect(url_for("get_recipes"))
# Paginate the results
if recipe_results:
recipes_paginated = paginated(recipe_results)
pagination = pagination_args(recipe_results)
# If no results found
else:
flash("Sorry no results found")
return redirect(url_for("get_recipes"))
# If user not logged in renders recipe page
if "user" not in session:
return render_template(
"recipes.html",
recipes=recipes_paginated,
pagination=pagination)
# If user logged in renders recipe page with tailored buttons
else:
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
return render_template(
"recipes.html", recipes=recipes_paginated,
pagination=pagination, favourites=favourites)
# Users
@app.route("/register", methods=["GET", "POST"])
def register():
""" Register view, when form submitted database
is updated """
# If user already logged in
if "user" in session:
flash("You are already logged in")
return redirect(url_for("index"))
# If user not logged in
else:
if request.method == "POST":
# Checks if username is already taken
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# If username is taken, message displayed and reloads page
flash("Username already exists")
return redirect(url_for("register"))
if request.form.get("password") != request.form.get(
"confirm-password"):
# Checks if confirmed password matches original password
# If confirmation
# not met, flash message displayed and reloads page
flash("Your password did not match, please try again")
return redirect(url_for("register"))
register_user = {
# Gets user information from registration form
"username": request.form.get("username").lower(),
"first-name": request.form.get("first-name"),
"surname": request.form.get("surname"),
"email": request.form.get("email"),
"password": generate_password_hash(
request.form.get("password")),
"admin": bool("")
}
# New user created in db
mongo.db.users.insert_one(register_user)
# Put the new user into 'session' cookie
session["user"] = request.form.get("username").lower()
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
mongo.db.profiles.insert_one({
"user_id": ObjectId(user_id),
"favourites": [],
"shopping_list": []})
name = request.form.get("first-name")
flash(
f'Welcome {name} ' +
'you have been successfully registered')
return redirect(url_for("profile", username=session["user"]))
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
""" Allows users to login into an existing account and verifies
username and password """
# If user logged in
if "user" in session:
flash("You are already logged in")
return redirect(url_for("index"))
# If user not logged in
else:
if request.method == "POST":
# Check if username exists in db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# Check hashed password matches user input
if check_password_hash(
existing_user["password"],
request.form.get("password")):
session["user"] = request.form.get(
"username").lower()
flash("Welcome, you have logged in")
return redirect(url_for(
"profile", username=session["user"]))
else:
# If password does not match
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
else:
# If username doesn't exist
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
return render_template("login.html")
@app.route("/profile/<username>", methods=["GET", "POST"])
def profile(username):
""" Displays user profile once logged in """
name = mongo.db.users.find_one(
{"username": session["user"]})["first-name"]
# Only logged in users can view profile
if "user" in session:
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
user_profile = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})
recipes = list(mongo.db.recipes.find(
{"created_by": username}))
favourites = user_profile["favourites"]
shopping_list = user_profile["shopping_list"]
return render_template(
"profile.html",
recipes=recipes, favourites=favourites,
shopping_list=shopping_list, username=username, name=name)
# If not logged in
else:
flash("You need to be signed in for that")
return redirect(url_for("login"))
@app.route("/logout")
def logout():
""" Allows users to logout of account """
# Remove user from session cookie
flash("You have been logged out")
session.pop("user")
return redirect(url_for("index"))
@app.route("/remove_shopping_list/<username>", methods=["GET", "POST"])
def remove_shopping_list(username):
""" Users can remove ingredients from shopping list on profile """
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
# If user logged in
if "user" in session:
username = mongo.db.users.find_one(
{"username": session["user"]}["username"])
if request.method == "POST":
remove_items = request.form.getlist('shopping_list')
update = mongo.db.profiles.update_many(
{"user_id": ObjectId(user_id)},
{"$pull": {"shopping_list": {"$in": remove_items}}})
print(remove_items)
if update:
flash("Ingredients have been removed")
return redirect(url_for("profile", username=session["user"]))
@app.route("/delete_account")
def delete_account():
""" Allows users to delete account from db"""
# Delete user account, display confirmation and redirect to register view
if "user" in session:
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
mongo.db.profiles.delete_one({"user_id": ObjectId(user_id)})
mongo.db.users.delete_one({"username": session["user"]})
session.pop("user")
flash("Account successfully deleted")
return redirect("register")
else:
flash("Sorry you need to be logged in to view this")
return redirect("register")
# Recipes
@app.route("/view_recipe/<recipe_id>")
def view_recipe(recipe_id):
""" Displays selected recipe in seperate page
based on recipe_title """
# Find recipe in db by id
recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
# If user not logged in renders recipe page
if "user" not in session:
return render_template("view_recipe.html", recipe=recipe)
# If user logged in renders recipe page with tailored buttons
else:
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
admin_user = mongo.db.users.find_one(
{"username": session["user"]})["admin"]
# If recipe not found show error
if not recipe:
return render_template("404.html")
return render_template(
"view_recipe.html", recipe=recipe,
favourites=favourites, admin=admin_user)
@app.route("/create_recipe", methods=["GET", "POST"])
def create_recipe():
""" Allow users to create a recipe and save """
# Only users can create recipe
if not session.get("user"):
flash("Please login in to create a recipe")
return redirect(url_for("login"))
# Add recipe to db
if request.method == "POST":
recipe = {
"recipe_title": request.form.get("recipe_title"),
"category_name": request.form.get("category_name"),
"image_url": request.form.get("image_url"),
"allergen_list": request.form.getlist("allergen_list"),
"servings": request.form.get("servings"),
"prep_time": request.form.get("prep_time"),
"cook_time": request.form.get("cook_time"),
"recipe_description": request.form.get(
"recipe_description"),
"ingredients": request.form.getlist("ingredients"),
"method_step": request.form.getlist("method_step"),
"created_by": session["user"]
}
recipe_id = mongo.db.recipes.insert_one(recipe).inserted_id
flash("You created a recipe!")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
# Find catergories and allergen list from db
categories = mongo.db.categories.find().sort("category_name", 1)
allergens = mongo.db.allergens.find().sort("allergen_name", 1)
return render_template(
"create_recipe.html", categories=categories, allergens=allergens)
@app.route("/edit_recipe/<recipe_id>", methods=["GET", "POST"])
def edit_recipe(recipe_id):
""" Allow users to edit existing recipe """
recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
categories = mongo.db.categories.find().sort("category_name", 1)
allergens = mongo.db.allergens.find().sort("allergen_name", 1)
if "user" in session:
# Admin or users that created the recipe can edit the recipe
if admin or session.get("user") == recipe["created_by"]:
# Edit recipe and update db
if request.method == "POST":
edit = {
"recipe_title": request.form.get("recipe_title"),
"category_name": request.form.get("category_name"),
"image_url": request.form.get("image_url"),
"allergen_list": request.form.getlist("allergen_list"),
"servings": request.form.get("servings"),
"prep_time": request.form.get("prep_time"),
"cook_time": request.form.get("cook_time"),
"recipe_description": request.form.get(
"recipe_description"),
"ingredients": request.form.getlist("ingredients"),
"method_step": request.form.getlist("method_step"),
"created_by": session["user"]
}
mongo.db.recipes.replace_one(
{"_id": ObjectId(recipe_id)}, edit, True)
flash("You successfully edited the recipe!")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
else:
flash("You are not authorised to edit this recipe")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
return render_template(
"edit_recipe.html", recipe=recipe,
categories=categories, allergens=allergens)
@app.route("/delete_recipe/<recipe_id>")
def delete_recipe(recipe_id):
""" Users can delete their own recipes from the db """
recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
if "user" in session:
# Admin or users that created the recipe can delete it
if admin or session.get("user") == recipe["created_by"]:
mongo.db.recipes.delete_one({"_id": ObjectId(recipe_id)})
flash("You deleted a recipe!")
return redirect(url_for("get_recipes"))
else:
flash("You are not authorised to delete this recipe")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
else:
flash("You are not authorised to delete this recipe")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
@app.route("/favourite_recipe/<recipe_id>")
def favourite_recipe(recipe_id):
""" Users can save recipes to their profile """
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
user_profile = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})
# Check if user logged in
if "user" in session:
# If recipe already exists in user favourites
if recipe in user_profile["favourites"]:
flash("You have already saved this recipe")
# Updates user favourites with recipe
else:
mongo.db.profiles.update_one(
{"user_id": ObjectId(user_id)},
{"$addToSet": {"favourites": recipe}})
flash("Recipe saved to your favourites!")
return redirect(url_for(
"view_recipe", recipe_id=recipe_id, favourites=favourites))
@app.route("/favourite_recipe_multi/<recipe_id>")
def favourite_recipe_multi(recipe_id):
""" Users can save recipes to their profile """
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
user_profile = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})
# Check is user logged in
if "user" in session:
# If recipe already exists in user favourites
if recipe in user_profile["favourites"]:
flash("You have already saved this recipe")
# Updates user favourites with recipe
else:
mongo.db.profiles.update_one(
{"user_id": ObjectId(user_id)},
{"$addToSet": {"favourites": recipe}})
flash("Recipe saved to your favourites!")
return redirect(url_for(
"get_recipes", recipe_id=recipe_id, favourites=favourites))
@app.route("/remove_favourite/<recipe_id>")
def remove_favourite(recipe_id):
""" Remove recipe from users favourite section in profile """
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
favourites = mongo.db.profiles.find_one(
{"user_id": ObjectId(user_id)})["favourites"]
# Check if user is logged in
if "user" in session:
# Pull recipe in users favourite recipes field
mongo.db.profiles.update_one(
{"user_id": ObjectId(user_id)},
{"$pull": {"favourites": recipe}})
flash("Recipe successfully removed from favourites!")
return redirect(url_for(
"view_recipe", recipe_id=recipe_id,
favourites=favourites))
@app.route("/create_shopping_list/<recipe_id>", methods=["GET", "POST"])
def create_shopping_list(recipe_id):
""" Users can select ingredients to save to own profile """
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
# Checks if user is in session
if "user" in session:
if request.method == "POST":
shopping_list = request.form.getlist('shopping_list')
# Updates user shopping list with selected ingredients
update = mongo.db.profiles.update_one(
{"user_id": ObjectId(user_id)},
{"$push": {"shopping_list": {"$each": shopping_list}}})
print(shopping_list)
if update:
flash("Ingredients added to your shopping list!")
return redirect(url_for(
"view_recipe", recipe_id=recipe_id))
else:
flash("Please login to add ingredients to your shopping list")
return redirect(url_for("login"))
@app.route("/create_comment/<recipe_id>", methods=["GET", "POST"])
def create_comment(recipe_id):
""" Logged in users can leave a comment on a recipe """
recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
# Check if user logged in
if "user" in session:
if request.method == "POST":
comment = {
"comment": request.form.get("user_comment"),
"rating": int(request.form.get("rating")),
"author": session["user"]
}
rating = request.form.get("rating")
print(rating)
# Updates comment field in recipe with user comments
mongo.db.recipes.update_one(
{"_id": ObjectId(recipe_id)},
{"$push": {"comments": comment}})
flash("Comment successfully added")
return redirect(url_for("view_recipe", recipe_id=recipe_id))
else:
flash("Please login to leave a comment")
return redirect(url_for("login"))
# Newsletter subscription
@app.route("/subscribe", methods=["GET", "POST"])
def subscribe():
""" Uploads user email to db for newsletter subscription """
if request.method == "POST":
# Check if email already exists in db
existing_email = mongo.db.subscribers.find_one(
{"email": request.form.get("email").lower()})
if existing_email:
flash("You have already subscribed!")
return redirect(url_for("index"))
# Add new email to db
subscribe = {
"email": request.form.get("email").lower()
}
mongo.db.subscribers.insert_one(subscribe)
flash("You have successfully subscribed!")
return redirect(url_for("index"))
# Error handlers
@app.errorhandler(404)
def page_not_found(error):
""" 404 error handling from flask documentation """
return render_template("404.html"), 404
@app.errorhandler(500)
def server_error(error):
""" 500 error handling from flask documentation """
return render_template("500.html"), 500
# Run the app
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)