-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
277 lines (236 loc) · 8.84 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
from flask import Flask, render_template, redirect, session, flash, url_for, request
from flask_debugtoolbar import DebugToolbarExtension
from models import connect_db, db, bcrypt, User, Recipe
from forms import SignUpForm, LogInForm, DeleteForm
from werkzeug.exceptions import Unauthorized
from sqlalchemy.exc import IntegrityError
import requests
import json
import os
import re
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'postgres:///meal_db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY", "secreto001")
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
connect_db(app)
toolbar = DebugToolbarExtension(app)
url = ""
CATEGORY_URL="https://www.themealdb.com/api/json/v1/1/filter.php?c="
MAIN_IN_URL = "https://www.themealdb.com/api/json/v1/1/filter.php?i="
NAME_URL ="https://www.themealdb.com/api/json/v1/1/search.php?s="
KEY = "1"
@app.route("/")
def homepage():
"""Homepage of site; or redirect to login."""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
try:
res = requests.get("https://www.themealdb.com/api/json/v1/1/categories.php", params={'key': KEY})
except:
return render_template("404.html")
d = res.json()
categories = d["categories"]
meals = []
url = "https://www.themealdb.com/api/json/v1/1/filter.php?c="
for category in categories:
categoryName =category['strCategory']
try:
res = requests.get(f"{url}{categoryName}", params={'key': KEY})
except:
flash("Something went wrong while requesting data to Api. Try again later?")
return render_template("404.html")
data = res.json()
m = data["meals"]
meals = meals + m
return render_template("meals.html", meals=meals)
@app.route("/help")
def user_help():
"""route where users can view all categories and can get help navigating the app"""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
try:
res = requests.get("https://www.themealdb.com/api/json/v1/1/categories.php", params={'key': KEY})
except:
return render_template("404.html")
data = res.json()
categories = data["categories"]
return render_template("help.html", categories=categories)
@app.route('/find-meals', methods=["POST"])
def get_meals():
"""submitting form to find meals based on search input."""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
search_by = request.form["search-by"]
search_term = request.form["search-term"]
if search_by == "category":
url = CATEGORY_URL
if search_by == "mainIngridient":
url = MAIN_IN_URL
if search_by == "name":
url = NAME_URL
try:
res = requests.get(f"{url}{search_term}", params={'key': KEY})
except:
return render_template("404.html")
data = res.json()
meals = data["meals"]
if meals == None:
flash(f"Searching by {search_by} NO MATCHES for {search_term}", "warning")
return render_template("makeMeal.html")
flash(f"Searching {search_by} of {search_term}", "success")
return render_template("searchMeals.html", meals=meals)
@app.route("/find-meals/<category>")
def get_category_meals(category):
"""Get all meals by category"""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
url = "https://www.themealdb.com/api/json/v1/1/filter.php?c="
try:
res = requests.get(f"{url}{category}", params={'key': KEY})
except:
return render_template("404.html")
data = res.json()
meals = data["meals"]
return render_template("category.html", meals=meals)
@app.route("/find-meals/<int:id>")
def get_meal(id):
"""render a single meal by id"""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i="
try:
res = requests.get(f"{url}{id}", params={'key': KEY})
except:
return render_template("404.html")
data = res.json()
m = data["meals"]
meal = m[0]
return render_template("meal.html", meal=meal)
@app.route("/my-recipes", methods=["POST"])
def add_recipe():
"""save recipe to users recipes"""
if "user_id" not in session:
flash("Login First or sign for an ccount!","primary")
return redirect("/login")
user_id = session["user_id"]
recipe_id = request.form["recipe"]
r = Recipe.query.filter(Recipe.user_id==user_id, Recipe.recipe_id==recipe_id).first()
if r is not None:
flash("Already in your recipes", "warning")
return redirect("/")
try:
recipe = Recipe(
recipe_id = recipe_id,
user_id = user_id
)
db.session.add(recipe)
db.session.commit()
except IntegrityError:
flash("Already in your Recipes!")
my_recipes = []
recipes = Recipe.query.filter(Recipe.user_id==session["user_id"]).all()
for recipe in recipes:
url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i="
try:
res = requests.get(f"{url}{recipe.recipe_id}", params={'key': KEY})
except:
flash("That recipe is no longer available", "warning")
data = res.json()
my_recipe = data["meals"]
r = my_recipe[0]
my_recipes.append(r)
return render_template("recipes.html", my_recipes=my_recipes)
@app.route("/my-recipes")
def get_user_recipes():
"""get all user recipes"""
if "user_id" not in session:
flash("Login or Signup first", "primary")
return redirect("/login")
my_recipes = []
recipes = Recipe.query.filter(Recipe.user_id==session["user_id"]).all()
for recipe in recipes:
url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i="
try:
res = requests.get(f"{url}{recipe.recipe_id}", params={'key': KEY})
except:
flash("That recipe is no longer available", "warning")
data = res.json()
recipe = data["meals"]
r = recipe[0]
my_recipes.append(r)
return render_template("recipes.html", my_recipes=my_recipes)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
"""Sign UP a user: produce form and handle form submission."""
if "user_id" in session:
flash("You already have an account", "danger")
return redirect("/")
form = SignUpForm()
if form.validate_on_submit():
try:
user = User.signup(
username=form.username.data,
password=form.password.data,
image_url=form.image_url.data
)
db.session.add(user)
db.session.commit()
except IntegrityError:
flash("Username already taken", 'danger')
return render_template('signup.html', form=form)
session["user_id"] = user.id
return redirect("/")
else:
return render_template("signup.html", form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
"""Produce login form or handle login."""
if "user_id" in session:
flash("You are already logged in!", "danger")
return redirect("/")
form = LogInForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
user = User.authenticate(username, password) # <User> or False
if user:
session["user_id"] = user.id
return redirect("/")
else:
form.username.errors = ["Invalid username/password."]
return render_template("login.html", form=form)
return render_template("login.html", form=form)
@app.route("/logout")
def logout():
"""Logout, pop user_id from session"""
if "user_id" not in session:
raise Unauthorized()
session.pop("user_id")
flash("See you later!", "primary")
return redirect("/login")
@app.route("/users/<int:id>")
def show_user(id):
"""User profile"""
if "user_id" not in session or id != session["user_id"]:
raise Unauthorized()
user = User.query.get(id)
form = DeleteForm()
return render_template("user.html", user=user, form=form)
@app.route("/users/<int:id>/delete", methods=["POST"])
def remove_user(id):
"""Remove user and redirect to login."""
if "user_id" not in session or id != session["user_id"]:
raise Unauthorized()
user = User.query.get(id)
db.session.delete(user)
db.session.commit()
session.pop("user_id")
flash("Sad to see you go", "warning")
return redirect("/login")