diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index 521caaa..c55f6a2 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -42,7 +42,6 @@ from .exceptions import DataBaseException, ModelViewException from .model_dependent_funs import ( initActiveSemesterMenuLinks, lastSemester, - setUserActiveSemester, sortedSemestersStartingWithNewest, ) from .model_independent_funs import randomPassword @@ -347,7 +346,7 @@ class SemesterView(SecureAdminModelView): category._children.insert(0, link) def after_model_change(self, form, model, is_created): - setUserActiveSemester(model) + current_user.setActiveSemester(model) SemesterView.addMenuLink(adminSpace, model) SemesterView.addMenuLink(assistantSpace, model) diff --git a/advlabdb/model_dependent_funs.py b/advlabdb/model_dependent_funs.py index 46f904a..e134dca 100644 --- a/advlabdb/model_dependent_funs.py +++ b/advlabdb/model_dependent_funs.py @@ -24,23 +24,6 @@ def lastSemester(): return sortedSemestersStartingWithNewest(limit=1).first() -def setUserActiveSemester(semester): - if semester is None: - flash(f"Can not change the active semester to None!", "error") - return - - if current_user.active_semester != semester: - try: - current_user.active_semester = semester - db.session.commit() - except Exception as ex: - flash(str(ex), "error") - - db.session.rollback() - else: - flash(f"Active semester changed to {semester.repr()}!", "warning") - - def initActiveSemesterMenuLinks(space): with app.app_context(): try: diff --git a/advlabdb/models.py b/advlabdb/models.py index 676d1b7..c3c4e45 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -586,6 +586,22 @@ class User(db.Model, FsUserMixin): admin = db.relationship("Admin", back_populates="user", lazy=False, uselist=False) assistant = db.relationship("Assistant", back_populates="user", lazy=True, uselist=False) + def setActiveSemester(semester): + if semester is None: + flash(f"Can not change the active semester to None!", "error") + return + + if self.active_semester != semester: + try: + self.active_semester = semester + db.session.commit() + except Exception as ex: + flash(str(ex), "error") + + db.session.rollback() + else: + flash(f"Active semester changed to {semester.repr()}!", "warning") + def repr(self): return f"{self.first_name} {self.last_name}" diff --git a/advlabdb/routes.py b/advlabdb/routes.py index c4cb308..fbbb404 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -2,11 +2,7 @@ from flask import flash, redirect, render_template, request, url_for from flask_security import auth_required, current_user, roles_accepted, roles_required from . import app, db -from .model_dependent_funs import ( - active_semester_repr, - lastSemester, - setUserActiveSemester, -) +from .model_dependent_funs import active_semester_repr, lastSemester from .models import Semester @@ -34,7 +30,7 @@ def set_semester(): flash(str(ex), "error") else: semester = db.session.get(Semester, semesterId) - setUserActiveSemester(semester) + current_user.setActiveSemester(semester) red = request.referrer or url_for("index") return redirect(red)