1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/dependent_funs.py

69 lines
2.3 KiB
Python
Raw Normal View History

from flask import flash, url_for
from flask_admin.menu import MenuLink
2021-06-02 21:43:41 +00:00
from flask_security import current_user
2022-05-08 19:26:25 +00:00
from . import app, db
from .models import Semester
2021-05-17 20:36:24 +00:00
2022-02-23 19:36:29 +00:00
def flashRandomPassword(password):
flash(f"Random password: {password}", category="warning")
2022-04-12 13:00:12 +00:00
def sortedSemestersStartingWithNewest():
return Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc())
def userActiveSemester(flashWarning=False):
2022-04-12 13:00:12 +00:00
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:
2021-07-14 02:25:34 +00:00
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")
2021-07-14 02:25:34 +00:00
return Semester.query.get(current_user.active_semester_id)
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")
def initActiveSemesterMenuLinks(space):
with app.app_context():
2022-03-02 00:56:42 +00:00
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")))
2021-11-30 00:36:19 +00:00
def reportBadAttempt(message):
print("BAD ATTEMPT:", message) # TODO: Log