-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
285 lines (228 loc) · 9.07 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
import os
import requests
from flask import Flask, session, render_template, redirect, url_for, request, jsonify, abort
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from werkzeug.security import check_password_hash, generate_password_hash
from functools import wraps
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.12/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if not session.get("user_id"):
return redirect(url_for("login"))
return f(*args, **kwargs)
return decorated_function
@app.route("/")
@login_required
def index():
firstname = session["username"].split(' ', 1)[0]
return render_template("index.html", username=session["username"], firstname=firstname)
@app.route("/login", methods=["GET", "POST"])
def login():
# if login form is submitted
if request.method == "POST":
# GET INPUTS
username = request.form.get("username")
password = request.form.get("password")
# TEST INPUTS
# if not username or not password:
# return "One or more field is missing! Please try again"
# (checked on frontend)
user_exists = db.execute("SELECT * FROM users WHERE username=:username", {"username":username}).rowcount
if not user_exists:
# return "username doesn't exist!"
return render_template("login.html", not_user=True)
user_info = db.execute("SELECT * FROM users WHERE username=:username", {"username":username}).fetchone()
if not check_password_hash(user_info.password, password):
# return "password is not correct!"
return render_template("login.html", wrong_password=True)
# COOKIE USER
session["user_id"] = user_info.id
session["username"] = user_info.username
return redirect(url_for("index"))
# if login form is requested
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/register", methods=["GET", "POST"])
def register():
# if registration form is submitted
if request.method == "POST":
# GET INPUTS
username = request.form.get("username")
email = request.form.get("email")
password = request.form.get("password")
password2 = request.form.get("password2")
# TEST INPUTS
# check all values are provided
# if not username or not email or not password or not password2:
# return "One or more field is missing! Please try again"
# # check passwords match
# if password != password2:
# return "Passwords don't match! Please try again"
# (checked on frontend)
# check username and email are unique
username_repeated = db.execute("SELECT * FROM users WHERE username = :username", {"username": username}).rowcount
email_repeated = db.execute("SELECT * FROM users WHERE email = :email", {"email": email}).rowcount
if username_repeated:
# return "username already exists!"
return render_template("register.html", username_repeated=True)
if email_repeated:
# return "email already exists!"
return render_template("register.html", email_repeated=True)
# HASH PASSWORD
password = generate_password_hash(password)
# ADD USER
try:
db.execute("""INSERT INTO users (username, email, password)
VALUES (:username, :email, :password)""",
{"username":username, "email":email, "password":password})
db.commit()
except:
return "Failure"
# COOKIE USER
user_info = db.execute("SELECT * FROM users WHERE username=:username", {"username":username}).fetchone()
session["user_id"] = user_info.id
session["username"] = user_info.username
return redirect(url_for("index"))
# if registration form is requested
else:
return render_template("register.html")
@app.route("/search")
@login_required
def search():
# get search text
q1 = request.args.get("q")
# if not q1:
# return "Search field is empty!"
# (checked on frontend)
# make it partial
q2 = f"%{q1}%"
# query database enabling case-insensitivity
# concatenate columns with separator to prevent results of words between two columns
book_list = db.execute("""SELECT * FROM books
WHERE CONCAT_WS(' ', LOWER(isbn), LOWER(title), LOWER(author), year::text) LIKE LOWER(:q2)""", {"q2":q2}).fetchall()
# also see COLLATE utf8_general_ci to fix case-insensitivity
return render_template("index.html", q1=q1, book_list=book_list, username=session["username"])
@app.route("/clicksearch")
@login_required
def clicksearch():
"""for search on click, so that a clicked on year doesn't yield isbn results"""
# get search text
q1 = request.args.get("q")
# make it partial
q2 = f"%{q1}%"
# year clicked
if len(q1) == 4:
try:
# confirm it is an integer
is_it_integer = int(q1)
book_list = db.execute("""SELECT * FROM books
WHERE year::text = :q1""", {"q1":q1}).fetchall()
except: pass
# author clicked
else:
book_list = db.execute("""SELECT * FROM books
WHERE LOWER(author) LIKE LOWER(:q2)""", {"q2":q2}).fetchall()
return render_template("index.html", q1=q1, book_list=book_list, username=session["username"])
@app.route("/reviews")
@login_required
def reviews():
# get current user reviews from my DB
user_reviews = db.execute("SELECT isbn, title, author, year FROM reviews, users, books WHERE users.id = reviews.user_id AND books.id = reviews.book_id AND username=:username", {"username": session["username"]}).fetchall()
return render_template("index.html", reviews_query=True, user_reviews=user_reviews[::-1], username=session["username"])
@app.route("/book/<string:isbn>", methods=["GET", "POST"])
@login_required
def book(isbn):
# get book's basic info from my DB
book_info = db.execute("SELECT * FROM books WHERE isbn=:isbn", {"isbn":isbn}).fetchone()
if request.method == "GET":
# get book's review info from goodreads
response = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "yLM7LLyUkFTyeElwIrzUDA", "isbns": isbn})
goodreads_info = response.json()
# get book's review info from my DB
reviews = db.execute("SELECT rating, opinion, username FROM reviews JOIN users ON users.id = reviews.user_id WHERE book_id=:book_id", {"book_id": book_info.id}).fetchall()
# check if current user submitted a review
user_submitted_review = False
for review in reviews:
if review.username == session["username"]:
user_submitted_review = True
return render_template("book.html", book_info=book_info, goodreads_info=goodreads_info, reviews=reviews, user_submitted_review=user_submitted_review, username=session["username"])
else:
# get review data
opinion = request.form.get("opinion")
rating = request.form.get("rating")
# get around the 'selected' rating option value
# if rating not in ('1','2','3','4','5'): rating = None
# fixed with value="" attribute
# check data exist
# if not opinion or not rating:
# return "opinion or rating is missing!"
# (checked on frontend)
# ???? opinion shouldn't be a must, update DB
# append <br> to \n so that new lines are translated
opinion = opinion.replace('\n','<br>\n')
# nl2br. see https://stackoverflow.com/a/30593254
# unescape html. see http://flask.pocoo.org/docs/1.0/quickstart/#rendering-templates
# ADD REVIEW
try:
db.execute("""INSERT INTO reviews (book_id, user_id, rating, opinion)
VALUES (:book_id, :user_id, :rating, :opinion)""",
{"book_id": book_info.id, "user_id": session["user_id"], "rating": rating, "opinion": opinion})
db.commit()
except:
return "Failure"
return redirect(f"/book/{isbn}")
@app.route("/api")
def api_info():
return render_template("api.html", username=session["username"])
@app.route("/api/<string:isbn>")
def api(isbn):
# if isbn in DB
try:
# get book's basic info from my DB
book_info = db.execute("SELECT * FROM books WHERE isbn=:isbn", {"isbn":isbn}).fetchone()
# get ratings count
review_count = db.execute("SELECT COUNT(rating) FROM reviews JOIN books ON books.id=reviews.book_id WHERE isbn=:isbn", {"isbn":isbn}).scalar()
# get ratings average
review_avg = db.execute("SELECT AVG(rating) FROM reviews JOIN books ON books.id=reviews.book_id WHERE isbn=:isbn", {"isbn":isbn}).scalar()
# scalar() fetches first col of first row = fetchall()[0]
# if no reviews, set avg to 0 instead of None
if not review_avg:
review_avg = 0
# return f"{review_count}, {review_avg}" # testing
return jsonify (
title=book_info.title,
author=book_info.author,
year=book_info.year,
isbn=isbn,
review_count=int(review_count),
average_score=float(review_avg)
) # jsonify takes a dict or any json serializable
# if isbn not in DB
except:
abort(404)
if __name__=="__main__":
app.run()