Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
741 lines (653 loc) · 29 KB
/
Copy pathapp.py
File metadata and controls
741 lines (653 loc) · 29 KB
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#!/usr/bin/env python3
from collections import defaultdict
from logging.handlers import SMTPHandler
import base64
import binascii
import logging
import math
import re
import struct
import sys
import time
import click
from flask import Flask, render_template, session, redirect, url_for, request, g, flash, abort, request
from board.util import *
from board import teaminfo, materials, gradeupload, taskadmin, sshkeys, tasks, users, autograde, presentations, tutorials, eastereggs, booking, timesheet
try:
import config
except ImportError:
print("Please create a config.py before using the scoreboard!")
import sys
sys.exit(-1)
from Crypto.Cipher import DES
app = Flask(__name__)
app.config.from_object(config.Config)
app.register_blueprint(teaminfo.bp)
app.register_blueprint(materials.bp)
app.register_blueprint(gradeupload.bp)
app.register_blueprint(taskadmin.bp)
app.register_blueprint(sshkeys.bp)
app.register_blueprint(tasks.bp)
app.register_blueprint(users.bp)
app.register_blueprint(autograde.bp)
app.register_blueprint(presentations.bp)
app.register_blueprint(tutorials.bp)
app.register_blueprint(booking.bp)
app.register_blueprint(eastereggs.bp)
app.register_blueprint(timesheet.bp)
@click.group()
def cli():
pass
# Load plugins from configuration
menuitems = {"tutor": [], "admin": []}
for plugin_pkg in app.config["PLUGINS"]:
import importlib
plug = importlib.import_module("plugins." + plugin_pkg)
app.register_blueprint(plug.bp)
plug_menu = plug.get_menuitems()
menuitems["tutor"].extend(plug_menu.get("tutor", []))
menuitems["admin"].extend(plug_menu.get("admin", []))
register_cli = getattr(plug, "register_cli", None)
if register_cli:
register_cli(cli)
#from werkzeug.middleware.profiler import ProfilerMiddleware
#app.wsgi_app = ProfilerMiddleware(app.wsgi_app)
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
def datetime_filter(x):
if x:
return time.strftime("%d.%m.%Y %H:%M", time.localtime(x))
return ""
app.jinja_env.filters["datetime"] = datetime_filter
app.jinja_env.globals.update(
is_admin=is_admin,
is_tutor=is_tutor,
board_title=app.config["BOARD_TITLE"],
plugmenu=menuitems)
if app.config.get("EMAIL_BUGS", False) == True:
# Register SMTP handler to mail bugs to configured recipients
recipients = app.config.get("EMAIL_BUGS_TO")
sender = app.config.get("EMAIL_BUGS_FROM")
mailserver = app.config.get("EMAIL_BUGS_VIA")
credentials = app.config.get("EMAIL_BUGS_CREDENTIALS")
subject = app.config.get("EMAIL_BUGS_SUBJECT")
assert isinstance(recipients, list), "EMAIL_BUGS_TO must be a list of recipients"
assert isinstance(sender, str), "EMAIL_BUGS_FROM must contain the sender's email address"
assert isinstance(mailserver, str), "EMAIL_BUGS_VIA must contain the SMTP server's address"
assert credentials is None or len(credentials) == 2, "Invalid EMAIL_BUGS_CREDENTIALS"
assert isinstance(subject, str), "EMAIL_BUGS_SUBJECT must contain the email's subject line"
handler = SMTPHandler(
mailhost=mailserver,
fromaddr=sender,
toaddrs=recipients,
subject=subject,
credentials=credentials,
secure=tuple() # NB: TLS is always via STARTTLS, and only enabled if credentials are given
)
handler.setLevel(logging.ERROR)
handler.setFormatter(logging.Formatter(
"[%(asctime)s] %(levelname)s in %(module)s: %(message)s"
))
app.logger.addHandler(handler)
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.before_request
def check_maintenance():
if app.config["MAINTENANCE"] and request.endpoint != "maintenance" and not request.path.startswith("/static/"):
return redirect("/maintenance")
@app.before_request
def anti_csrf():
# These are the ones that according to RFC7231 mustn't modify state
if request.method in ["GET", "HEAD", "OPTIONS", "TRACE"]:
return
sec_fetch_site = request.headers.get("Sec-Fetch-Site", None)
if sec_fetch_site is None or sec_fetch_site == "same-origin":
return
abort(403)
if not app.config.get("DISABLE_ACTIVITY_LOG"):
@app.before_request
def log_activity():
if is_logged_in():
cur = get_db().cursor()
cur.execute("UPDATE users SET last_active = strftime('%s','now') WHERE id = ?", [session['user-id']])
@app.after_request
def set_security_headers(response):
response.headers["X-Frame-Options"] = "DENY"
response.headers["Content-Security-Policy"] = "frame-ancestors 'none'; script-src 'self'; object-src 'none'; form-action 'self';"
return response
@app.route("/maintenance")
def maintenance():
if not app.config["MAINTENANCE"]:
return redirect("/")
return render_template("maintenance.html")
@app.route("/")
def index():
if is_logged_in():
return redirect("/scoreboard")
return render_template("login.html", welcome_text=app.config["WELCOME_TEXT"])
@app.route("/scoreboard")
@login_required
def scoreboard():
# Replay submission log
cur = get_db().cursor()
#cur.execute("SELECT task_id, u.vorname || ' ' || u.nachname as user FROM flag_submissions LEFT JOIN users u ON u.id = user_id ORDER BY flag_time")
# Fetch number of tasks
cur.execute("SELECT task_id, task_short FROM tasks WHERE from_date < strftime('%s','now') ORDER BY order_num")
tasks = [r for r in cur.fetchall()]
cur.execute("SELECT s.task_id, s.team_id FROM flag_submissions s LEFT JOIN teams t ON s.team_id = t.team_id WHERE t.deleted IS NULL ORDER BY flag_time")
task_ctr = {k["task_id"]: 1 for k in tasks}
state = defaultdict(lambda: {k["task_id"]: 0 for k in tasks})
for r in cur.fetchall():
cur_state = state[r["team_id"]]
if cur_state[r["task_id"]] == 0:
cur_state[r["task_id"]] = task_ctr[r["task_id"]]
task_ctr[r["task_id"]] += 1
# Sort
sorted_state = sorted(
[(k, v, sum(v.values()), sum(1 for x in v.values() if x > 0)) for k, v in state.items()],
key=lambda x: (-x[3], x[2])
)
#print([(k,sum(v),v) for k,v in sorted_state])
teams = None
cur.execute("""SELECT
t.team_id,
CASE WHEN tt.teamname IS NULL THEN 'Team ' || tt.team_id ELSE tt.teamname END as teamname,
GROUP_CONCAT(u.vorname || ' ' || u.nachname, ', ') as member
FROM team_members t
LEFT JOIN users u ON u.id = t.member_id
LEFT JOIN teams tt ON t.team_id = tt.team_id
WHERE tt.deleted IS NUll
GROUP BY t.team_id""")
teams = {res['team_id']: res for res in cur.fetchall()}
show_names_in_scoreboard = is_admin() or app.config["SHOW_NAMES_TO_ALL"]
feedback_data = None
if "last-solved" in session:
task_id = session["last-solved"]
del session["last-solved"]
if "pending-feedback" in session:
session["pending-feedback"].append(task_id)
else:
session["pending-feedback"] = [task_id]
cur.execute("SELECT task_long FROM tasks WHERE task_id = ?", (task_id,))
res = cur.fetchone()
if res:
feedback_data = {
"task_long": res["task_long"],
"url": f"/tasks/{task_id}/feedback"
}
return render_template("scoreboard.html", scoreboard=sorted_state, teams=teams, tasks=tasks, show_names=show_names_in_scoreboard, feedback_data=feedback_data)
@app.route("/lookup")
@tutor_required
def lookup():
q = request.args.get("q")
if q is None:
return redirect("/404")
# Clear name
q = re.sub(r'\W*\[[^\]]*\]', "", q)
cur = get_db().cursor()
cur.execute("""SELECT tm.team_id FROM team_members tm LEFT JOIN users u ON u.id = tm.member_id WHERE u.vorname || ' ' || u.nachname LIKE :q OR u.displayname LIKE :q ORDER BY tm.team_id DESC LIMIT 1""", {"q": q})
row = cur.fetchone()
if row is None:
return redirect("/404")
return redirect(f"/teaminfo/{row[0]}")
@app.get("/logout")
def logout_template():
return render_template("logout.html")
@app.post("/logout")
@login_required
def logout():
if request.form.get("everywhere", False):
get_db().execute("update users set session_generation=session_generation+1 where id=?", [session['user-id']])
session.pop("user-id")
session.pop("permasession", None)
session.permanent = False
return redirect("/")
@app.route("/flag", methods=["POST"])
@login_required
def flag():
team_id = get_team_from_db()
if not team_id:
flash("You have to form a team with another student in order to submit flags!")
return redirect("/scoreboard")
flag = request.form["flag"].strip()
# Check for an easter egg
cur = get_db().cursor()
cur.execute("SELECT * FROM easteregg_flags WHERE flag = ?", (flag,))
if m := cur.fetchone():
# Updated accessed counter
cur.execute("UPDATE easteregg_flags SET counter=counter+1 WHERE easteregg_id=?", (m['easteregg_id'],))
# Redirect user to target
return redirect(m['link'])
fdec = check_flag(flag)
if fdec is not None:
cur = get_db().cursor()
cur.execute("SELECT user_id, team_id FROM flag_submissions WHERE flag=?", (flag,))
data = cur.fetchone()
# Check if task is already available
cur.execute("SELECT * FROM tasks WHERE task_id=?", [fdec['task_id']])
task_data = cur.fetchone()
if task_data['from_date'] > (fdec['ftime'] / 1e6):
flash("You are really fast! The task which created this flag hasn't even been released so far. Please wait until it started and submit a new flag then!")
return redirect("/scoreboard")
if data:
team = get_team_from_db()
if data["user_id"] == session["user-id"] or data["team_id"] == team:
# This was already submitted -- by the same team!
flash("You already submitted this flag!")
else:
log_event(session["user-id"], Category.STUDENT_INCIDENT, f"User {session['user-id']} on team {team} submitted flag {flag} for task {fdec['task_id']} previously submitted by user {data['user_id']} on team {data['team_id']}")
flash("We already got this flag submitted by another team! Flag sharing is forbidden!")
else:
cur.execute("INSERT INTO flag_submissions (flag, team_id, user_id, task_id, submission_time, flag_time) VALUES (?,?,?,?,strftime('%s','now'),?)", (flag, team_id, session["user-id"], fdec["task_id"], fdec["ftime"]))
flash("Looks like a flag! Congratz!", "success")
session["last-solved"] = fdec["task_id"]
else:
flash("This does not look like a flag :/")
return redirect("/scoreboard")
@app.route("/settings")
@login_required
def settings():
return render_template("settings.html")
@app.route("/flagcheck")
@tutor_required
def flagcheck():
flag = request.args.get("flag")
if flag:
flag = flag.strip()
cur = get_db().cursor()
cur.execute("SELECT * FROM easteregg_flags WHERE flag = ?", (flag,))
if m := cur.fetchone():
flash(f"Easteregg-Flag; Link: {m['link']}")
else:
fdec, fdetails = check_flag_details(flag)
if fdec is not None:
t = time.localtime(fdec['ftime'] // 1e6)
tstr = time.strftime('%Y-%m-%d %H:%M:%S', t)
flash(f"Valide Flag für Aufgabe {fdec['task_short']}, erstellt am {tstr}", category="success")
else:
flash(f"Keine valide Flag! Details: {fdetails}")
return render_template("flagcheck.html")
@app.route("/team/join", methods=["POST"])
@login_required
def team_join():
join_code = request.form["join_code"].strip()
try:
join_code_dec = base64.b32decode(join_code.replace("0","O") + "="*(len(join_code)%5))
except:
print(f"Join code: Base64 decoding failed: {join_code}")
flash("Ungültiger Join-Code!")
return redirect("/team")
cry = DES.new(key=app.config["JOIN_KEY"], mode=DES.MODE_ECB)
try:
mate_user_id, jtime = struct.unpack("<II", cry.decrypt(join_code_dec))
except struct.error:
print(f"Join code: Unpacking failed: {join_code}")
flash("Ungültiger Join-Code!")
return redirect("/team")
now = time.time()
# Check for outdated join code
if not now - app.config["JOIN_MINUTES_VALID"]*60 < jtime < now:
flash("Dein Join-Code ist bereits abgelaufen")
return redirect("/team")
# Check if we are about to join the team of a different user
if session["user-id"] == mate_user_id:
flash("Das ist dein eigener Join-Code! Bilde ein Team mit einem anderen Studenten!")
return redirect("/team")
# Check for mate existance in DB
mate_name = get_user_name(mate_user_id)
if mate_name is None:
flash("Ungültiger Join-Code!")
return redirect("/team")
if not create_team([mate_user_id, session["user-id"]]):
return redirect("/team")
flash("Du bist dem Team von {} beigetreten".format(mate_name), "success")
return redirect("/team")
@app.route("/team/singleteam", methods=["POST"])
@login_required
def create_one_person_team():
if not app.config["SINGLETEAMS_ALLOWED"]:
return redirect("/team")
create_team([session["user-id"]])
return redirect("/team")
@app.route("/team")
@login_required
def team():
team_id = get_team_from_db()
cur = get_db().cursor()
cur.execute("""
SELECT t.team_id, IIF(u.displayname IS NOT NULL, u.displayname, u.vorname || ' ' || u.nachname) as name, tt.teamname
FROM team_members t
LEFT JOIN users u ON u.id = t.member_id
LEFT JOIN teams tt ON t.team_id = tt.team_id
WHERE tt.team_id=? and tt.deleted is null""", (team_id, ))
res = cur.fetchall()
if res:
teamname = generate_teamname(res[0]["teamname"], res[0]['team_id'])
return render_template("team.html",
in_team=True, teamname=teamname,teammember=[x['name'] for x in res])
else:
cry = DES.new(key=app.config["JOIN_KEY"], mode=DES.MODE_ECB)
join_code = cry.encrypt(struct.pack("<II", session["user-id"], int(time.time())))
join_code = base64.b32encode(join_code).rstrip(b"=").decode()
return render_template("team.html",
in_team=False, join_code=join_code, minutes_valid=app.config["JOIN_MINUTES_VALID"], singleteams=app.config["SINGLETEAMS_ALLOWED"])
@app.route("/team/changename", methods=["POST"])
@login_required
def team_changename():
cur = get_db().cursor()
team_id = get_team_from_db()
new_name = request.form["teamname"]
if not re.match(r"^[A-z0-9 ]*$", new_name):
flash("Der neue Teamname darf nur alphanumerische Zeichen und Leerzeichen enthalten!")
return redirect("/team")
new_name = new_name.strip()
if len(new_name) == 0:
flash("Nur Leerzeichen im Namen oder ein leerer Name?! Sehr witzig, aber nein...")
return redirect("/team")
if len(new_name) > 32:
flash("Wow... Sehr kreativ, aber auch sehr lang! Die maximale Länge für Teamnamen beträgt 32 Zeichen!")
return redirect("/team")
m = re.match(r"Team (\d+)", new_name)
if m and int(m.group(1)) != team_id:
flash("Olalala... Sieht so aus als würdest du vorgeben wollen ein anderes Team zu. Mehr Glück beim nächsten Mal!")
return redirect("/team")
cur.execute("""SELECT COUNT(*) FROM teams WHERE teamname = ? AND deleted IS NULL""", [new_name])
if cur.fetchone()[0] > 0:
flash("Dieser Teamname ist leider schon vergeben. Bitte suche dir einen anderen aus!")
return redirect("/team")
cur.execute("UPDATE teams SET teamname =? WHERE team_id=?", [new_name, get_team_from_db()])
return redirect("/team")
@app.route("/comments")
@tutor_required
def comments():
# Find task/team combinations with comments other than grading entries
cur = get_db().cursor()
cur.execute("""
SELECT
a.*,
g.corrector AS last_corrector,
MAX(g.created_time) AS last_correction_time,
g.comment AS last_correction_text
FROM
(
SELECT
s.task_id,
s.team_id,
x.task_long,
t.teamname,
s.author AS last_comment_author,
MAX(s.created_time) AS last_comment_time,
s.text AS last_comment_text
FROM submission_comments s
LEFT OUTER JOIN teams t
ON
t.team_id = s.team_id
LEFT OUTER JOIN tasks x
ON
x.task_id = s.task_id
GROUP BY s.task_id, s.team_id
) a
LEFT OUTER JOIN task_grading g
ON
g.task_id = a.task_id AND
g.team_id = a.team_id AND
g.deleted_time IS NULL
GROUP BY a.task_id, a.team_id
""")
threads = []
for row in cur:
last_is_comment = row["last_comment_time"] > row["last_correction_time"]
last_event_text = row["last_comment_text"] if last_is_comment else row["last_correction_text"]
last_event_author = get_user_name(row["last_comment_author"]) if last_is_comment else row["last_corrector"]
last_event_time = row["last_comment_time"] if last_is_comment else row["last_correction_time"]
acted_on = (not last_is_comment) or row["last_comment_author"] not in get_team_members(row["team_id"]) # acted on: new grading upload, or a comment by a non-team-member.
threads.append({
"team_name": row["teamname"] if row["teamname"] is not None else f"Team {row['team_id']}",
"team_id": row["team_id"],
"task_long": row["task_long"],
"task_id": row["task_id"],
"last_comment": last_event_text,
"last_author": last_event_author,
"last_time": last_event_time,
"acted_on": acted_on
})
return render_template("comments.html", threads=threads)
@app.route("/comments/<int:team_id>/<int:task_id>", methods=["GET", "POST"])
@tutor_required
def comment_thread(team_id, task_id):
if request.method == "POST":
text = request.form.get("message", "").strip()
if not text:
flash("You didn't specify a message!")
else:
author = session["user-id"]
add_comment(task_id, team_id, author, text)
flash("Comment added", "success")
return redirect(f"/comments/{team_id}/{task_id}")
else:
comments = get_comments(task_id, team_id)
teammates = get_team_members(team_id)
cur = get_db().cursor()
cur.execute("SELECT teamname FROM teams WHERE team_id = ?", (team_id,))
res = cur.fetchone()
if not res:
flash("No such team")
return redirect("/comments")
team_name = res["teamname"] or f"Team {team_id}"
cur.execute("SELECT task_long FROM tasks WHERE task_id = ?", (task_id,))
res = cur.fetchone()
if not res:
flash("No such task")
return redirect("/comments")
task_long = res["task_long"] or f"Task {task_id}"
comments = sorted(({
**c,
"by_tutor": c["author"] not in teammates,
"own": c["author"],
"author": get_user_name(c["author"]) if isinstance(c["author"], int) else c["author"]
} for c in comments), key=lambda c: c["time"])
# TODO: Since this is for complaints, maybe add a link to their submission?
return render_template("comment_thread.html", comments=comments, team_id=team_id, task_id=task_id, team_name=team_name, task_long=task_long)
@app.route("/shib-login")
def login():
if "X-Remoteuser" not in request.headers:
flash("TUM-ID login broken; use alternative login instead")
return redirect("/alternative-login")
"""This URL is protected by the apache server. Just set a cookie so that
we can recognize the user"""
session["user-permanent-id"] = request.headers.get("X-Remoteuser")
session["user-matrikel"] = request.headers.get("X-Matrikel").encode("iso-8859-1").decode("utf-8")
session["user-vorname"] = request.headers.get("X-Givenname").encode("iso-8859-1").decode("utf-8")
session["user-name"] = request.headers.get("X-Sn").encode("iso-8859-1").decode("utf-8")
session["user-mail"] = request.headers.get("X-Mail")
uid = request.headers.get("X-Uid")
gender = request.headers.get("X-Gender")
session["user-displayname"] = request.headers.get("X-DisplayName").encode("iso-8859-1").decode("utf-8")
# Create user on demand
cur = get_db().cursor()
cur.execute("SELECT * FROM users WHERE permanent_id = ?", (session["user-permanent-id"],))
if not cur.fetchone():
cur.execute("INSERT INTO users (permanent_id, matrikel, vorname, nachname, uid, gender, role, email, displayname) VALUES (?,?,?,?,?,?,?,?,?)", (session["user-permanent-id"], session["user-matrikel"], session["user-vorname"], session["user-name"], uid, gender, 0, session['user-mail'], session["user-displayname"]))
else:
# In rare circumstances the first- and surname are changed in TUMOnline.
# Maybe also the mail address and display name change, update the database accordingly
cur.execute("UPDATE users SET vorname=?, nachname=?, gender=?, email=?, displayname=? WHERE permanent_id=? LIMIT 1", (session['user-vorname'], session['user-name'], gender, session['user-mail'], session["user-displayname"], session["user-permanent-id"],))
cur.execute("SELECT id, role, session_generation FROM users WHERE permanent_id = ?", (session["user-permanent-id"],))
res = cur.fetchone()
session["user-id"] = res["id"]
session["user-role"] = res["role"]
session["session-generation"] = res["session_generation"]
# Check if the user is on the list of allowed students (for seminars etc.)
if res["role"] not in TUTOR_ROLES and app.config["USER_ALLOWLIST_TABLE"] is not None:
# Can't parametrize the allowlist table name, so concatenation it is...
# This comes from the config so it shouldn't be injectable
cur.execute(f"SELECT * FROM {app.config['USER_ALLOWLIST_TABLE']} WHERE matrikel=?", [session["user-matrikel"]])
if not cur.fetchone():
session.pop("user-id")
return render_template("login-denied.html", contact_info=app.config["CONTACT_INFO"])
# do this last so that session becomes valid as late as possible, to guard against exceptions during login process
session["logged-in-until"] = time.time() + app.config["MAX_SESSION_SECONDS"]
return redirect_to_post_login_url()
@app.route("/alternative-login", methods=["POST", "GET"])
def alternative_login():
if request.method == "POST" and "email" in request.form and "password" in request.form:
# Try to login
cur = get_db().cursor()
cur.execute("SELECT * FROM users WHERE email=?", (request.form["email"],))
res = cur.fetchone()
if res and res["password"]:
salt, db_pw = res["password"].split(":")
salt = binascii.unhexlify(salt)
if pw_hash(request.form["password"].encode(), salt).hex() == db_pw:
session["user-name"] = res ["nachname"]
session["user-vorname"] = res["vorname"]
session["user-matrikel"] = res["matrikel"]
session["user-id"] = res["id"]
session["user-role"] = res["role"]
session["user-displayname"] = res["displayname"]
session["session-generation"] = res["session_generation"]
session["logged-in-until"] = time.time() + app.config["MAX_SESSION_SECONDS"]
return redirect_to_post_login_url()
else:
return redirect("/alternative-login")
else:
return redirect("/alternative-login")
else:
return render_template("alternative-login.html")
@app.route("/set-role/<int:role>")
@tutor_required
def set_role(role):
if role in ADMIN_ROLES and not is_admin():
return redirect("/")
if role in TUTOR_ROLES and not is_tutor():
return redirect("/")
session["user-role"] = role
return redirect("/")
@app.route("/impersonate/<int:user_id>")
@admin_required
def impersonate(user_id):
cur = get_db().cursor()
cur.execute("SELECT * FROM users WHERE id=?", (user_id,))
res = cur.fetchone()
session["user-id"] = res["id"]
session["user-role"] = res["role"]
session["user-matrikel"] = res["matrikel"]
session["user-vorname"] = res["vorname"]
session["user-name"] = res["nachname"]
session["user-displayname"] = res["displayname"]
session["session-generation"] = res["session_generation"]
return redirect("/scoreboard")
@app.route("/presenter-view")
@admin_required
def presenter_view():
# Drop privileges to normal user for demonstration purposes to not leak PII while screen sharing etc.
session["user-role"] = 0
# Hide the user's PII too
session["user-matrikel"] = "hidden"
return redirect("/scoreboard")
@app.get("/permasession")
@tutor_required
def permasession_get():
return render_template("permasession.html", state="perm" if session.permanent else "nonperm", lifetime=app.permanent_session_lifetime)
@app.post("/permasession")
@tutor_required
def permasession_post():
if "makeperm" in request.form:
perm = True
elif "makenonperm" in request.form:
perm = False
else:
return "Neither makeperm nor makenonperm", 400
session.permanent = perm
session["permasession"] = perm
return render_template("permasession.html", state="perm-freshly" if perm else "nonperm-freshly", lifetime=app.permanent_session_lifetime)
@cli.command()
def create_db():
init_db()
set_teamidoffset(app)
@cli.command()
def upgrade_db():
init_db()
@cli.command()
@click.argument("email")
@click.argument("firstname")
@click.argument("name")
@click.option("--matrikel", "-m")
def create_user(email, firstname, name, matrikel):
import getpass
pw = getpass.getpass()
add_user(email, firstname, name, matrikel, pw)
@cli.command()
@click.argument("task")
@click.option("-d", "--due")
def get_newest_submissions(task, due):
cur = get_db().cursor()
if due:
dt = time.strptime(due,"%Y-%m-%d-%H-%M")
dts = int(time.mktime(dt))
cur.execute("""
SELECT ts.task_id, MAX(ts.submission_time) as submission_time, ts.filepath, ts.original_name
FROM task_submissions ts
LEFT JOIN tasks t ON t.task_id = ts.task_id
WHERE t.task_short=? and ts.submission_time < ?
GROUP BY ts.task_id,ts.team_id
""", (task,dts))
else:
cur.execute("""
SELECT ts.task_id, MAX(ts.submission_time) as submission_time, ts.filepath, ts.original_name
FROM task_submissions ts
LEFT JOIN tasks t ON t.task_id = ts.task_id
WHERE t.task_short=?
GROUP BY ts.task_id,ts.team_id
""", (task,))
for r in cur.fetchall():
print(r["filepath"])
@cli.command()
#@click.argument("filename")
def export_grading():
if current_app.config["PRESENTATIONS_PER_USER"]:
pres_per_user_query = "SELECT team_id as user_id, SUM(successful) as presentations FROM task_presentations GROUP BY team_id"
else:
pres_per_user_query = "SELECT m.member_id as user_id, SUM(successful) as presentations FROM task_presentations p LEFT JOIN team_members m ON m.team_id=p.team_id GROUP BY m.member_id"
cur = get_db().cursor()
cur.execute(f"""SELECT u.vorname, u.nachname, u.matrikel,SUM(g.points) as points
FROM task_grading g
LEFT JOIN team_members m ON m.team_id = g.team_id
LEFT JOIN users u ON m.member_id = u.id
LEFT JOIN ({pres_per_user_query}) p ON p.user_id = u.id
WHERE g.deleted_time IS NULL AND u.matrikel IS NOT NULL AND u.matrikel != "(null)" AND p.presentations >=1
GROUP BY u.id""")
print("matrikel;problem;subproblem;credit")
for u in cur.fetchall():
pointbonus = math.ceil(((min(u['points'],28.5) / 28.5) * 10)*2) / 2
# pointbonus = min(11.5, pointbonus)
if pointbonus < 0.001:
continue
print(";".join(map(str, [u['matrikel'], "1", "1", pointbonus])))
@cli.command()
@click.argument("taskshort")
@click.argument("taskname")
@click.argument("flaguid")
@click.argument("starttime")
@click.argument("deadline")
@click.argument("submission_expected")
@click.argument("order_num")
def add_task(taskshort, taskname, flaguid, starttime, deadline, submission_expected, order_num):
import time
submission_expected = (submission_expected == "1")
dt = time.strptime(deadline,"%Y-%m-%d-%H-%M")
dts = int(time.mktime(dt))
st = time.strptime(starttime,"%Y-%m-%d-%H-%M")
sts = int(time.mktime(st))
order_num = int(order_num)
print("Creating task {} Short: {} Deadline: {} Submission: {}".format(taskname, taskshort, dts, submission_expected))
cur = get_db().cursor()
cur.execute("""INSERT INTO tasks (task_short, task_desc, flag_uid, from_date, due_date, needed, order_num) values (?, ?, ?, ?, ?, ?, ?)""", (taskshort, taskname, flaguid, sts, dts, submission_expected, order_num))
if __name__ == "__main__":
with app.app_context():
cli()