-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
281 lines (202 loc) · 8.71 KB
/
application.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
# pylint: disable=E1101
import os
import requests
from flask import Flask, flash, jsonify, redirect, render_template, request, session, url_for
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from functions import login_required
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
# 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))
@app.route("/")
def index():
"""Render landing page."""
return render_template("index.html")
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""
# when user signs up using the form
if request.method == "POST":
# ensure passwords match
if request.form.get("password") != request.form.get("password_check"):
flash("Passwords do not match. 😕")
return redirect(url_for("register"))
# hash user password
hash = pwd_context.hash(request.form.get("password"))
# add user to db & check if unique
try:
db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", {"username": request.form.get("username").lower(), "hash": hash})
except:
flash("Username is taken. 😕")
return redirect(url_for("register"))
db.commit()
# when register succeeds
flash("Successfully registered! 👍 You can now sign in.")
return redirect(url_for("login"))
# when user reaches register via GET
else:
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""Sign in user."""
if len(session) > 1:
session.clear()
if request.method == "POST":
# query database
user = db.execute("SELECT * FROM users WHERE username = :username", {"username" : request.form.get("username").lower()}).fetchone()
# verify user-password from db
if user is None or not pwd_context.verify(request.form.get("password"), user["hash"]):
flash("Invalid username and/or password. ☹️")
return redirect(url_for("login"))
# remember which user is logged in
session["user_id"] = user["id"]
session["user"] = user["username"]
# query db for book reviewed by user
# add it to session["reviewed"] for easy access
reviewed = db.execute("SELECT library.id, library.title, library.year FROM reviews JOIN library ON library.id = reviews.book_id WHERE user_id = :id ORDER BY date_posted DESC", {"id" : session["user_id"]}).fetchall()
if len(reviewed) == 0:
pass
else:
for book in reviewed:
session.setdefault("reviews",[]).append((book.id, book.title, book.year))
# redirect to search page
return redirect(url_for("search"))
# when user reaches login via GET
else:
return render_template("login.html")
@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
"""Visit user profile page."""
# when user submits form to change password
if request.method == "POST":
# query database
user = db.execute("SELECT * FROM users WHERE username = :username", {"username" : session["user"]}).fetchone()
# verify user-password from db
if not pwd_context.verify(request.form.get("oldPass"), user["hash"]) or request.form.get("newPass") != request.form.get("checkPass"):
flash("❌ Mismatch found. Please fill up the form correctly.")
return redirect(url_for("search"))
# hash user password
hash = pwd_context.hash(request.form.get("newPass"))
# change old password
db.execute("UPDATE users SET hash = :hash WHERE username = :username", {"hash" : hash, "username" : session["user"]})
db.commit()
flash("✅ Password successfully updated!")
return redirect(url_for("search"))
else:
return redirect(url_for("search"))
@app.route("/search", methods=["GET", "POST"])
@login_required
def search():
"""Search library for books."""
# if user submits form
if request.args.get("category") and request.args.get("q"):
category = request.args.get("category")
search = request.args.get("q")
rough_search = f'%{search}%'
#query database
books = db.execute("SELECT * FROM library WHERE "f'{category}'" ILIKE :rough_search ORDER BY author ASC", {"rough_search" : rough_search}).fetchall()
# if search returns empty
if len(books) == 0:
flash("My magnifying glass broke but still couldn't find anything. 🤔")
return redirect(url_for("search"))
# return books to search page
return render_template("search.html", books = books, number = len(books))
# if user reaches page via GET
else:
return render_template("search.html")
@app.route("/book/<int:book_id>", methods=["GET", "POST"])
@login_required
def book(book_id):
"""Show individual book pages."""
# find book in library db
book = db.execute("SELECT * FROM library WHERE id = :id", {"id" : book_id}).fetchone()
# if user submits a review via the form
if request.method == "POST":
# get review details
rating = request.form.get("star")
comment = request.form.get("comment")
# verify if user has made a review for this book already
check = db.execute("SELECT * from reviews WHERE user_id = :user_id AND book_id = :book_id", {"user_id" : session["user_id"], "book_id" : book_id}).fetchone()
if check:
flash("Sorry. You have reviewed this book already. 💔")
return redirect(url_for("book",book_id=book_id))
# add review to database
db.execute("INSERT INTO reviews (rating, review_text, user_id, book_id) VALUES (:rating, :review_text, :user_id, :book_id)", {"rating" : rating, "review_text" : comment, "user_id" : session["user_id"], "book_id" : book_id})
db.commit()
# add book to session["reviewed"]
book = (book_id, book.title, book.year)
session.setdefault("reviews",[]).append(book)
flash("Awesome! Your review has been added. ❤️")
return redirect(url_for("book", book_id=book_id))
# user reaches route via GET
else:
if book is None or len(book) == 0:
return redirect(url_for("search"))
# get Goodreads data
res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": os.getenv("GOODREADS_KEY"), "isbns": book.isbn})
# Goodreads API data
goodreads = res.json()['books'][0]
avg_rating = goodreads['average_rating']
rev_count = goodreads['work_ratings_count']
# get plot & thumbnail from Google Books API
req = "https://www.googleapis.com/books/v1/volumes?q=title+inauthor:byauthor"
req = req.replace("title", book.title)
req = req.replace("byauthor", book.author)
googleAPI = requests.get(req).json()["items"][0]["volumeInfo"]
plot = googleAPI["description"]
thumbnail = ""
try:
thumbnail = googleAPI["imageLinks"]["thumbnail"].replace("zoom=1", "zoom=3")
except:
thumbnail = "http://covers.openlibrary.org/b/isbn/"f'{book.isbn}'"-L.jpg"
# query back BooksDB API to get review scores
booksDB = requests.get("https://books4cs50w.herokuapp.com/api/"f'{book.isbn}'"").json()
ave_score = booksDB["average_score"]
ave_count = int(booksDB["review_count"])
# get user reviews from db
reviews = db.execute("SELECT username, date_posted, rating, review_text FROM reviews JOIN users ON users.id = reviews.user_id WHERE book_id = :book_id ORDER BY date_posted DESC", {"book_id" : book_id}).fetchall()
# track user browsing history
if session.get("history") is None:
session.setdefault("history",[]).append({book.title : book.id})
elif {book.title : book.id} in session["history"]:
pass
else:
session.setdefault("history",[]).append({book.title : book.id})
return render_template("book.html", book=book, rating=avg_rating, count=rev_count, plot=plot, thumbnail=thumbnail, reviews=reviews, ave_score = ave_score, ave_count = ave_count)
@app.route("/logout")
def logout():
"""Log user out."""
# forget any user_id
session.clear()
# redirect user to login form
return redirect(url_for("index"))
@app.route("/api/<isbn>", methods=["GET"])
def api(isbn):
"""Provide access to API."""
# query database for book using isbn
book = db.execute("SELECT * FROM library WHERE isbn = :isbn", {"isbn" : isbn}).fetchone()
# return 404 eror if book can't be found in db
if book is None:
return jsonify({"error": "Invalid ISBN"}), 404
# query db for review count and rating
booksdb = db.execute("SELECT COUNT(*), ROUND(AVG(rating),2) FROM reviews WHERE book_id = :book_id", {"book_id" : book.id}).fetchone()
return jsonify({
"title": book.title,
"author": book.author,
"year": book.year,
"isbn": book.isbn,
"review_count": f'{booksdb[0]}',
"average_score": "0" if booksdb[1] is None else f'{booksdb[1]}'
})