2021-04-01 12:08:42 +00:00
|
|
|
from random import choice
|
2021-06-02 21:43:41 +00:00
|
|
|
from string import ascii_letters, digits
|
|
|
|
|
2021-04-27 13:50:03 +00:00
|
|
|
from flask import flash
|
2021-06-02 21:43:41 +00:00
|
|
|
from flask_security import current_user
|
2021-04-26 19:31:15 +00:00
|
|
|
|
2021-04-27 21:28:47 +00:00
|
|
|
from advlabdb import db
|
2021-06-02 21:43:41 +00:00
|
|
|
from advlabdb.models import Semester
|
2021-03-19 12:41:52 +00:00
|
|
|
|
2021-05-17 20:36:24 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
def randomPassword():
|
2021-06-02 21:43:41 +00:00
|
|
|
return "".join(choice(ascii_letters + digits) for i in range(12))
|
2021-04-01 12:08:42 +00:00
|
|
|
|
|
|
|
|
2021-05-17 14:55:04 +00:00
|
|
|
def userActiveSemester(flashWarning=False):
|
2021-07-29 22:46:36 +00:00
|
|
|
lastSemesterId = (
|
|
|
|
Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc()).first().id
|
|
|
|
) # "WS" > "SS"
|
2021-04-26 19:31:15 +00:00
|
|
|
if current_user.active_semester_id is None:
|
|
|
|
current_user.active_semester_id = lastSemesterId
|
2021-04-27 13:50:03 +00:00
|
|
|
db.session.commit()
|
2021-04-26 19:31:15 +00:00
|
|
|
elif current_user.active_semester_id != lastSemesterId:
|
|
|
|
activeSemester = Semester.query.get(current_user.active_semester_id)
|
|
|
|
if activeSemester:
|
2021-05-17 14:55:04 +00:00
|
|
|
if flashWarning:
|
2021-07-14 02:25:34 +00:00
|
|
|
flash(f"You are in the old semester {activeSemester.repr()}!", "warning")
|
2021-04-26 19:31:15 +00:00
|
|
|
else:
|
|
|
|
current_user.active_semester_id = lastSemesterId
|
2021-04-27 13:50:03 +00:00
|
|
|
db.session.commit()
|
|
|
|
flash("Semester changed!", "warning")
|
|
|
|
|
2021-07-14 02:25:34 +00:00
|
|
|
return Semester.query.get(current_user.active_semester_id)
|
2021-05-18 12:54:30 +00:00
|
|
|
|
|
|
|
|
2021-06-25 23:59:21 +00:00
|
|
|
def setUserActiveSemester(semesterId):
|
2021-07-11 11:48:13 +00:00
|
|
|
if current_user.active_semester_id != semesterId:
|
2021-07-14 02:25:34 +00:00
|
|
|
semester = Semester.query.get(semesterId)
|
|
|
|
if semester:
|
2021-07-11 11:48:13 +00:00
|
|
|
try:
|
|
|
|
current_user.active_semester_id = semesterId
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), "error")
|
|
|
|
|
|
|
|
db.session.rollback()
|
|
|
|
else:
|
2021-07-14 02:25:34 +00:00
|
|
|
flash(f"Active semester changed to {semester.repr()}!", "warning")
|
2021-07-11 11:48:13 +00:00
|
|
|
else:
|
|
|
|
flash(f"No semester with ID {semesterId}!", "error")
|