-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
265 lines (198 loc) · 7.22 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
from flask import Flask, request, render_template, redirect, url_for, g, jsonify, flash, abort, session
from models import db, connect_db, User, Recipe, Ingredient, Step, UserRecipe, Measurement
from sqlalchemy.exc import IntegrityError
from forms import SignUpForm, LoginForm
from flask_debugtoolbar import DebugToolbarExtension
from helpers import add_user_data, create_login_data, generate_headers, generate_search_params, add_and_commit, do_search, get_recipe, do_login, do_logout, add_ingredients_to_db, add_recipe_to_db, add_measurement_for_ingredient, API_BASE_URL, valid_cuisines, valid_diets
from secrets import api_key
import requests
import os
app = Flask(__name__)
if __name__ == '__main__':
app.run()
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL','postgresql:///meal_planner')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', "it's a secret")
toolbar = DebugToolbarExtension(app)
connect_db(app)
db.create_all()
CURRENT_USER_KEY = "user_id"
API_BASE_URL = "https://api.spoonacular.com"
# API_BASE_URL = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com"
API_KEY = api_key
@app.before_request
def add_user_to_g():
"""Add current user"""
if CURRENT_USER_KEY in session:
g.user = User.query.get(session[CURRENT_USER_KEY])
else:
g.user = None
g.valid_diets = [diet for diet in valid_diets]
g.valid_cuisines = [cuisines for cuisines in valid_cuisines]
@app.context_processor
def context_processor():
"""Global context jinja templates"""
return dict(diets=valid_diets, cuisines=valid_cuisines)
###User Signup###
@app.route('/signup', methods=["GET", "POST"])
def signup():
""" User Signup """
form = SignUpForm()
if form.validate_on_submit():
user_data = add_user_data(form)
try:
user = User.signup(user_data)
add_and_commit(user)
db.session.commit()
except IntegrityError as error:
db.session.rollback()
if (f'(username)=({user.username}) already exists') in error._message():
flash("username already taken", 'danger')
elif (f'(email)=({user.email}) already exists') in error._message():
flash("Email already taken", 'danger')
return render_template('users/signup.html', form=form)
do_login(user)
return redirect("/")
else:
return render_template('users/signup.html', form=form)
### User Login###
@ app.route('/login', methods=["GET", "POST"])
def login():
""" Handle user login."""
form = LoginForm()
if form.validate_on_submit():
# user = User.authenticate(data)
data = create_login_data(form)
user = User.authenticate(data)
if user:
do_login(user)
flash(f"Hello, {user.username}!", "success")
return redirect("/")
flash("Invalid Credentials.", 'danger')
return render_template('users/login.html', form=form)
@ app.route('/logout')
def logout():
"""Handle logout of user."""
do_logout()
flash('You have been logged out', 'success')
return redirect(url_for('home_page'))
####Search Routes####
@app.route('/')
def home_page():
""" Home Page """
return render_template('home.html')
@ app.route('/load')
def load():
"""Load more results after the end of the page"""
if request.args:
response = do_search(request)
data = response.json()
if len(data['results']) == 0:
return (jsonify(data=data), 200)
user_favorites = [f.id for f in g.user.recipes]
favorites = [r['id']for r in data['results'] if r['id'] in user_favorites]
response_json = jsonify(data=data, favorites=favorites)
return (response_json, 200)
@ app.route('/search')
def search_recipes():
""" Recipe Serach. """
if not g.user:
return abort(401)
response = do_search(request)
data = response.json()
if len(data['results']) == 0:
return (jsonify(data=data), 200)
user_favorites = [f.id for f in g.user.recipes]
favorites = [r['id'] for r in data['results'] if r['id'] in user_favorites]
response_json = jsonify(data=data, favorites=favorites)
return (response_json, 200)
#### User Routes####
@ app.route('/users/<int:id>')
def view_user(id):
""" Display user """
if not g.user:
flash('You must log in first', 'warning')
return redirect(url_for('login'))
return render_template('users/profile.html')
@ app.route('/users/<int:id>', methods=['PATCH'])
def update_user(id):
""" Update user info """
if not g.user:
return abort(401)
if request.json['id'] != id:
return jsonify(errors="You don't have permission to do that!")
try:
user = User.query.get_or_404(id)
new_email = request.json.get('email', user.email)
new_img_url = request.json.get('imgUrl', user.img_url)
if new_email:
user.email = new_email
if new_img_url:
user.img_url = new_img_url
db.session.commit()
response_json = jsonify(user=user.serialize(),
message="Update successful!")
return (response_json, 200)
except Exception as e:
return jsonify(errors=str(e))
@ app.route('/favorites/')
def view_saved_recipes():
""" Route to view saved recipes """
if not g.user:
flash('You must be logged in to do that', 'warning')
return redirect(url_for('login'))
id_list = [recipe.id for recipe in g.user.recipes]
return render_template('users/favorites.html', id_list=id_list)
@app.route('/favorites/<int:id>', methods=['POST'])
def add_favorite(id):
"""Add recipe to favorites"""
if not g.user:
return abort(401)
recipe = Recipe.query.filter_by(id=id).first()
if not recipe:
response = get_recipe(id)
data = response.json()
recipe = add_recipe_to_db(data)
g.user.recipes.append(recipe)
db.session.commit()
else:
g.user.recipes.append(recipe)
db.session.commit()
response_json = jsonify(
recipe=recipe.serialize(), message="Congrats Recipe added successfully!")
return (response_json, 200)
@app.route('/favorites/<int:id>', methods=['DELETE'])
def remove_favorite(id):
"""Remove recipe from favorites"""
if not g.user:
return abort(401)
try:
recipe = Recipe.query.filter_by(id=id).first()
UserRecipe.query.filter(
UserRecipe.user_id == g.user.id, UserRecipe.recipe_id == recipe.id).delete()
db.session.commit()
response_json = jsonify(recipe=recipe.serialize(), message="Recipe removed!")
return (response_json, 200)
except Exception as e:
print(str(e))
return jsonify(errors=str(e))
@app.route('/recipes/<int:id>')
def view_recipe_details(id):
""" View recipe in detail """
if not g.user:
flash('You must be logged in to do that', 'warning')
return redirect(url_for('login'))
recipe = Recipe.query.filter_by(id=id).first()
if not recipe:
response = get_recipe(id)
data = response.json()
recipe = add_recipe_to_db(data)
return render_template('recipes/details.html', recipe=recipe)
else:
return render_template('recipes/details.html', recipe=recipe)
@app.errorhandler(404)
def page_not_found(error):
"""404 NOT FOUND PAGE."""
return render_template('errors/404.html'), 404