mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Move setActiveSemester
This commit is contained in:
parent
f99354ba53
commit
1141d1dfb9
4 changed files with 19 additions and 25 deletions
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue