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

Add set_old_semesters_done and set_done

This commit is contained in:
Mo 2022-09-19 19:11:08 +02:00
parent 7fe8024202
commit 77385c0861
2 changed files with 34 additions and 22 deletions

View file

@ -354,6 +354,11 @@ class SemesterView(SecureAdminModelView):
description="This option transfers assistants of your active semester to active experiments in the new semester. Make sure that your active semester is the last semester before creating a new one! Active experiments are transferred anyway. If you do not want an experiment to be transferred, set it to inactive before creating the new semester. Experiments which are switched to active before creating the new semester will be created in the new semester without assistants. It is recommended to check the assistants of all experiments after creating a new semester.", description="This option transfers assistants of your active semester to active experiments in the new semester. Make sure that your active semester is the last semester before creating a new one! Active experiments are transferred anyway. If you do not want an experiment to be transferred, set it to inactive before creating the new semester. Experiments which are switched to active before creating the new semester will be created in the new semester without assistants. It is recommended to check the assistants of all experiments after creating a new semester.",
default=True, default=True,
) )
set_old_semesters_done = BooleanField(
"Set all old semesters as done",
description="Setting semesters as done prevents assistants from changing or even seeing marks in these semesters. Choose this option only if all marks in all old semesters are already set.",
default=False,
)
can_delete = False can_delete = False
column_display_all_relations = True column_display_all_relations = True
@ -390,7 +395,7 @@ class SemesterView(SecureAdminModelView):
form_args = { form_args = {
"done": { "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." "description": "Setting a semester as done prevents assistants from changing or even seeing marks in this semester. 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."
}, },
} }
@ -405,30 +410,16 @@ class SemesterView(SecureAdminModelView):
def on_model_change(self, form, model, is_created): def on_model_change(self, form, model, is_created):
if is_created: if is_created:
if form.set_old_semesters_done.data:
last_semester = db.session.get(Semester, model.id - 1)
last_semester.set_done(model)
else:
current_user.active_semester = model current_user.active_semester = model
flash(f"Active semester changed to the new semester {model}!", "warning") flash(f"Active semester changed to the new semester {model}!", "warning")
else: else:
if model.done: if model.done:
next_semester = db.session.get(Semester, model.id + 1) next_semester = db.session.get(Semester, model.id + 1)
set_next_semester = next_semester is not None model.set_done(next_semester)
# 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(): def programQueryFactory():

View file

@ -577,6 +577,27 @@ class Semester(db.Model):
def lastSemester(): def lastSemester():
return Semester.sortedSemestersStartingWithNewest(limit=1).first() return Semester.sortedSemestersStartingWithNewest(limit=1).first()
def set_done(self, next_semester=None):
set_next_semester = next_semester is not None
# Set also all previous semesters as done
for id in range(1, self.id + 1):
semester = db.session.get(Semester, id)
if semester == self 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")
class ExperimentMark(db.Model): class ExperimentMark(db.Model):
# A mark for a student after a specific experiment # A mark for a student after a specific experiment