-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
336 lines (290 loc) · 13.4 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
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
328
329
330
331
332
333
334
335
336
# import os
from flask import Flask, render_template, request, session, flash, redirect, url_for, jsonify
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from functools import wraps
import requests
# import json
from datetime import datetime
from config import DATABASE_URL, BOOK_READ_API_KEY, BOOK_API_KEY
app = Flask(__name__)
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(DATABASE_URL)
db = scoped_session(sessionmaker(bind=engine))
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'logged_in' not in session:
flash("Please Login or Signup to view website")
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
@app.context_processor
def book_processor():
def book_details(isbn):
dec = requests.get("https://www.googleapis.com/books/v1/volumes", params={"q": isbn, "key": BOOK_API_KEY})
data_json = dec.json()
if 'error' in data_json:
data = {'thumbnail': "/static/images/imagenotavailable.png",
'smallthumbnail': "/static/images/imagenotavailable.png",
'description': "Description Currently Not Available...",
'textsnippet': "Description Currently Not Available...",
'author': ""
}
else:
if 'thumbnail' in data_json:
thumbnail = data_json['items'][0]['volumeInfo']['imageLinks']['thumbnail']
else:
thumbnail = "/static/images/imagenotavailable.png"
if 'smallThumbnail' in data_json:
smallthumbnail = data_json['items'][0]['volumeInfo']['imageLinks']['smallThumbnail']
else:
smallthumbnail = "/static/images/imagenotavailable.png"
if 'description' in data_json:
description = data_json['items'][0]['volumeInfo']['description']
else:
description = "Description Currently Not Available..."
if 'textSnippet' in data_json:
textsnippet = data_json['items'][0]['searchInfo']['textSnippet']
else:
textsnippet = "Description Currently Not Available..."
if 'authors' in data_json:
author = data_json['items'][0]['volumeInfo']['authors'][0]
else:
author = ""
data = {'thumbnail': thumbnail,
'smallthumbnail': smallthumbnail,
'description': description,
'textsnippet': textsnippet,
'author': author
}
return data
return dict(book_details=book_details)
@app.context_processor
def review_processor():
def review_details(bookid):
book = db.execute("SELECT title, isbn FROM books WHERE id=:bookid",
{"bookid": bookid}).fetchone()
book_data = {'title': book.title,
'isbn': book.isbn
}
return book_data
return dict(review_details=review_details)
@app.route("/", methods=["GET", "POST"])
@login_required
def homepage():
username = session['user_name']
if request.method == 'GET':
book = db.execute("SELECT * FROM books WHERE id IN (SELECT bookid FROM reviews WHERE rating IN (SELECT MAX(rating) FROM reviews) Order By review_date desc)").fetchone()
return render_template("Homepage.html", username=username, book=book)
@app.route("/login", methods=['GET', 'POST'])
def login():
error = ""
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = db.execute("SELECT * FROM users WHERE username=:username and password=:password",
{"username": username, "password": password}).fetchone()
if user is None or username != user.username or password != user.password:
error = "Invalid Login"
return render_template("login.html", error=error)
elif username == user.username and password == user.password and user.active_status == False:
error = "You are Subscribed but your accounts awaiting approval"
return render_template("login.html", error=error)
else:
session['logged_in'] = True
session['user_name'] = user.username
session['user_id'] = user.id
if user.admin == True:
session['user_admin'] = True
return redirect(url_for('homepage'))
return render_template("Login.html", error=error)
@app.route("/logout")
@login_required
def logout():
session.pop('logged_in')
session.pop('user_name')
session.pop('user_id')
if 'user_admin' in session:
session.pop('user_admin')
flash('You are logged out')
return redirect(url_for('login'))
@app.route("/registration", methods=['GET', 'POST'])
def registration():
error = ""
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
confirm = request.form['confirm']
email = request.form['email']
if username is None or username == "" or password is None or password == "" or email is None or email == "":
print("error")
error = "Please fill all the fields."
elif password != confirm:
print("error")
error = "Password did not match!"
else:
db.execute(
"INSERT INTO users (username,password,email,regdate,active_status,admin) VALUES(:username,:password,:email,DEFAULT,FALSE,FALSE)",
{"username": username, "password": password, "email": email})
db.commit()
flash("Successfully Registered")
return redirect(url_for("login"))
return render_template('Registration.html', error=error)
@app.route("/user", methods=['GET', 'POST'])
@login_required
def user():
username = session['user_name']
user_id = session['user_id']
error = ""
if request.method == 'POST':
oldpassword = request.form['oldpassword']
newpassword = request.form['newpassword']
confirm = request.form['confirm']
user_pass = db.execute("SELECT password FROM users WHERE id=:user_id", {"user_id": user_id}).fetchone()
if oldpassword is None or oldpassword == "" or newpassword is None or newpassword == "" or confirm is None or confirm == "":
print("error")
error = "Please fill all the fields."
elif user_pass[0] != oldpassword:
print("error")
error = "Incorrect Credentials."
elif newpassword != confirm:
print("error")
error = "New Password did not match Confirm."
else:
db.execute(
"UPDATE users SET password=:newpassword", {"newpassword": newpassword})
db.commit()
flash("Successfully Updated Password")
return redirect(url_for("homepage"))
return render_template('User.html', error=error, username=username)
@app.route("/admin", methods=['GET', 'POST'])
@login_required
def admin():
username = session['user_name']
user_id = session['user_id']
error = ""
approvals = {}
if 'user_admin' in session:
if request.method == 'GET':
approvals = db.execute("SELECT * FROM users WHERE active_status=FALSE").fetchall()
elif request.method == 'POST':
approve_username = request.form.get('approval')
decline_username = request.form.get('decline')
if approve_username is not None:
print(
"APPROVING USER, USER_NAME : {}".format(approve_username))
db.execute("UPDATE users SET active_status=TRUE WHERE username=:USER_NAME", {"USER_NAME":approve_username})
print("APPROVED!")
db.commit()
flash(f"{approve_username} : USER APPROVED!")
return redirect(url_for("admin"))
elif decline_username is not None:
print(
"REMOVING USER, USER_NAME : {}".format(approve_username))
db.execute("DELETE FROM users WHERE username=:USER_NAME",
{"USER_NAME": decline_username})
print("REMOVED!")
db.commit()
flash(f"{decline_username} : USER DECLINED!")
return redirect(url_for("admin"))
else:
flash("You are not an Admin")
return redirect(url_for("homepage"))
return render_template('Admin.html', error=error, username=username, approvals=approvals)
@app.route("/search", methods=['GET'])
@login_required
def search():
username = session['user_name']
val = request.args.get('search')
if val:
books = db.execute("SELECT * FROM books WHERE lower(isbn) LIKE lower(:isbn) or lower(title) LIKE lower(:title) or lower(author) LIKE lower(:author)",
{'isbn': "%" + val + "%", 'title': "%" + val + "%", 'author': "%" + val + "%"})
else:
flash("Search by Title, Isbn or Author to get a list of books")
books = []
return render_template("Search.html", username=username, books=books)
@app.route("/reviews", methods=['GET', 'POST'])
@login_required
def reviews():
username = session['user_name']
user_id = session['user_id']
if request.method == 'GET':
user_reviews = db.execute("SELECT * FROM reviews WHERE userid=:userid ORDER BY review_date desc",
{"userid": user_id}).fetchall()
review_count = db.execute("SELECT COUNT(*) FROM reviews WHERE userid=:userid",
{"userid": user_id}).fetchone()
review_avg = db.execute("SELECT CAST(AVG(rating) AS DECIMAL (10,2)) FROM reviews WHERE userid=:userid",
{"userid": user_id}).fetchone()
return render_template("Reviews.html", username=username, reviews=user_reviews, count=review_count, avg=review_avg)
elif request.method == 'POST':
book_id = request.form['delete']
print(
"DELETING REVIEW FOR BOOK ID : {}, USER_ID : {}".format(book_id, user_id))
db.execute(
"DELETE FROM reviews WHERE userid=:user_id AND bookid=:book_id",
{"user_id": user_id, "book_id": int(book_id)})
print("DELETED")
db.commit()
flash("REVIEW DELETED!")
user_reviews = db.execute("SELECT * FROM reviews WHERE userid=:userid ORDER BY review_date desc",
{"userid": user_id}).fetchall()
return render_template("Reviews.html", username=username, reviews=user_reviews)
@app.route("/books/<string:isbn>", methods=['GET', 'POST'])
@login_required
def book(isbn):
username = session['user_name']
error = ""
user_id = session['user_id']
book = db.execute("SELECT * FROM books WHERE isbn=:isbn", {'isbn': isbn}).fetchone()
book_id = book.id
if request.method == 'GET':
res = requests.get("https://www.goodreads.com/book/review_counts.json",
params={"key": BOOK_READ_API_KEY, "isbns": isbn})
return render_template('Book.html', username=username, response=res.json(), book=book)
else:
review_text = request.form['review']
rating = request.form['rating']
try:
review_exist = db.execute("SELECT * FROM reviews WHERE userid=:userid and bookid=:bookid",
{"userid": user_id, "bookid": book_id}).fetchone()
if review_exist:
error = "You have already reviewed this book!"
else:
print(
"INSERTING REVIEW FOR BOOK ID : {}, USER_ID : {}, RATING : {}, REVIEW : {}, REVIEW_DATE : {}".format(
book_id, user_id, rating, review_text, datetime.now()))
db.execute(
"INSERT INTO reviews (userid,bookid,rating,review,review_date)VALUES(:user_id,:book_id,:rating,:review_text,:review_date)",
{"user_id": user_id, "book_id": book_id, "rating": rating, "review_text": review_text,
"review_date": datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
print("INSERTED")
db.commit()
flash("REVIEW COMMITTED!")
return redirect(url_for('reviews'))
except:
error = "INSERT ERROR"
return render_template('Book.html', username=username, error=error, book=book)
@app.route("/api/<string:isbn>")
# @login_required
def book_api(isbn):
book_item = db.execute("SELECT * FROM books WHERE isbn=:isbn", {'isbn': isbn}).fetchone()
book_id = book_item.id
if book:
book_json = {"title": book_item.title, "author": book_item.author, "year": book_item.year,
"isbn": book_item.isbn}
rev_count = dict(
db.execute("SELECT COUNT(*) FROM Reviews WHERE bookid=:bookid", {"bookid": book_id}).fetchone())
avg_score = dict(
db.execute("SELECT CAST(AVG(rating) AS DECIMAL (10,2)) FROM reviews WHERE bookid=:bookid", {"bookid": book_id}).fetchone())
book_json.update(review_count=rev_count['count'], average_score=str(avg_score['avg']))
return jsonify(book_json)
else:
return render_template('404.html')
if __name__ == "__main__":
app.run()