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

Add set_last_semester_as_active

This commit is contained in:
Mo 2022-09-19 15:19:25 +02:00
parent 8020762261
commit c0a4f215e8
2 changed files with 27 additions and 16 deletions

View file

@ -683,6 +683,32 @@ class User(db.Model, FsUserMixin):
admin = db.relationship("Admin", back_populates="user", lazy=False, uselist=False) admin = db.relationship("Admin", back_populates="user", lazy=False, uselist=False)
assistant = db.relationship("Assistant", back_populates="user", lazy=True, uselist=False) assistant = db.relationship("Assistant", back_populates="user", lazy=True, uselist=False)
def set_last_semester_as_active(self):
"""
Return True if changed, False otherwise.
"""
last_semester = Semester.lastSemester()
if last_semester.done:
return False
try:
self.active_semester = last_semester
db.session.commit()
except Exception as ex:
flash(str(ex), "error")
db.session.rollback()
return False
else:
flash(
f"Active semester changed to {last_semester} because your last active semester was marked as done!",
"warning",
)
return True
def str(self): def str(self):
return f"{self.first_name} {self.last_name}" return f"{self.first_name} {self.last_name}"

View file

@ -36,22 +36,7 @@ def post_login():
current_active_semester = current_user.active_semester current_active_semester = current_user.active_semester
if current_active_semester.done: if current_active_semester.done:
last_semester = Semester.lastSemester() current_user.set_last_semester_as_active()
if not last_semester.done:
try:
current_user.active_semester = last_semester
db.session.commit()
except Exception as ex:
flash(str(ex), "error")
db.session.rollback()
else:
flash(
f"Active semester changed to {last_semester} because the semester {current_active_semester} was marked as done!",
"warning",
)
if current_user.has_role("admin"): if current_user.has_role("admin"):
endpoint_base = "admin" endpoint_base = "admin"