mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
from flask import flash, url_for
|
|
from flask_admin.menu import MenuLink
|
|
from flask_security import current_user
|
|
|
|
from advlabdb import app, db
|
|
from advlabdb.models import Semester
|
|
|
|
|
|
def flashRandomPassword(password):
|
|
flash(f"Random password: {password}", category="warning")
|
|
|
|
|
|
def sortedSemestersStartingWithNewest():
|
|
return Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc())
|
|
|
|
|
|
def userActiveSemester(flashWarning=False):
|
|
lastSemesterId = sortedSemestersStartingWithNewest().first().id # "WS" > "SS"
|
|
if current_user.active_semester_id is None:
|
|
current_user.active_semester_id = lastSemesterId
|
|
db.session.commit()
|
|
elif current_user.active_semester_id != lastSemesterId:
|
|
activeSemester = Semester.query.get(current_user.active_semester_id)
|
|
if activeSemester:
|
|
if flashWarning:
|
|
flash(f"You are in the old semester {activeSemester.repr()}!", "warning")
|
|
else:
|
|
current_user.active_semester_id = lastSemesterId
|
|
db.session.commit()
|
|
flash("Semester changed!", "warning")
|
|
|
|
return Semester.query.get(current_user.active_semester_id)
|
|
|
|
|
|
def setUserActiveSemester(semesterId):
|
|
if current_user.active_semester_id != semesterId:
|
|
semester = Semester.query.get(semesterId)
|
|
if semester:
|
|
try:
|
|
current_user.active_semester_id = semesterId
|
|
db.session.commit()
|
|
except Exception as ex:
|
|
flash(str(ex), "error")
|
|
|
|
db.session.rollback()
|
|
else:
|
|
flash(f"Active semester changed to {semester.repr()}!", "warning")
|
|
else:
|
|
flash(f"No semester with ID {semesterId}!", "error")
|
|
|
|
|
|
def initActiveSemesterMenuLinks(space):
|
|
with app.app_context():
|
|
semesters = Semester.query.order_by(Semester.id.desc())
|
|
for semester in semesters:
|
|
space.add_link(
|
|
MenuLink(
|
|
name=semester.repr(),
|
|
url=url_for("set_semester") + "?semester_id=" + str(semester.id),
|
|
category="Active semester",
|
|
)
|
|
)
|
|
|
|
space.add_link(MenuLink(name="Logout", url=url_for("security.logout")))
|
|
|
|
|
|
def reportBadAttempt(message):
|
|
print("BAD ATTEMPT:", message) # TODO: Log
|