-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
662 lines (572 loc) · 22.3 KB
/
main.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
from flask import Flask, render_template, request, redirect
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
from data import db_session
import sqlalchemy as sa
import os
paths = ["/home/ilyav/testing-system/db/tests.sqlite",
os.path.join(os.getcwd(), "db", "tests.sqlite")]
db_path = paths[0]
for path in paths:
if os.path.exists(path):
db_path = path
break
db_session.global_init(db_path)
from data.__all_models import *
from forms.__all_forms import *
from notifications import bot
import datetime
import random
app = Flask(__name__)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.unauthorized_handler(callback=(lambda: redirect('/login')))
app.config['SECRET_KEY'] = 'testing_system_key'
def del_test(test_id):
db = db_session.create_session()
test = db.query(Test).get(test_id)
if test:
for q in test.questions:
for a in q.answers:
db.delete(a)
db.delete(q)
db.delete(test)
db.commit()
def save_and_notify(db, user, text, link=None):
notif = Notification()
notif.user_id = user.id
notif.text = text
notif.link = link
db.add(notif)
db.commit()
if link:
text += f'\nПосмотреть: https://ilyav.pythonanywhere.com{link}'
bot.notify(user, text)
def main():
# port = int(os.environ.get("PORT", 8000))
# app.run(host='0.0.0.0', port=port)
from waitress import serve
serve(app, host="0.0.0.0", port=8000)
@login_manager.user_loader
def load_user(user_id):
session = db_session.create_session()
session.expire_on_commit = False
return session.query(User).get(user_id)
def log(error):
message = str(type(error)) + ": " + str(error)
with open('log.txt', 'a') as file:
file.write(message + '\n' + str(datetime.datetime.now()) + '\n-----\n')
def is_allowed(test, user):
return any(list(group.id in list(i.id for i in test.groups)
for group in user.groups)) \
or test.creator == current_user \
or current_user.type_id == 1
def test_started():
db = db_session.create_session()
if db.query(Result).filter(~Result.is_finished, Result.user == current_user).first():
return True
return False
def add_group_to_test(group_id, test_id):
db = db_session.create_session()
group = db.query(Group).get(group_id)
test = db.query(Test).get(test_id)
if group and test and (current_user.type_id == 1 or current_user == test.creator):
test.groups.append(group)
for user in group.users:
text = f"Вам открыли доступ к тесту {test.name}"
link = "/"
save_and_notify(db, user, text, link)
db.commit()
def remove_group_from_test(group_id, test_id):
db = db_session.create_session()
group = db.query(Group).get(group_id)
test = db.query(Test).get(test_id)
if group and test and group in test.groups and (current_user.type_id == 1 or current_user == test.creator):
test.groups.remove(group)
for user in group.users:
text = f"У вас больше нет доступа к тесту {test.name}"
link = "/"
save_and_notify(db, user, text, link)
db.commit()
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
session = db_session.create_session()
user = session.query(User).filter(User.nickname == form.nickname.data.strip().lower()).first()
if user and user.check_password(form.password.data.strip()):
login_user(user, remember=form.remember_me.data)
return redirect("/")
return render_template("login.html",
title='Log in',
message="Password or login is incorrect",
form=form)
return render_template('login.html', title='Log in', form=form)
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect("/")
@app.route("/check")
@login_required
def check():
db = db_session.create_session()
bot.check_updates(db, User)
return redirect('/notifications')
@app.route('/disconnect')
@login_required
def disconnect():
db = db_session.create_session()
user = db.query(User).filter(User.id == current_user.id).first()
bot.disconnect(db, user)
return redirect('/notifications')
@app.route("/")
@app.route("/index")
@app.route("/all_tests")
@login_required
def index():
try:
if test_started():
return redirect('/test')
db = db_session.create_session()
tests = db.query(Test).all()
tests = list(test for test in tests if is_allowed(test, current_user))
return render_template("all_tests.html",
title="Тесты",
tests=tests,
all_tests='active')
except sa.orm.exc.DetachedInstanceError:
return redirect('/')
@app.route('/notifications')
@login_required
def show_notifs():
db = db_session.create_session()
notifs = db.query(Notification).filter(Notification.user == current_user).order_by(Notification.id.desc()).all()
for notif in notifs:
d = notif.date
notif.formatted = d.strftime('%H:%M (%d.%m)')
return render_template("notifications.html",
title="Уведомления",
notifications=notifs,
other="/notifications",
other_title="Уведомления")
@app.route("/groups")
@login_required
def get_groups():
try:
code = 0
db = db_session.create_session()
groups = db.query(Group).all()
if current_user.type_id == 3:
groups = list(group for group in groups if current_user in group.users)
if not groups:
code = 1
return render_template("all_groups.html",
title='Группы',
groups=groups,
other="/groups",
other_title='Группы',
code=code)
except sa.orm.exc.DetachedInstanceError:
return redirect("/groups")
@app.route("/groups/<int:group_id>")
@login_required
def get_group(group_id):
try:
code = 0
edit = True
title = 'Редактирование'
db = db_session.create_session()
group = db.query(Group).get(group_id)
tests = []
users = []
if not group:
code = 1
elif current_user.type_id == 3:
code = 2
else:
if current_user.type_id != 1 and current_user != group.creator:
edit = False
title = 'Просмотр'
tests = db.query(Test).all()
tests = list(filter(lambda t: group not in t.groups, tests))
if current_user.type_id == 2:
tests = list(filter(lambda t: t.creator == current_user, tests))
users = db.query(User).filter(User.type_id != 1,
User != current_user).order_by(User.type_id).all()
users = list(user for user in users if not user in group.users and user != group.creator)
return render_template("group.html",
title=title,
group=group,
other="/groups",
other_title='Группы',
users=users,
tests=tests,
edit=edit,
code=code)
except sa.orm.exc.DetachedInstanceError:
return redirect(f"/groups/{group_id}")
@app.route('/test_to_groups/<int:test_id>')
def show_test_to_groups(test_id):
code = 0
groups = []
edit = False
db = db_session.create_session()
test = db.query(Test).get(test_id)
if not test:
code = 1
elif not is_allowed(test, current_user) and current_user.type_id != 1 or current_user.type_id == 3:
code = 2
else:
groups = db.query(Group).filter(~Group.id.in_(list(group.id for group in test.groups))).all()
edit = test.creator == current_user or current_user.type_id == 1
return render_template("test_to_groups.html",
title="Группы с доступом",
test=test,
edit=edit,
groups=groups,
other=f"/test_to_groups/{test_id}",
other_title="Доступ",
code=code)
@app.route("/create_group", methods=['GET', 'POST'])
@login_required
def create_group():
if current_user.type_id != 3:
form = CreateGroupForm()
if form.validate_on_submit():
name = form.name.data
db = db_session.create_session()
group = Group()
group.creator_id = current_user.id
group.name = name
db.add(group)
db.commit()
return redirect(f'/groups/{group.id}')
return render_template("create_group.html",
other="/groups",
other_title='Группы',
form=form)
else:
return redirect('/more')
@app.route("/add_group_to_test/<int:test_id>/<int:group_id>")
@login_required
def add_group(test_id, group_id):
add_group_to_test(group_id, test_id)
return redirect(f'/test_to_groups/{test_id}')
@app.route("/remove_group_from_test/<int:test_id>/<int:group_id>")
@login_required
def remove_group(test_id, group_id):
remove_group_from_test(group_id, test_id)
return redirect(f'/test_to_groups/{test_id}')
@app.route("/add_test_to_group/<int:test_id>/<int:group_id>")
@login_required
def add_test(test_id, group_id):
add_group_to_test(group_id, test_id)
return redirect(f'/groups/{group_id}')
@app.route("/remove_test_from_group/<int:test_id>/<int:group_id>")
@login_required
def remove_test(test_id, group_id):
remove_group_from_test(group_id, test_id)
return redirect(f'/groups/{group_id}')
@app.route('/remove_user/<int:user_id>/<int:group_id>')
@login_required
def remove_user(user_id, group_id):
db = db_session.create_session()
user = db.query(User).get(user_id)
group = db.query(Group).get(group_id)
if user and group and user in group.users and (current_user.type_id == 1 or group.creator == current_user):
group.users.remove(user)
text = f"Вас исключили из группы {group.name} ({group.creator.nickname})"
link = "/groups"
save_and_notify(db, user, text, link)
db.commit()
return redirect(f'/groups/{group_id}')
@app.route('/add_user/<int:user_id>/<int:group_id>')
@login_required
def add_user(user_id, group_id):
db = db_session.create_session()
group = db.query(Group).get(group_id)
if group and (current_user.type_id == 1 or current_user == group.creator):
user = db.query(User).get(user_id)
if not user:
return
group.users.append(user)
text = f"Вас добавили в группу {group.name} ({group.creator.nickname})"
link = "/groups"
save_and_notify(db, user, text, link)
db.commit()
return redirect(f'/groups/{group_id}')
@app.route("/register", methods=['GET', 'POST'])
@login_required
def create_user():
code = 0
form = RegisterForm()
if current_user.type_id == 3:
code = 1
else:
if form.validate_on_submit():
db = db_session.create_session()
user = User()
user.nickname = form.nickname.data.strip().lower()
user.creator_id = current_user.id
user.set_password(form.password.data.strip())
if form.is_teacher.data:
user.type_id = 2
else:
user.type_id = 3
user.set_secret_code()
db.add(user)
db.commit()
return redirect('/users')
return render_template('register.html',
title='Создать',
form=form,
other="/register",
other_title='Регистрация',
code=code)
@app.route("/statistics/<int:test_id>")
@login_required
def get_test_statistics(test_id):
try:
code = 0
results = []
db = db_session.create_session()
test = db.query(Test).get(test_id)
if not test:
code = 1
return show_statistics(results, title=f"Такого теста нет", code=code)
elif test.creator != current_user:
results = db.query(Result).filter(Result.test_id == test_id,
Result.user_id == current_user.id,
~Result.is_deleted).all()
elif current_user.type_id == 1 or test.creator == current_user:
results = db.query(Result).filter(Result.test_id == test_id).all()
else:
code = 2
results.reverse()
return show_statistics(results, title=f"История по {test.name}", code=code)
except sa.orm.exc.DetachedInstanceError:
return redirect(f"/statistics/{test_id}")
@app.route("/user_statistics/<int:user_id>")
@login_required
def get_user_statistics(user_id):
try:
if user_id == current_user.id:
return redirect("/statistics")
code = 0
results = []
nickname = ""
db = db_session.create_session()
user = db.query(User).get(user_id)
if not user:
code = 3
elif (current_user.type_id == 3 and user != current_user) or \
(current_user.type_id == 2 and user not in current_user.created and current_user != user):
code = 2
else:
nickname = user.nickname
if current_user.type_id == 3:
results = db.query(Result).filter(Result.user_id == user_id, ~Result.is_deleted).all()
else:
results = db.query(Result).filter(Result.user_id == user_id).all()
results.reverse()
return show_statistics(results, title=f"История {nickname}", code=code)
except sa.orm.exc.DetachedInstanceError:
return redirect(f"/user_statistics/{user_id}")
@app.route("/statistics")
@login_required
def get_statistics():
try:
db = db_session.create_session()
results = db.query(Result).filter(Result.user_id == current_user.id, ~Result.is_deleted).all()
results.reverse()
return show_statistics(results, title="Моя история")
except sa.orm.exc.DetachedInstanceError:
return redirect("/statistics")
def show_statistics(results, title, code=0):
if not results and code == 0:
code = 1
results = list(r for r in results if r.is_finished)
for r in results:
all_answers = list(a.answer == a.correct for a in r.rows)
r.n_correct_answers = all_answers.count(True)
r.n_all_answers = len(all_answers)
r.finish_date = datetime.datetime.strftime(r.end_date, "%d %b, %H:%M")
return render_template("statistics.html",
title=title,
results=results,
code=code,
statistics='active')
@app.route('/users')
@login_required
def get_users():
try:
code = 0
if current_user.type_id == 3:
code = 1
db = db_session.create_session()
users = db.query(User).order_by(User.type_id).all()
return render_template('users.html',
title='Пользователи',
users=users,
other="/users",
other_title='Пользователи',
code=code)
except sa.orm.exc.DetachedInstanceError:
return redirect('/users')
@app.route("/delete_result/<int:result_id>")
@login_required
def delete_result(result_id):
return render_template("error.html",
text="Теперь так нельзя.",
link="/statistics",
button="Вернуться",
other="/statistics",
other_title="Статистика")
db = db_session.create_session()
result = db.query(Result).get(result_id)
if not result or (not result.user == current_user and current_user.type_id != 1):
return redirect('/statistics')
result.is_deleted = True
db.commit()
return redirect(f'/statistics/{result.test_id}')
@app.route('/delete_group/<int:group_id>')
@login_required
def delete_group(group_id):
db = db_session.create_session()
group = db.query(Group).get(group_id)
if group and (group.creator == current_user or current_user.type_id == 1):
group.users = []
group.tests = []
db.delete(group)
db.commit()
return redirect('/groups')
@app.route("/delete_user/<int:user_id>")
@login_required
def delete_user(user_id):
try:
db = db_session.create_session()
user = db.query(User).get(user_id)
if user and user in current_user.created or current_user.type_id == 1:
if not user.created_groups:
db.delete(user)
db.commit()
else:
return render_template("error.html",
text=f"Вы не можете удалить пользователя {user.nickname}, "
"так как он является администратором одной или нескольких групп",
link="/users",
button="Вернуться")
if user.results:
user.results = []
if user.groups:
user.groups = []
return redirect('/users')
except sa.orm.exc.DetachedInstanceError:
return redirect(f'/delete_user/{user_id}')
@app.route("/test/<int:test_id>")
@login_required
def start_test(test_id):
try:
if test_started():
return redirect('/test')
db = db_session.create_session()
test = db.query(Test).get(test_id)
if not test or not is_allowed(test, current_user):
return render_template("error.html",
text="Теста не существует или у вас нет прав доступа.",
button="На главную",
link="/all_tests")
result = Result()
result.test_id = test.id
result.user_id = current_user.id
db.add(result)
for q in test.questions:
row = ResultRow()
row.result = result
row.text = q.text
row.q_id = q.id
for a in q.answers:
if a.is_correct:
row.correct = a.text
break
db.add(row)
db.commit()
return redirect("/test")
except sa.orm.exc.DetachedInstanceError:
return redirect(f'/test/{test_id}')
@app.route("/test")
@login_required
def test():
if not test_started():
return redirect("/all_tests")
db = db_session.create_session()
result = db.query(Result).filter(Result.is_finished == False).first()
questions = db.query(ResultRow).filter(ResultRow.result_id == result.id).all()
result.n_questions = len(questions)
questions = list(q for q in questions if q.answer is None)
result.current_n = result.n_questions - len(questions) + 1
if questions:
q_id = random.choice(questions).q_id
question = db.query(Question).get(q_id)
question.n_questions = result.n_questions
question.current_n = result.current_n
if question:
return render_template("question.html",
title=result.test.name,
question=question,
all_tests="active")
return render_template("error.html",
text="Произошла ошибка. Скорее всего, тест был удалён.",
button="На главную",
link="/finish_test")
return redirect("/finish_test")
@app.route("/save_answer", methods=["GET", "POST"])
@login_required
def save_answer():
try:
q_id = request.form["question_id"]
answer = request.form["answer"]
except KeyError:
return redirect('/test')
db = db_session.create_session()
result = db.query(Result).filter(Result.is_finished == False).first()
question = db.query(ResultRow).filter(ResultRow.result == result,
ResultRow.q_id == q_id).first()
if not question:
return redirect('/test')
question.answer = answer
db.commit()
return redirect('/test')
@app.route("/finish_test")
@login_required
def finish_test():
if not test_started():
return redirect("/all_tests")
db = db_session.create_session()
st = db.query(Result).filter(~Result.is_finished).first()
if all(list(i.answer is None for i in st.rows)):
db.delete(st)
db.commit()
return redirect('/all_tests')
st.is_finished = True
st.end_date = datetime.datetime.now()
all_answers = list(a.answer == a.correct for a in st.rows)
n_cor = all_answers.count(True)
n_all = len(all_answers)
user = st.test.creator
text = f"{current_user.nickname} завершил(а) тест {st.test.name} ({n_cor}/{n_all})"
link = f"/statistics/{st.test_id}"
save_and_notify(db, user, text, link)
db.commit()
return redirect(f"/statistics/{st.test_id}".format(st.test_id))
@app.route('/more')
@login_required
def more():
try:
return render_template('more.html',
title='Дополнительно',
more='active')
except sa.orm.exc.DetachedInstanceError:
return redirect('/more')
if __name__ == '__main__':
main()