/
routes.py
567 lines (474 loc) · 18.7 KB
/
routes.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
from bson import ObjectId
from uuid import uuid4
from flask import Flask, render_template
from flask import (flash, request, redirect, render_template,
Flask, url_for, make_response, session)
from flask_login import (login_user, current_user, logout_user,
LoginManager, login_required)
from flask_paranoid import Paranoid
from models.users import User
from models.quiz import Quiz
from models.history import History
from models.contact import Contact
from Forms.SignUp import SignupForm
from Forms.Login import LoginForm
from Forms.Quiz import AddQuestion, EditQuestion, Play
from Forms.Settings import SettingsForm
from Forms.Contact import ContactForm
from random import randint
DEBUG = True
app = Flask(__name__)
app.config['SECRET_KEY'] = str(uuid4())
login_manager = LoginManager()
@login_manager.user_loader
def load_user(_id):
if type(_id) is str:
return User.get_user_info(
_id=ObjectId(_id)
)
return User.get_user_info(
_id=_id
)
# print(History.show_history())
@app.context_processor
def inject_stage_and_region():
return dict(
enumerate=enumerate,
leaderboard=History.show_history()
)
# Login Manager Config
login_manager.init_app(app)
login_manager.session_protection = None # for flask paranoid
login_manager.login_view = 'login'
login_manager.login_message_category = "info"
# For Preventing Multiple requests using same cookie from
# Multiple IPs
paranoid = Paranoid(app)
paranoid.redirect_view = '/'
User.load_user = classmethod(load_user)
@app.route('/')
@app.route('/home')
def index():
template = 'index.html'
if current_user.is_authenticated:
if current_user.is_admin:
return redirect('/admin/dashboard', 302)
else:
if request.path in ['/admin', '/admin/dashboard']:
return render_template("404.html")
resp = make_response(render_template(template))
# for Chrome
resp.set_cookie('SameSite',
'None',
samesite="Lax",
secure=True)
resp.set_cookie('cross-site-cookie',
'None',
samesite='Lax',
secure=True)
return resp
@app.route('/admin/dashboard')
def admin_dashboard():
if current_user.is_authenticated and current_user.is_admin:
messages = Contact.get_messages()
return render_template(
'admin_dashboard.html',
messages=messages,
zip=zip,
range=range, len=len,
str=str
)
return render_template("404.html")
@app.route('/leaderboard')
def leaderboard():
if not History.show_history():
flash('No Player Data is available at the moment')
return redirect('/')
return render_template('leaderboard.html')
@login_required
@app.route('/admin/dashboard/Players/view')
@app.route('/admin/dashboard/Players/remove')
@app.route('/admin/dashboard/Players/remove/<string:pid>', methods=['GET', 'POST'])
def player_dashboard(pid=None):
if current_user.is_authenticated and current_user.is_admin:
players = User.get_all_users()
if request.path == '/admin/dashboard/Players/view':
return render_template('view_players.html', players=players)
elif pid and len(pid) < 100 and (pid.replace('-', '').isalnum() or pid.replace('_', '').isalnum()):
if not User.get_user_info(username=pid):
flash('Can\'t find player')
return redirect('/admin/dashboard/Players/view')
if request.method == "POST":
Quiz.remove_player(pid)
flash('Player Removed!')
return redirect('/admin/dashboard/Players/view')
return render_template('remove_player.html', players=players)
return render_template('view_players.html', players=players)
return render_template('404.html')
@login_required
@app.route('/admin/dashboard/Quiz/add', methods=['GET', 'POST'])
@app.route('/admin/dashboard/Quiz/edit')
@app.route('/admin/dashboard/Quiz/edit/<string:qid>',
methods=["GET", "POST"])
@app.route('/admin/dashboard/Quiz/delete')
@app.route('/admin/dashboard/Quiz/delete/<string:qid>',
methods=["GET", "POST"])
def quiz_dashboard(qid=None):
if not current_user.is_authenticated:
flash('Login is Required for this action!')
return redirect('/', 302)
if not current_user.is_admin:
flash('You need to be an Admin for Performing this action!')
return redirect('/', 302)
template = "_question.html"
req_path = request.path.split('/')[-1]
if qid and len(qid) > 24:
return render_template('404.html')
if req_path == qid:
req_path = request.path.split('/')[-2]
if req_path == 'add':
form = AddQuestion()
if request.method == "POST":
if form.validate():
sol = {
1: form.option1.data,
2: form.option2.data,
3: form.option3.data,
4: form.option4.data,
}
Quiz(
question=form.question.data,
options=[
form.option1.data,
form.option2.data,
form.option3.data,
form.option4.data,
],
answer=sol.get(form.solution.data)
).save_quiz()
flash('Question Saved Successfully!')
return redirect('/admin/dashboard/Quiz/edit')
return render_template('add' + template, form=form)
elif req_path == 'edit':
if not qid:
questions = Quiz.get_questions(userID=None,
show_history=True,
is_admin=True, limit=1000)
for question in questions:
question._id = str(question._id)
return render_template('display_questions.html', questions=questions)
form = EditQuestion()
question = Quiz.get_question(qid)
if request.method == "POST":
if form.validate_on_submit():
if question:
question.question = form.question.data
question.options = [
form.option1.data,
form.option2.data,
form.option3.data,
form.option4.data
]
sol = {
1: form.option1.data,
2: form.option2.data,
3: form.option3.data,
4: form.option4.data,
}
question.answer = sol.get(form.solution.data)
question.update_quiz_info()
# print('Quiz updated!')
flash('Question Edited Successfully!')
return redirect('/admin/dashboard/Quiz/edit')
else:
flash('Unable to find Question in DB')
return redirect('/')
else:
flash("Couldn't Validate form..")
# print(form.validate_on_submit())
return render_template('edit' + template, form=form)
if question:
form.solution.default = question.options.index(
question.answer
) + 1
form.process()
form.question.data = question.question
form.option1.data = question.options[0]
form.option2.data = question.options[1]
form.option3.data = question.options[2]
form.option4.data = question.options[3]
# form.process()
return render_template('edit' + template, form=form)
elif req_path == 'delete' or qid:
if request.method == "POST" and qid:
# print(help(Quiz.DB.CURSOR.delete_one))
Quiz.DB.CURSOR.Quiz.delete_one({'_id': ObjectId(qid)})
flash('Question Deleted!')
return redirect('/admin/dashboard/Quiz/delete')
if not qid:
questions = Quiz.get_questions(userID=None,
show_history=True,
is_admin=True, limit=1000)
for question in questions:
question._id = str(question._id)
return render_template('display_questions.html', questions=questions)
return render_template('delete_question.html')
else:
return render_template('404.html')
@login_required
@app.route('/final_score')
def display_score():
if session.get('score') != None:
current_user.total_score += session.get('score')
current_user.update_user_info()
score = session.pop('score')
History.find_and_update(userID=current_user._id, score=score)
return render_template('final_score.html', score=score)
else:
flash('You haven\'t played the Quiz yet!')
return redirect('/')
@login_required
@app.route('/play', methods=['GET', 'POST'])
@app.route('/play/<string:qid>', methods=["POST"])
def play_quiz(qid=None):
form = Play()
if not current_user.is_authenticated:
flash('Please Login for playing Quiz..')
return redirect('/login')
if request.method == "POST":
if form.validate():
if question := Quiz.get_question(form.qid.data):
if Quiz.attempt_question(current_user, form.ans.data, question):
session['score'] += 10
return redirect('/play')
else:
return redirect('/final_score')
return render_template('404.html')
else:
errors = []
errors.append(form.qid.errors)
errors.append(form.ans.errors)
errors.append(form.current_score.errors)
errors.append(form.correct_ans.errors)
if qid:
return redirect('/play/')
return str(errors)
if not qid:
if question := Quiz.get_questions(current_user._id, sample=1):
# question is a list with one element
question = question[0] # accessing that element
form.qid.data = str(question._id)
if not form.current_score.data:
form.current_score.data = 0
form.correct_ans.data = question.answer
else:
flash('Well Done! You have reached the end of the Quiz!')
if session.get('score') != None and session.get('score') > 0:
current_user.total_wins += 1
current_user.update_user_info()
return redirect('/final_score')
return redirect('/')
score = form.current_score.data
if session.get('score'):
score += session.get('score')
form.current_score.data += session.get('score')
else:
session['score'] = score = 0
return render_template(
"main_quiz.html",
form=form,
quiz=question,
score=score
)
@app.route('/login', methods=["GET", "POST"])
def login():
if current_user.is_authenticated:
return redirect('/', 302)
form = LoginForm()
if request.method == 'POST':
if result := form.validate():
# print("result:", result)
login_user(
User(
_id=str(result.get('_id')),
username=result.get('username'),
name=result.get('name')
)
)
flash(f"Welcome {current_user.name.split()[0]}!")
if result.get('is_admin'):
return redirect('/admin/dashboard')
return redirect(f'/player/{current_user.username}', 302)
return render_template('login.html', form=form)
@app.route('/signup', methods=["GET", "POST"])
@app.route('/signup/admin', methods=["GET", "POST"])
def signup():
if current_user.is_authenticated:
return redirect('/', 302)
is_admin = False
form = SignupForm()
if request.path == '/signup/admin':
is_admin = True
if request.method == "POST" and form.validate():
User(
username=str(uuid4()),
email=form.email.data.lower(),
password=form.password.data,
name=form.name.data.title(),
is_admin=is_admin
).save_user(signup=True)
flash("Registration Completed! Please Login..")
return redirect(url_for('index'))
if is_admin:
return render_template("signup_admin.html", form=form)
return render_template("signup.html", form=form)
@app.route('/player/<string:username>')
def player(username):
if len(username) > 36:
flash('User Does Not Exist!')
return redirect('/', 302)
user = User.get_user_info(username=username)
return render_template("player.html", user=user)
@app.route('/followers/<string:username>')
def followers(username):
if not username and len(username) > 36:
flash("User Not Found!")
return redirect('/', 302)
user = User.get_user_info(username=username)
if user:
return render_template('followers.html', user=user)
flash("User Not Found!")
return redirect('/', 302)
@app.route('/following/<string:username>')
def following(username):
if not username and len(username) > 36:
flash("User Not Found!")
return redirect('/', 302)
user = User.get_user_info(username=username)
if user:
return render_template('following.html', user=user)
flash("User Not Found!")
return redirect('/', 302)
@app.route('/follow/<string:username>')
def follow(username):
if not username and len(username) > 36:
flash("User Not Found!")
return redirect('/', 302)
if current_user.is_authenticated:
user = User.get_user_info(username=username)
if user and current_user.follow_user(user):
return redirect(f'/player/{username}')
flash("User Not Found!")
return redirect('/', 302)
flash('Please Login before this action!')
return redirect('/', 302)
@app.route('/unfollow/<string:username>')
def unfollow(username):
if not username and len(username) > 36:
flash("User Not Found!")
return redirect('/', 302)
if current_user.is_authenticated:
user = User.get_user_info(username=username)
if user and current_user.unfollow_user(user):
return redirect(f'/player/{username}')
flash("User Not Found!")
return redirect('/', 302)
flash('Please Login before this action!')
return redirect('/', 302)
@app.route('/settings', methods=['GET', 'POST'])
@app.route('/settings/delete', methods=['GET', 'POST'])
@login_required
def settings():
if not current_user.is_authenticated:
flash('Please Login to continue..')
return redirect('/login')
user = current_user
form = SettingsForm()
form.name.render_kw = {'placeholder': user.name}
form.username.render_kw = {'placeholder': user.username}
form.email.render_kw = {'placeholder': user.email}
if request.method == 'GET':
if request.path == '/settings/delete':
return render_template('confirm_remove.html')
return render_template('settings.html', user=user, form=form)
elif request.method == 'POST':
if request.path == '/settings/delete':
Quiz.remove_player(user.username)
flash('Sorry to see you go :(')
flash('You will be missed..')
return redirect('/')
if form.validate():
if form.name.data.strip() and user.name != form.name.data.strip():
user.name = form.name.data.strip()
form.name.render_kw = {'placeholder': form.name.data}
user.update_user_info()
flash('Name Updated Successfully!')
if form.username.data.strip() and user.username != form.username.data.strip():
user.username = form.username.data.strip().lower()
form.username.render_kw = {
'placeholder': form.username.data.strip().lower()}
user.update_user_info()
flash('Username Updated Successfully!')
if form.email.data.strip() and user.email != form.email.data.strip():
user.email = form.email.data.strip().lower()
form.email.render_kw = {
'placeholder': form.email.data.strip().lower()}
user.update_user_info()
flash('Email Address Updated Successfully!')
if form.password.data and len(form.password.data) in [i for i in range(8, 17)]:
user.password = form.password.data
user.update_user_info(settings=True)
flash('Password Updated Successfully!')
form.email.data = form.name.data = form.username.data = ""
return render_template('settings.html', user=user, form=form)
return render_template('404.html')
@app.route('/logout')
def logout():
if current_user.is_authenticated:
fname = current_user.name.split()[0].capitalize()
logout_user()
flash(f'See You Next Time {fname}!')
return redirect('/')
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")
@app.route('/about/how')
@app.route('/about/why')
@app.route('/about/who')
def about():
path = request.path.split('/')[-1]
return render_template(f'about_{path}.html')
@app.route('/contact', methods=['GET', 'POST'])
@app.route('/contact/delete/<string:mid>', methods=['GET', 'POST'])
def contact(mid=None):
if request.path == '/contact':
form = ContactForm()
if current_user.is_authenticated:
form.email.data = current_user.email
form.name.data = current_user.name
if request.method == 'POST':
if form.validate():
Contact(
email=form.email.data,
title=form.title.data,
name=form.name.data,
message=form.message.data
).save_message()
flash('Message Sent to Admin!')
return redirect('/')
return render_template('contact.html', form=form)
if current_user.is_authenticated:
if current_user.is_admin and mid and len(mid) == 24 and mid.isalnum():
if Contact.get_message(mid):
if request.method == 'POST':
Contact.delete_message(mid)
flash('Message Deleted!')
return redirect('/')
return render_template(
'confirm_message_delete.html',
mid=mid
)
return redirect('/')
return render_template('404.html')
if __name__ == "__main__":
app.run(debug=DEBUG)