-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview_anything_app.py
353 lines (290 loc) · 9.89 KB
/
review_anything_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from flask import (
Flask, render_template, request, url_for, redirect)
from flask_login import (
UserMixin, LoginManager, login_required,
login_user, logout_user, current_user)
import os
import json
from additional_funcs import (
create_db_conn, flash_message, cursor_results,
check_pw, create_hash_pw, make_csrf, verify_csrf
)
import urllib.parse
from datetime import datetime
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
CONFIG_ARGS = json.loads(open(os.path.join(APP_ROOT, 'config.json')).read())
# make sure to set debug to false for production
DEBUG = CONFIG_ARGS['DEBUG']
HOST = CONFIG_ARGS['HOST']
PORT = CONFIG_ARGS['PORT']
app = Flask(__name__)
app.secret_key = os.urandom(16)
login_manager = LoginManager()
login_manager.init_app(app)
class User(UserMixin):
def __init__(self, id_, name, email):
self.id = id_
self.name = name
self.email = email
# noinspection SqlDialectInspection
@login_manager.user_loader
def user_loader(user_id):
conn, cursor = create_db_conn()
cursor.execute('select * from users where id = ?', (user_id,))
results = cursor_results(cursor)
conn.close()
if len(results) == 0:
return
user = User(results[0]['id'], results[0]['name'], results[0]['email'])
return user
@login_manager.unauthorized_handler
def unauthorized_handler():
return render_template('unauthorized.html')
# noinspection SqlDialectInspection
@app.route('/create_review', methods=['GET', 'POST'])
@login_required
def create_review():
if request.method == 'GET':
make_csrf()
return render_template('create_review.html')
form_title = request.form['title']
form_rating = request.form['rating']
form_review = request.form['review']
if not form_title or not form_rating or not form_review:
flash_message('Not all information provided.', 'danger')
return render_template('create_review.html')
if not verify_csrf():
return render_template('create_review.html')
# TODO: add captcha verification
conn, cursor = create_db_conn()
cursor.execute(
'insert into reviews VALUES (?,?,?,?,?,?,?,?)',
(None, form_title, form_rating, form_review,
current_user.id, datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
0, 0)
)
conn.commit()
cursor.execute('select * from reviews ORDER BY id desc limit 1')
results = cursor_results(cursor)
conn.close()
url = url_for("view_review", id_=results[0]['id'], title=results[0]['title'])
view_text = ' View it <a href="{}">here</a>.'.format(url)
flash_message('Your review has been created!' + view_text, 'success')
return render_template('create_review.html')
# noinspection SqlDialectInspection
@app.route('/search')
def search():
search_text = request.args['keyword']
conn, cursor = create_db_conn()
cursor.execute(
"""
SELECT *
FROM
(
SELECT
reviews.*,
u.name,
u.id AS user_id
FROM reviews
INNER JOIN users AS u
ON reviews.created_by = u.id
WHERE title LIKE ? OR review LIKE ? OR name LIKE ?
AND (downvotes < 3 OR upvotes > 0)
ORDER BY upvotes, date_created
)
""",
('%'+search_text+'%', '%'+search_text+'%', '%'+search_text+'%')
)
results = cursor_results(cursor)
conn.close()
return render_template('search_results.html', results=results)
# noinspection SqlDialectInspection
@app.route('/review/<id_>/<title>')
def view_review(id_, title):
conn, cursor = create_db_conn()
cursor.execute(
"""
select r.id as [id], r.title as [title], r.rating as [rating],
r.review as [review], u.name as [by], u.id as [user_id],
r.date_created as [date], r.upvotes as [up],
r.downvotes as [down]
from reviews as r
INNER JOIN users as u
on r.created_by = u.id
where r.id = ? and title = ?
""",
(id_, title)
)
results = cursor_results(cursor)
if len(results) == 0:
flash_message('No review found.', 'danger')
return render_template('view_review.html')
results = results[0]
return render_template(
'view_review.html', title=results['title'],
by=results['by'], rating=results['rating'],
id=results['id'], user_id=results['user_id'],
date=results['date'], up=results['up'],
down=results['down'], review=results['review']
)
# noinspection SqlDialectInspection
@app.route('/user/<id_>/<name>')
def view_user(id_, name):
conn, cursor = create_db_conn()
cursor.execute(
"""
select reviews.*, u.name, u.id as user_id from reviews
INNER JOIN users as u
on reviews.created_by = u.id
where u.id = ? and name = ?
""",
(id_, name)
)
results = cursor_results(cursor)
cursor.execute(
"select * from users where id = ? and name = ?",
(id_, name)
)
user = cursor_results(cursor)[0]
conn.close()
return render_template('user_page.html', results=results, user=user)
# noinspection SqlDialectInspection
@app.route('/vote')
@login_required
def vote():
review_id = request.args['id']
up_down = request.args['ud']
if up_down not in ('0', '1'):
flash_message('Invalid options.', 'danger')
return redirect(url_for('home'))
# TODO: figure out a better way to do this
# my idea here was to re-route the user to
# the page they came from
referrer = request.referrer
if referrer is None:
next_url = url_for('home')
else:
referrer_parsed = urllib.parse.urlparse(referrer)
next_url = urllib.parse.urlunparse(
('', '', referrer_parsed.path, '',
referrer_parsed.query, '')
)
conn, cursor = create_db_conn()
cursor.execute(
'select * from votes where review_id = ? and user_id = ?',
(review_id, current_user.id)
)
results = cursor_results(cursor)
if len(results) != 0:
conn.close()
flash_message("You've already voted on this review.", 'danger')
return redirect(next_url)
cursor.execute(
'insert into votes VALUES (?,?,?,?)',
(None, review_id, current_user.id, up_down)
)
up_or_down = 'upvotes' if up_down == '1' else 'downvotes'
cursor.execute(
"""
update reviews set
{up_down} = (
select {up_down} from reviews where id = ?
) + 1
WHERE id = ?
""".format(up_down=up_or_down),
(review_id, review_id)
)
conn.commit()
conn.close()
flash_message('Your vote has been posted.', 'success')
return redirect(next_url)
# noinspection SqlDialectInspection
@app.route('/login', methods=['GET', 'POST'])
def login():
"""A route for a user to login."""
if request.method == 'GET':
make_csrf()
return render_template('login.html')
form_email = request.form['email']
form_password = request.form['password']
conn, cursor = create_db_conn()
cursor.execute('select * from users where email = ?', (form_email,))
results = cursor_results(cursor)
conn.close()
if len(results) == 0:
flash_message('Incorrect email or password.', 'danger')
return render_template('login.html')
if not verify_csrf():
return render_template('login.html')
# TODO: add captcha verification
if check_pw(form_password, results[0]['hash'], results[0]['salt']):
user = User(results[0]['id'], results[0]['name'], results[0]['email'])
login_user(user)
flash_message('You are now logged in.', 'success')
return redirect(url_for('home'))
flash_message('Incorrect email or password.', 'danger')
return render_template('login.html')
# noinspection SqlDialectInspection
@app.route('/add_user', methods=['GET', 'POST'])
def add_user():
"""A route for creating new users."""
if request.method == 'GET':
make_csrf()
return render_template('add_user.html')
form_email = request.form['email']
form_name = request.form['name']
form_password = request.form['password']
if not form_email or not form_name or not form_password:
flash_message('All fields need to be provided.', 'danger')
return render_template('add_user.html')
if not verify_csrf():
return render_template('login.html')
# TODO: add captcha verification
# check that the email doesn't already exist
conn, cursor = create_db_conn()
cursor.execute('select * from users where email = ?', (form_email,))
results = cursor_results(cursor)
if len(results) != 0:
conn.close()
flash_message('Account with that email already exists.', 'danger')
return render_template('add_user.html')
# hash the pw
hashed_pw, salt = create_hash_pw(form_password)
# add new user to db
cursor.execute(
'insert into users VALUES (?,?,?,?,?)',
(None, form_name, form_email, hashed_pw, salt)
)
conn.commit()
conn.close()
flash_message('New account created under {}'.format(form_email), 'success')
return redirect(url_for('home'))
@app.route('/logout')
def logout():
"""The route to log out a user."""
logout_user()
return redirect(url_for('home'))
# noinspection SqlDialectInspection
@app.route('/')
def home():
conn, cursor = create_db_conn()
cursor.execute("""
SELECT *
FROM
(
SELECT
reviews.*,
u.name,
u.id AS user_id
FROM reviews
INNER JOIN users AS u
ON reviews.created_by = u.id
WHERE (downvotes < 3 OR upvotes > 0)
ORDER BY upvotes DESC , date_created DESC
LIMIT 50
)
""")
results = cursor_results(cursor)
return render_template('home.html', reviews=results)
if __name__ == '__main__':
app.run(host=HOST, port=PORT, debug=DEBUG)