generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
847 lines (640 loc) · 26.3 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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# ------- Library Imports -------
import os
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_pymongo import PyMongo
from functools import wraps
from flask_paginate import Pagination, get_page_args
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
if os.path.exists("env.py"):
import env
# ------- Flask App Configuration -------
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)
# ------- Pagination -------
"""
Pagination adapted from:
https://github.com/Sharon-B/Recipe-Box/blob/master/app.py
"""
PER_PAGE = 6
def paginated(recipes):
# Set pagination configuration
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):
# Set pagination configuration
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="bootstrap4")
# ------- Check User Login Function -------
def login_required(f):
"""
Using login_required decorator adapted from:
https://flask.palletsprojects.com/en/2.0.x/patterns/viewdecorators/#login-required-decorator
https://github.com/TravelTimN/flask-task-manager-project/blob/demo/app.py
"""
@wraps(f)
def decorated_function(*args, **kwargs):
# User NOT logged in
if "user" not in session:
flash("Please log in to view this page!")
return redirect(url_for("login"))
# User IS logged in
return f(*args, **kwargs)
return decorated_function
# ------- Home Page -------
@app.route("/")
@app.route("/home")
def home():
"""
Home page displays the 4 latest recipes
"""
latest_recipes = mongo.db.recipes.find().sort("_id", -1).limit(4)
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("index.html",
recipes=latest_recipes,
user=user,
title="Bake It Til You Make It")
else:
return render_template("index.html",
recipes=latest_recipes,
title="Bake It Til You Make It")
# ------- Find Recipes Page -------
@app.route("/find_recipes")
def find_recipes():
"""
Find recipes page displays all recipes (with pagination).
Providing search function for users.
"""
recipes = list(mongo.db.recipes.find())
# pagination code adapted from:
# https://github.com/alandoherty95/reciprocate-app/blob/master/app.py
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("recipe/find_recipes.html",
user=user,
recipes=recipes_paginated,
pagination=pagination,
title="Find Recipes")
return render_template("recipe/find_recipes.html",
recipes=recipes_paginated,
pagination=pagination,
title="Find Recipes")
# ------- Search for Recipes -------
@app.route("/search", methods=['GET', 'POST'])
def search():
"""
Search function allows users to search within text indices for:
recipe_name and description
"""
# query search index set out in MongoDB
query = request.form.get("query")
recipes = list(mongo.db.recipes.find({"$text": {"$search": query}}))
results_count = len(recipes)
# recommended functionality adapted from:
# https://github.com/johnnycistudent/recipe-app/blob/master/app.py
recommended = mongo.db.recipes.find().sort(
"favourite_count", -1).limit(6)
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("recipe/search.html",
recipes=recipes_paginated,
user=user,
recommended=recommended,
query=query,
results_count=results_count,
pagination=pagination,
title="Search Results")
return render_template("recipe/search.html",
recipes=recipes_paginated,
recommended=recommended,
query=query,
results_count=results_count,
pagination=pagination,
title="Search Results")
# ------- Filtered Search for Recipes -------
@app.route("/category_filter/<id>")
def category_filter(id):
"""
Allows for filtered search.
In this case: by CATEGORY
"""
recipes = list(mongo.db.recipes.find({"category": id}))
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("recipe/find_recipes.html",
recipes=recipes_paginated,
user=user,
pagination=pagination,
title="Search Results")
return render_template("recipe/find_recipes.html",
recipes=recipes_paginated,
pagination=pagination,
title="Search Results")
@app.route("/difficulty_filter/<id>")
def difficulty_filter(id):
"""
Allows for filtered search.
In this case: by DIFFICULTY
"""
recipes = list(mongo.db.recipes.find({"difficulty": id}))
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("recipe/find_recipes.html",
recipes=recipes_paginated,
user=user,
pagination=pagination,
title="Search Results")
return render_template("recipe/find_recipes.html",
recipes=recipes_paginated,
pagination=pagination,
title="Search Results")
# ------- Register Page -------
@app.route("/register", methods=["GET", "POST"])
def register():
"""
Allows user to register an account on the database,
rendering register.html.
Prevention of username duplicates included.
"""
if "user" not in session:
if request.method == "POST":
# Checks if username already exists
existing_user = mongo.db.users.find_one(
{"username": request.form.get(
"username").lower()})
# Prevents duplicates
if existing_user:
flash("Username already exists, try another")
return redirect(url_for("register"))
# Sends collected data to user collection
register = {
"username": request.form.get("username").lower(),
"email": request.form.get("email").lower(),
"password": generate_password_hash(
request.form.get("password")),
"user_img": request.form.get("user_img"),
"favourite_recipes": [],
"is_admin": False
}
mongo.db.users.insert_one(register)
# Welcomes user to session
session["user"] = request.form.get("username").lower()
flash("Welcome, Baker {}! Let's get started!".format(
request.form.get("username")))
return redirect(url_for("my_recipes",
username=session["user"]))
return render_template("user/register.html", title="Sign Up")
else:
# If user is logged in
flash("You already have an account!")
return redirect(url_for("my_recipes",
username=session["user"]))
# ------- Login Page -------
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Allows users to login to account
"""
if "user" not in session:
if request.method == "POST":
# checking if user exists
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username".lower())})
if existing_user:
# stored hash password must match user input
if check_password_hash(
existing_user["password"], request.form.get(
"password")):
session["user"] = request.form.get("username").lower()
flash("Welcome back, {}! Let's get started!".format(
request.form.get("username")))
return redirect(url_for(
"my_recipes", username=session["user"]))
else:
# if the password does not match
flash(
"Incorrect Username and/or Password, please try again")
return redirect(url_for("login"))
else:
# username does not exist
flash(
"Incorrect Username and/or Password, please try again")
return redirect(url_for("login"))
return render_template("user/login.html", title="Login")
else:
# If user is logged in
flash("You are already logged in!")
return redirect(url_for("my_recipes",
username=session["user"]))
# ------- Logout Page -------
@app.route("/logout")
@login_required
def logout():
"""
Allows current session user to log out.
"""
# User session cookies removal
session.pop("user")
flash("Logged out. See you soon!")
return redirect(url_for("login"))
# ------- Edit User Page -------
@app.route("/edit_user/<username>", methods=["GET", "POST"])
@login_required
def edit_user(username):
"""
Allows user to edit profile
"""
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
if session["user"] == username:
# Update profile function
if request.method == "POST":
mongo.db.users.update_one({'username': session['user']},
{'$set': {
'user_img': request.form.get(
'user_img')
}})
flash("{}'s Profile Updated!".format(
session["user"]))
return redirect(url_for("my_recipes",
username=session["user"]))
return render_template(
"user/edit_user.html", user=user, title="Edit User")
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for("my_recipes",
username=session["user"]))
# ------- Edit Account Page -------
@app.route("/edit_account/<username>", methods=["GET", "POST"])
@login_required
def edit_account(username):
"""
Allows user to edit their account settings
- Change Password
- Delete Account
"""
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
if session["user"] == username:
# Update profile function
if request.method == "POST":
mongo.db.users.update_one({'username': session['user']},
{'$set': {
'password': generate_password_hash(
request.form.get("password"))
}})
flash("Account Updated! Log back in to confirm changes.".format(
request.form.get("username")))
session.pop("user")
return render_template("user/login.html",
title="Login")
return render_template("user/edit_account.html",
user=user,
title="Edit Account")
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for("my_recipes",
username=session["user"]))
# ------- Delete Profile -------
@app.route("/delete_user/<username>")
@login_required
def delete_user(username):
"""
Allows user to delete profile
"""
if session["user"] == username:
mongo.db.users.remove(
{"username": username.lower()})
flash("Profile Deleted")
session.pop("user")
return redirect(url_for("register"))
else:
# if wrong user
flash("You do not have permission to do that!")
return redirect(url_for("my_recipes",
username=session["user"]))
# ------- My Recipes Page -------
@app.route("/my_recipes/<username>", methods=["GET", "POST"])
@login_required
def my_recipes(username):
"""
Displays User's recipes page with their own created recipes.
Home page for logged in users.
"""
if session["user"] == username:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
recipes = list(
mongo.db.recipes.find(
{"baker": username.lower()}))
recipes_paginated = paginated(recipes)
pagination = pagination_args(recipes)
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for("my_recipes",
username=session["user"]))
return render_template("user/my_recipes.html",
user=user,
recipes=recipes_paginated,
pagination=pagination,
title="My Recipes")
# ------- My Favourites Page -------
@app.route("/my_favourites/<username>", methods=["GET", "POST"])
@login_required
def my_favourites(username):
"""
Displays recipes the user has 'favourited'
Favouriting functionality inspired by:
https://github.com/johnnycistudent/recipe-app/blob/master/app.py
"""
if session["user"] == username:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
recipes = user["favourite_recipes"]
favourites = mongo.db.recipes.find(
{"_id": {"$in": recipes}})
recommended = mongo.db.recipes.find().sort(
"favourite_count", -1).limit(4)
recipes_paginated = paginated(favourites)
pagination = pagination_args(recipes)
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for("my_favourites",
username=session["user"]))
return render_template("user/my_favourites.html",
user=user,
recommended=recommended,
favourites=recipes_paginated,
recipes=recipes,
pagination=pagination,
title="My Favourites")
# ------- Add to Favourites -------
@app.route("/add_to_favourites/<recipe_id>", methods=["GET", "POST"])
@login_required
def add_to_favourites(recipe_id):
"""
Adds recipe to the current users 'favourites'
Favouriting functionality inspired by:
https://github.com/johnnycistudent/recipe-app/blob/master/app.py
"""
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
if session["user"] != recipe["baker"]:
user = mongo.db.users.find_one(
{"username": session["user"]})
favourite_recipes = user["favourite_recipes"]
# Checks if recipe is already in favourites
if ObjectId(recipe_id) not in favourite_recipes:
# Adds recipe_id to users favourite_recipes
mongo.db.users.update_one({"username": session["user"]},
{"$push": {
"favourite_recipes": ObjectId(
recipe_id)}})
# Increments recipes favourite_count by 1
mongo.db.recipes.update_one({"_id": ObjectId(recipe_id)},
{"$inc": {"favourite_count": 1}})
else:
# If recipe is already favourited
flash("Already Added to favourites")
return redirect(url_for("recipe",
recipe_id=recipe_id))
flash("Recipe added to My Favourites")
return redirect(url_for("recipe",
recipe_id=recipe_id))
else:
# if user created recipe, they cannot favourite
flash("This creation is yours!")
return redirect(url_for("recipe",
recipe_id=recipe_id))
# ------- Remove From Favourites -------
@app.route("/remove_from_favourites/<recipe_id>", methods=['GET', 'POST'])
@login_required
def remove_from_favourites(recipe_id):
"""
Allows user to remove favourited recipe from favourites
Favouriting functionality inspired by:
https://github.com/johnnycistudent/recipe-app/blob/master/app.py
"""
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
if session["user"] != recipe["baker"]:
user = mongo.db.users.find_one({"username": session["user"]})
favourites = user["favourite_recipes"]
# Checks if the recipe is in favourites
if ObjectId(recipe_id) in favourites:
# Removes recipe_id from users favourite_recipes
mongo.db.users.update({"username": session['user']}, {"$pull": {
"favourite_recipes": ObjectId(recipe_id)}})
# Decrement recipes favourite_count by 1
mongo.db.recipes.update({"_id": ObjectId(recipe_id)},
{'$inc': {
'favourite_count': -1
}})
flash("Recipe removed from My Favourites")
else:
flash("Recipe is not in My Favourites!")
return redirect(url_for('recipe', recipe_id=recipe_id))
else:
# if user created recipe they cannot favourite
flash("This creation is yours!")
return redirect(url_for("recipe",
recipe_id=recipe_id))
# ------- Individual Recipe Page -------
@app.route("/recipe/<recipe_id>")
def recipe(recipe_id):
"""
Displays the full recipe
"""
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
# checking that the user is in session
if "user" not in session:
# If user is not logged in
favourited = False
return render_template(
"recipe/recipe.html", recipe=recipe,
favourited=favourited, title="Recipe")
else:
# If user is logged in
user = mongo.db.users.find_one({"username": session["user"]})
favourites = user['favourite_recipes']
# Checking if recipe is in favourites
if ObjectId(recipe_id) in favourites:
favourited = True
else:
favourited = False
return render_template("recipe/recipe.html",
recipe=recipe,
favourited=favourited,
user=user,
title="Recipe")
# ------- Create Recipe Page -------
@app.route("/create_recipe", methods=["GET", "POST"])
@login_required
def create_recipe():
"""
Registered users can upload their favourite recipes.
"""
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
if request.method == "POST":
recipe = {
"recipe_name": request.form.get("recipe_name"),
"recipe_img": request.form.get("recipe_img"),
"recipe_url": request.form.get("recipe_url"),
"description": request.form.get("description"),
"category": request.form.get("category"),
"difficulty": request.form.get("difficulty"),
"serving": request.form.get("serving"),
"total_time": request.form.get("total_time"),
"ingredients": request.form.getlist("ingredients"),
"directions": request.form.getlist("directions"),
"baker": session["user"],
"favourite_count": int(0)
}
mongo.db.recipes.insert_one(recipe)
flash("Recipe Shared! Let's get baking!")
return redirect(url_for(
"my_recipes", username=session["user"]))
categories = mongo.db.categories.find().sort("category", 1)
difficulty = mongo.db.level.find().sort("difficulty", 1)
return render_template(
"recipe/create_recipe.html", categories=categories,
difficulty=difficulty, user=user, title="Create Recipe")
# ------- Edit Recipe Page -------
@app.route("/edit_recipe/<recipe_id>", methods=["GET", "POST"])
@login_required
def edit_recipe(recipe_id):
"""
User can edit the recipe they uploaded
"""
# user variable for user image/admin functionality
user = mongo.db.users.find_one(
{"username": session["user"]})
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
if session["user"] == recipe["baker"] or user["is_admin"]:
if request.method == "POST":
mongo.db.recipes.update_one(
{"_id": ObjectId(recipe_id)}, {
'$set': {
"recipe_name": request.form.get("recipe_name"),
"recipe_img": request.form.get("recipe_img"),
"recipe_url": request.form.get("recipe_url"),
"description": request.form.get("description"),
"category": request.form.get("category"),
"difficulty": request.form.get("difficulty"),
"serving": request.form.get("serving"),
"total_time": request.form.get("total_time"),
"ingredients": request.form.getlist("ingredients"),
"directions": request.form.getlist("directions"),
}
})
flash("{} Recipe Updated!".format(
request.form.get("recipe_name")))
return redirect(url_for("recipe", recipe_id=recipe_id))
categories = mongo.db.categories.find().sort("category", 1)
difficulty = mongo.db.level.find().sort("difficulty", 1)
return render_template(
"recipe/edit_recipe.html",
recipe=recipe,
categories=categories,
difficulty=difficulty,
user=user,
title="Edit Recipe")
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for('recipe',
recipe_id=recipe_id))
# ------- Delete Recipe -------
@app.route("/delete_recipe/<recipe_id>")
@login_required
def delete_recipe(recipe_id):
"""
Allows user to delete their uploaded
"""
# user variable for user image/admin functionality
user = mongo.db.users.find_one(
{"username": session["user"]})
recipe = mongo.db.recipes.find_one(
{"_id": ObjectId(recipe_id)})
if session["user"] == recipe["baker"] or user["is_admin"]:
mongo.db.recipes.remove({"_id": ObjectId(recipe_id)})
mongo.db.users.update({},
{"$pull": {
"favourite_recipes": ObjectId(recipe_id)}},
multi=True)
flash("Recipe Deleted")
return redirect(url_for("my_recipes",
username=session["user"]))
else:
# if wrong user
flash("You do not have permission to view this page")
return redirect(url_for('recipe',
recipe_id=recipe_id))
# ------- Error Handlers -------
# adapted from: https://flask.palletsprojects.com/en/1.1.x/errorhandling/
@app.errorhandler(404)
def page_not_found(e):
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("error_handlers/404.html", user=user), 404
else:
return render_template("error_handlers/404.html"), 404
@app.errorhandler(500)
def server_error(e):
if "user" in session:
# user variable for user image
user = mongo.db.users.find_one(
{"username": session["user"]})
return render_template("error_handlers/500.html", user=user), 500
else:
return render_template("error_handlers/500.html"), 500
# ------- Declaration of special variables -------
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)