1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-04 22:40:30 +00:00

Implemented semester done logic

This commit is contained in:
Mo 2022-09-17 15:55:49 +02:00
parent d18579059a
commit a2162363b9

View file

@ -128,7 +128,7 @@ class UserView(SecureAdminModelView):
@staticmethod
def semesterQueryFactory():
return Semester.query.order_by(Semester.id.desc())
return Semester.query.order_by(Semester.id.desc()).where(Semester.done == False)
@staticmethod
def default_roles():
@ -288,6 +288,8 @@ class UserView(SecureAdminModelView):
f"{model.email} registered with role(s): {', '.join(role.name for role in model.roles)}.",
category="success",
)
else:
flash(f"Active semester is {model.active_semester}.", "warning")
class SemesterView(SecureAdminModelView):
@ -331,13 +333,13 @@ class SemesterView(SecureAdminModelView):
default=True,
)
can_edit = False
can_delete = False
column_display_all_relations = True
column_list = [
"label",
"year",
"done",
"parts",
]
column_searchable_list = [
@ -345,8 +347,11 @@ class SemesterView(SecureAdminModelView):
"year",
]
column_default_sort = [
("year", True),
("label", True),
("id", True),
]
form_columns = [
"done",
]
column_formatters = {
@ -361,6 +366,12 @@ class SemesterView(SecureAdminModelView):
"groups": str_without_semester_formatter,
}
form_args = {
"done": {
"description": "Marking a semester as done does not let assistants work in the semester anymore. This is useful if all marks are already set and prevents assistants from changing or even seeing them after the semester is marked as done. Setting a semester as done sets older semesters as done, too. Only set the semester as done if all marks in the semester and all previous semesters are already set."
},
}
def customCreateModel(self, form):
return Semester.initFromOldSemester(
label=form.label.data,
@ -370,8 +381,32 @@ class SemesterView(SecureAdminModelView):
transferAssistants=form.transfer_assistants.data,
)
def after_model_change(self, form, model, is_created):
current_user.setActiveSemester(model)
def on_model_change(self, form, model, is_created):
if is_created:
current_user.active_semester = model
flash(f"Active semester changed to the new semester {model}!", "warning")
else:
if model.done:
next_semester = db.session.get(Semester, model.id + 1)
set_next_semester = next_semester is not None
# Set all previous semesters as done
for id in range(1, model.id + 1):
semester = db.session.get(Semester, id)
if semester == model or not semester.done:
semester.done = True
if set_next_semester:
# Set active_semester to next_semester
users_in_semester_done = db.session.execute(
select(User).where(User.active_semester == semester)
).scalars()
for user in users_in_semester_done:
user.active_semester = next_semester
if user == current_user:
flash(f"Active semester changed to the next semester {next_semester}!", "warning")
def programQueryFactory():