-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
391 lines (350 loc) · 14.4 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
import os
from datetime import date
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from flask_paginate import Pagination, get_page_args
from werkzeug.security import generate_password_hash, check_password_hash
if os.path.exists("env.py"):
import env
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)
@app.route("/")
@app.route("/recipes")
def recipes():
"""
App route for recipes page, returns all recipes paginated
with 3 results per page
"""
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
total = mongo.db.recipies.find().count()
recipe = list(mongo.db.recipies.find())
recipe_paginated = recipe[offset: offset + per_page]
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
return render_template('recipes.html',
recipes=recipe_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route("/range")
def range():
"""
App route for product range, returns all products paginated
"""
products = list(mongo.db.products.find())
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = offset = (page - 1) * 3 # My formula for getting the right offset
total = mongo.db.products.find().count()
product_paginated = products[offset: offset + per_page]
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
return render_template('range.html',
products=product_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route("/add_recipe", methods=["GET", "POST"])
def add_recipe():
"""
Function to add recipie from the provided form
"""
if request.method == "POST":
steps = request.form.getlist("steps")
step_list = []
for step in steps:
step_list.append(step)
today = date.today()
d1 = today.strftime("%Y-%m-%d")
ingredients = request.form.getlist("ing_name")
ingredients_list = []
for ingredient in ingredients:
ingredients_list.append(ingredient)
new_recipe = {
"created_by": session.get("user"),
"date_created": d1,
"food_name": request.form.get("food_name"),
"difficulty": request.form.get("difficulty"),
"cook_time": int(request.form.get("cook_time")),
"img_url": request.form.get("img_url"),
"ing_name": ingredients_list,
"steps": step_list,
"nationality": request.form.get("nationality")
}
mongo.db.recipies.insert_one(new_recipe)
flash("Thank you for submitting your recipe!")
return render_template("add_recipe.html")
@app.route("/edit_recipe/<recipe_id>", methods=["GET", "POST"])
def edit_recipe(recipe_id):
"""
Function to edit a recipe via the provided form
"""
if request.method == "POST":
steps = request.form.getlist("steps")
step_list = []
for step in steps:
step_list.append(step)
today = date.today()
d1 = today.strftime("%Y-%m-%d")
ingredients = request.form.getlist("ing_name")
ingredients_list = []
for ingredient in ingredients:
ingredients_list.append(ingredient)
submit = {
"created_by": session.get("user"),
"date_created": d1,
"food_name": request.form.get("food_name"),
"difficulty": request.form.get("difficulty"),
"cook_time": int(request.form.get("cook_time")),
"img_url": request.form.get("img_url"),
"ing_name": ingredients_list,
"steps": step_list,
"nationality": request.form.get("nationality")
}
mongo.db.recipies.update({"_id": ObjectId(recipe_id)}, submit)
flash("Recipe Successfully Updated!")
return redirect(url_for("user_profile"))
recipe = mongo.db.recipies.find_one({"_id": ObjectId(recipe_id)})
return render_template("edit_recipe.html", recipe=recipe)
@app.route("/delete_recipe/<recipe_id>")
def delete_recipe(recipe_id):
"""
Function to delete a recipe
"""
mongo.db.recipies.remove({"_id": ObjectId(recipe_id)})
flash("Recipe Deleted")
return redirect(url_for("user_profile"))
@app.route("/register", methods=["GET", "POST"])
def register():
"""
Function to register a new user via the registration page
"""
if request.method == "POST": # check if username already exists in db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
flash("username already exists")
return redirect(url_for("register"))
register = { # Register method from Code Institute walkthrough project
"username": request.form.get("username").lower(),
"password": generate_password_hash(request.form.get("password")),
"name": request.form.get("name"), # added name and email
"email": request.form.get("email")
}
mongo.db.users.insert_one(register)
session["user"] = request.form.get("username").lower() # add cookie
session["name"] = request.form.get("name").title()
flash("Registration Successful")
return redirect(url_for("user_profile", username=session["user"]))
return render_template("register.html")
@app.route("/user_profile")
def user_profile():
"""
Function to show all recipes created by logged in user
"""
user = session.get("user").lower()
if user is not None:
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
total = mongo.db.recipies.find({'created_by': user}).count()
recipe = list(mongo.db.recipies.find({'created_by': user}))
recipe_paginated = recipe[offset: offset + per_page]
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
return render_template('user_profile.html',
recipes=recipe_paginated,
page=page,
per_page=per_page,
pagination=pagination)
else:
return render_template("403.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Function to handle logins used and customized from walkthrough project
in the Code Institute Backend module
"""
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:
if check_password_hash( # ensure hash matches user input
existing_user["password"],
request.form.get("password")):
session["user"] = request.form.get("username").lower()
session["name"] = existing_user["name"]
flash("Welcome, {}!".format(request.form.get("username")))
return redirect(url_for("user_profile",
username=session["user"]))
else: # invalid password match
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
else: # username doesn't exist
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
return render_template("login.html")
@app.route("/logout")
def logout():
"""
logout method used from CI walkthrough project
"""
flash("Successfully logged out, Goodbye!")
session.pop("user") # Removes user from cookie
return redirect(url_for("login"))
@app.route("/search", methods=["GET"])
def search():
"""
Search function, takes the string used to search
and returns all matches from the db.
modified from CI walkthrough project.
"""
query = request.args.get("query")
recipes = list(mongo.db.recipies.find({"$text": {"$search": query}}))
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
recipe_paginated = recipes[offset: offset + per_page]
total = mongo.db.recipies.find({"$text": {"$search": query}}).count()
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
if len(recipes) == 0:
flash(f"We're sorry but no recipes with {query} were found!")
else:
flash(f"Your search for {query} returned {len(recipes)} result(s)!")
return render_template('recipes.html',
recipes=recipe_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route("/cook_time", methods=["GET"])
def by_cook_time():
"""
Sort all products by cooking time ascending
"""
recipes = list(mongo.db.recipies.find().sort("cook_time", 1))
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
recipe_paginated = recipes[offset: offset + per_page]
total = mongo.db.recipies.find().sort("cook_time", 1).count()
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
flash("Showing results by cooking time ascending!")
return render_template('recipes.html',
recipes=recipe_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route('/nationality_filter/<id>')
def nationality_filter(id):
"""
Function to take the id variable and return
paginated recipes which included that id
"""
recipes = list(mongo.db.recipies.find({"nationality": id}))
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
recipe_paginated = recipes[offset: offset + per_page]
total = len(recipes)
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
flash(f"Showing results for {id.title()}")
return render_template('recipes.html',
recipes=recipe_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route('/utensil_filter/<id>')
def utensil_filter(id):
"""
Function to take the id variable and return
paginated products which included that id
"""
products = list(mongo.db.products.find({"type": id}))
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
total = mongo.db.products.find({"type": "pot"}).count()
product_paginated = products[offset: offset + per_page]
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
flash(f"Showing results for {id.title()}")
return render_template('range.html',
products=product_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.route("/rating", methods=["GET"])
def by_rating():
"""
Function to filter by product rating in descending order
"""
products = list(mongo.db.products.find().sort("rating", -1))
page, per_page, offset = get_page_args(
page_parameter='page', per_page_parameter='per_page',
offset_parameter='offset')
per_page = 3
offset = (page - 1) * 3 # My formula for getting the right offset
total = mongo.db.products.find().count()
product_paginated = products[offset: offset + per_page]
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='materializecss')
flash("Showing highest rated products first")
return render_template('range.html',
products=product_paginated,
page=page,
per_page=per_page,
pagination=pagination,
)
@app.errorhandler(404)
def page_not_found(e):
"""
Error handler 404 from flask documentation
"""
return render_template('404.html'), 404
@app.errorhandler(403)
def access_denied(e):
"""
Error handler 403 from flask documentation
"""
return render_template('403.html'), 403
@app.errorhandler(500)
def internal_server_error(e):
"""
Error handler 500 from flask documentation
"""
return render_template('500.html'), 500
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)