-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
329 lines (252 loc) · 10.7 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
import requests
import os
from flask import Flask, render_template, request, redirect, url_for, flash, session, abort, jsonify, g, get_flashed_messages
from flask_wtf import FlaskForm
from flask_login import LoginManager
from flask_login import login_required, current_user, logout_user, login_user
from forms import RegisterForm, LoginForm, EditUserForm, AddFavoriteCharacterForm
from models import db, connect_db, User, FavoriteCharacter
from flask_bcrypt import Bcrypt
from functools import wraps
app = Flask(__name__)
DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql:///disney')
if DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql+psycopg2://", 1)
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'topsecret')
bcrypt = Bcrypt(app)
# Configure the LoginManager part of the app instance
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
connect_db(app)
# Function to reload the user object from the user ID stored in the session
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
BASE_URL = "https://api.disneyapi.dev"
# Routes
# Main route #############################################################################
@app.route("/")
def index():
form = FlaskForm()
if current_user.is_authenticated:
return render_template("index.html", form=form)
else:
return render_template("landing_page.html", form=form)
@app.route("/guest")
def guest():
form = FlaskForm()
return render_template("index.html", form=form)
# User Routes #############################################################################
@app.route('/users/<string:username>', methods=['GET', 'POST'])
def user_profile(username):
form = FlaskForm()
# Check if the current user is authenticated and the username matches
if not current_user.is_authenticated or current_user.username != username:
abort(403)
user = User.query.filter_by(username=username).first_or_404()
# Get user's favorite characters
favorite_characters = FavoriteCharacter.query.filter_by(user_id=user.id).all()
edit_form = EditUserForm(obj=user)
if edit_form.validate_on_submit():
user.username = edit_form.username.data
user.email = edit_form.email.data
db.session.commit()
session['username'] = user.username
return redirect(url_for("user_profile", username=user.username))
return render_template("user_profile.html", edit_form=edit_form, user=user, favorite_characters=favorite_characters, form=form)
# Delete user route
@app.route("/users/delete/<string:username>", methods=["POST"])
def delete_user(username):
# Ensure the current user is authenticated and the username matches
if not current_user.is_authenticated or current_user.username != username:
abort(403)
user = User.query.filter_by(username=username).first()
if user is None:
abort(404)
# Deleting favorite characters of the user before deleting the user
for character in user.favorite_characters:
db.session.delete(character)
db.session.delete(user)
db.session.commit()
logout_user()
flash("Your account has been deleted.", "success")
return redirect(url_for("index"))
# Registration route
@app.route('/register', methods=['GET', 'POST'])
def register():
"""Register a user: produce form and handle form submission."""
if current_user.is_authenticated:
return redirect(f"/users/{current_user.username}")
form = RegisterForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
email = form.email.data
# Hash the password
hashed_password = bcrypt.generate_password_hash(password).decode("utf-8")
# Create a new user
user = User(username=username, password=hashed_password, email=email)
# Add the user to the database
db.session.add(user)
db.session.commit()
session['username'] = user.username
return redirect(url_for("index"))
else:
return render_template("register.html", form=form)
# Login route
@app.route('/login', methods=['GET', 'POST'])
def login():
"""Produce login form or handle login."""
if current_user.is_authenticated:
return redirect(url_for("index"))
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:
login_user(user) # This sets up the user session
return redirect(url_for("index"))
else:
form.username.errors = ["Invalid username/password."]
return render_template("login.html", form=form)
return render_template("login.html", form=form)
# Logout route
@app.route("/logout")
def logout():
"""Logout route."""
logout_user()
return redirect("/login")
# Character Routes ############################################################################
# Error handling function for API calls
def handle_api_errors(api_func):
@wraps(api_func)
def wrapper(*args, **kwargs):
try:
return api_func(*args, **kwargs)
except requests.exceptions.JSONDecodeError:
flash("There was an issue retrieving data from the API. Please try again later.", "error")
return redirect(url_for("index"))
return wrapper
@app.route("/characters")
@handle_api_errors
def all_characters():
page = request.args.get("page", 1)
response = requests.get(f"{BASE_URL}/character?page={page}")
data = response.json()
return render_template("characters.html", data=data)
# Route for individual character
@app.route("/character/<int:character_id>")
@handle_api_errors
def single_character(character_id):
response = requests.get(f"{BASE_URL}/character/{character_id}")
data = response.json()
form = AddFavoriteCharacterForm()
if current_user.is_authenticated:
favorite_characters = [str(char.character_id) for char in current_user.favorite_characters]
is_favorite = any(fav.character_id == character_id for fav in current_user.favorite_characters)
else:
favorite_characters = []
is_favorite = False
return render_template("character.html", data=data["data"], form=form, is_favorite=is_favorite, favorite_characters=",".join(favorite_characters))
# Route for filtering character by name
@app.route("/filter-character")
@handle_api_errors
def filter_character():
name = request.args.get("name")
response = requests.get(f"{BASE_URL}/character?name={name}")
data = response.json()
return render_template("characters.html", data=data)
# Route for adding character to favorites
@app.route('/favorite_character/<character_id>', methods=['POST'])
@login_required
def favorite_character(character_id):
# Fetch character details from the API
response = requests.get(f"{BASE_URL}/character/{character_id}")
if response.status_code == 200:
data = response.json()
character_name = data["data"]["name"]
character_image_url = data["data"]["imageUrl"]
else:
character_name = "Unknown Character"
character_image_url = "image.jpg"
favorite = FavoriteCharacter.query.filter_by(user_id=current_user.id, character_id=character_id).first()
if favorite:
db.session.delete(favorite)
db.session.commit()
return jsonify({"status": "unfavorited"})
else:
new_favorite = FavoriteCharacter(
user_id=current_user.id,
character_id=character_id,
name=character_name,
image_url=character_image_url
)
db.session.add(new_favorite)
db.session.commit()
return jsonify({"status": "favorited"})
# Search route ############################################################################
@app.route('/search', methods=['GET'])
def search_characters():
form = FlaskForm()
query = request.args.get('query')
if not query:
return redirect(url_for('index'))
query_tokens = query.split(' ')
characters = []
for token in query_tokens:
encoded_token = requests.utils.quote(token)
# Search by character name
url = f"https://api.disneyapi.dev/character?name={encoded_token}"
response = requests.get(url)
data = response.json()
characters += data.get('data', [])
# Search by TV shows
if not characters:
url = f"https://api.disneyapi.dev/character?tvShows={encoded_token}"
response = requests.get(url)
data = response.json()
characters += data.get('data', [])
# Search by park attractions
if not characters:
url = f"https://api.disneyapi.dev/character?parkAttractions={encoded_token}"
response = requests.get(url)
data = response.json()
characters += data.get('data', [])
# Search by video games
if not characters:
url = f"https://api.disneyapi.dev/character?videoGames={encoded_token}"
response = requests.get(url)
data = response.json()
characters += data.get('data', [])
# Search by films
if not characters:
url = f"https://api.disneyapi.dev/character?films={encoded_token}"
response = requests.get(url)
data = response.json()
characters += data.get('data', [])
# Filter out non-dictionary items
characters = [character for character in characters if isinstance(character, dict)]
# Remove duplicate characters from the list
characters = list({character['_id']: character for character in characters}.values())
return render_template('search_results.html', characters=characters, form=form)
# 404 error handling ########################################################################
@app.errorhandler(404)
def page_not_found(e):
form = FlaskForm()
"""404 NOT FOUND page."""
return render_template('404.html', form=form), 404
# Adding non-caching headers on every request for development purposes
@app.after_request
def add_header(req):
"""Add non-caching headers on every request."""
req.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
req.headers["Pragma"] = "no-cache"
req.headers["Expires"] = "0"
req.headers['Cache-Control'] = 'public, max-age=0'
return req