-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
290 lines (219 loc) · 8.81 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
import os
import json
import tool
import constants
import random as rm
import mysql.connector
from functools import wraps
from datetime import datetime
from flask import Flask, render_template, redirect, url_for, request, session, jsonify
# DB connection
conn = mysql.connector.connect(user='root', password='root', host="localhost", database="book")
app = Flask(__name__)
# Wrapper function to verify that user is logged in
# If user is not logged in, redirects to login page
def isLoggedIn(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
return redirect(url_for('login'))
return wrap
# Wrapper function to verify that user is logged in as admin
# If user is not logged in as admin, redirects to home page
# If user is not logged in, redirects to login
def isLoggedAdmin(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
if session['user_type'] == 1:
return f(*args, **kwargs)
else:
return redirect(url_for('home'))
else:
return redirect(url_for('login'))
return wrap
# Init redirect
@app.route('/')
def index():
return redirect(url_for('login'))
# Home page
@app.route('/home')
def home():
cur = conn.cursor(dictionary=True)
cur.execute("select * from books limit %s", [24])
books = cur.fetchall()
cur.close()
return render_template('home.html', books=books)
# Page for specific book
@app.route('/book/<string:isbn>')
@isLoggedIn
def book(isbn):
cur = conn.cursor(dictionary=True)
# Grab info for book based on isbn
cur.execute("select * from books where BISBN = %s", [isbn])
# Try to fetch book info from DB
b = cur.fetchone()
# Grab the person who posted the book based on isbn
# I Limit 1 because this particular db structure only allows a single type of book
# to be posted once, meaning there should only be 1 result
cur.execute(f"select * from postings where UBooks LIKE '%\"{isbn}\"%' LIMIT 1")
# Try to fetch book info from DB
p = cur.fetchone()
cur.close()
# changing the serialized lists, to a list data structure
p['UBooks'] = json.loads(p['UBooks'])
p['PostDates'] = json.loads(p['PostDates'])
# removing all other books and postdates except the one for this book
p['PostDates'] = p['PostDates'][p['UBooks'].index(isbn)]
p['UBooks'] = isbn
# getting the email of the poster
UEmail = tool.getUser("UEmail", p['UserID'], conn)
# Only load page if book was found
if b and p:
b.update(p)
b.update(UEmail)
return render_template('book.html', book=b)
# if the book is not found then display error page
else:
return redirect(url_for('index'))
# DB search page
@app.route('/search', methods=['GET', 'POST'])
def search():
# Result of search
if request.method == 'POST':
cur = conn.cursor(dictionary=True)
# Grab info from HTML form
method = request.form.get('searchby').strip()
query = request.form.get('query').strip()
# Builds query with column name using python string manipulation
# Method is within a set of fixed values, low risk of SQL injection
q = "select * from books where " + method + " like %s"
args = ('%' + query + '%',)
cur.execute(q, args)
books = cur.fetchall()
cur.close()
return render_template('search.html', books=books, post=True, query=query)
# GET method, display search and search options
else:
return render_template('search.html')
# DB signup page
@app.route('/signup', methods=['GET', 'POST'])
def signup():
return render_template('signup.html')
# used to verify if all fields when signup is valid
@app.route('/verifySignUp', methods=['POST'])
def verifySignUp():
email = request.form["email"]
username = request.form["username"]
password = request.form["password"]
confirm_password = request.form["confirm_password"]
if password == "":
return jsonify({"error": "Password field empty"})
if password != confirm_password:
return jsonify({"error": "Password does not match confirmed password"})
# attempting to create account, if account creation fails, returns False and reason is printed
attempt_status, result = tool.register(username, password, email, conn)
# if the account could not be registered display why
if not attempt_status:
return jsonify({"error": result})
# if the account was created successfully log the account in
# result should contain the user dict
return verifyLogin(result)
# DB login page
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template("login.html")
@app.route('/logout')
@isLoggedIn
def logout():
session.clear()
return redirect(url_for('login'))
# used to verify if all fields when logging in is valid
@app.route('/verifyLogin', methods=['POST'])
def verifyLogin(user=None):
# no user information is given, then look for credentials matching
# that of the form
# (should only be given after a successful signup)
if user is None:
email = request.form["email"]
password = request.form["password"]
user = tool.userLogin(email, password, conn)
# if a user with matching credentials was found
# save information into session
if user is not None:
if user['IsAdmin']:
session['user_type'] = 1
else:
session['user_type'] = 2
session['logged_in'] = True
session['user_dict'] = user # all information needed can be found via this dict
return redirect(url_for('home'))
else:
return jsonify({'error': 'Incorrect password or email'})
@app.route('/posting')
@isLoggedIn
def posting():
return render_template("posting.html", courses=constants.courseIds)
@app.route('/verifyPosting', methods=['POST'])
def verifyPosting():
cur = conn.cursor(dictionary=True)
BISBN = request.form["BISBN"]
BTitle = request.form["BTitle"].strip()
BAuthor = request.form["BAuthor"].strip()
BCourse = request.form["BCourse"].strip()
BPrice = float(request.form["BPrice"])
BPic = rm.choice(constants.sampleBoookPics)
BNumber = int(request.form["BNumber"])
BDesc = request.form["BDesc"].strip()
if not tool.isValidISBN(BISBN):
return jsonify({"error": "Please enter a valid ISBN"})
if BCourse == "default":
return jsonify({"error": "Please select a course"})
try:
insertion_command = "INSERT INTO Books (BISBN, BTitle, BAuthor, BCourse, BPrice, BDesc, BPic, BNumber) VALUES " \
"(%s, %s, %s, %s, %s, %s, %s, %s)"
cur.execute(insertion_command, [BISBN, BTitle, BAuthor, BCourse, BPrice, BDesc, BPic, BNumber])
conn.commit()
cur.close()
except mysql.connector.Error:
cur.close()
return jsonify({"error": f"ISBN {BISBN} has already been posted"})
# updating the user's posting profile
postingHelper(BISBN)
return redirect(url_for('home'))
# updates relevant tables after a post has been made
def postingHelper(BISBN):
cur = conn.cursor(dictionary=True, buffered=True)
# getting all books posted by a user
retrieval_command = "Select UBooks, PostDates from postings where UserID = %s"
cur.execute(retrieval_command, [session['user_dict']['UserID']])
data = cur.fetchone()
post_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# in case that this is the first posting by a user
# add a row in the table
if data is None:
insertion_command = "INSERT INTO Postings (UserID, UBooks, PostDates) VALUES (%s, %s, %s)"
cur.execute(insertion_command, [session['user_dict']['UserID'], json.dumps([BISBN]), json.dumps([post_date])])
# if the user posted a book already, update data
else:
pBooks, pPostingDates = json.loads(data['UBooks']), json.loads(data['PostDates'])
# updating the data to now include the new post
pBooks.append(BISBN)
pPostingDates.append(post_date)
# updating the row in the db
update_postings_command_1 = "UPDATE Postings SET UBooks = %s WHERE UserID = %s"
update_postings_command_2 = "UPDATE Postings SET PostDates = %s WHERE UserID = %s"
cur.execute(update_postings_command_1, [json.dumps(pBooks), session['user_dict']['UserID']])
cur.execute(update_postings_command_2, [json.dumps(pPostingDates), session['user_dict']['UserID']])
conn.commit()
cur.close()
if __name__ == '__main__':
# set up commands
# tool.db_setup(conn, conn.cursor(dictionary=True), "sqlcommands_initial.sql")
# tool.db_insert_random_users(conn, numUsers=20)
# tool.db_insert_n_random_postings(conn, numPostings=40)
app.secret_key = os.urandom(12)
app.debug = True
app.run()