mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Remove Appointment.customInit
This commit is contained in:
parent
2e6151d05f
commit
9ff2405947
4 changed files with 36 additions and 63 deletions
|
@ -224,7 +224,8 @@ class UserView(SecureAdminModelView):
|
||||||
self.on_model_change(form, model, True)
|
self.on_model_change(form, model, True)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
flash(str(ex), "error")
|
if not self.handle_view_exception(ex):
|
||||||
|
flash(str(ex), "error")
|
||||||
|
|
||||||
self.session.rollback()
|
self.session.rollback()
|
||||||
else:
|
else:
|
||||||
|
@ -1059,15 +1060,12 @@ class GroupExperimentView(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:
|
||||||
for date, special, assistant in zip(
|
for date, special, assistant in (
|
||||||
(form.appointment1_date.data, form.appointment2_date.data),
|
(form.appointment1_date.data, form.appointment1_special.data, form.appointment1_assistant.data),
|
||||||
(form.appointment1_special.data, form.appointment2_special.data),
|
(form.appointment2_date.data, form.appointment2_special.data, form.appointment2_assistant.data),
|
||||||
(form.appointment1_assistant.data, form.appointment2_assistant.data),
|
|
||||||
):
|
):
|
||||||
if date:
|
if date is not None:
|
||||||
appointment = Appointment.customInit(
|
appointment = Appointment(date=date, special=special, group_experiment=model, assistant=assistant)
|
||||||
date=date, special=special, group_experiment=model, assistant=assistant
|
|
||||||
)
|
|
||||||
|
|
||||||
self.session.add(appointment)
|
self.session.add(appointment)
|
||||||
|
|
||||||
|
@ -1180,30 +1178,17 @@ class AppointmentView(SecureAdminModelView):
|
||||||
)
|
)
|
||||||
|
|
||||||
def customCreateModel(self, form):
|
def customCreateModel(self, form):
|
||||||
return Appointment.customInit(
|
return Appointment(
|
||||||
date=form.date.data,
|
date=form.date.data,
|
||||||
special=form.special.data,
|
special=form.special.data,
|
||||||
group_experiment=form.group_experiment.data,
|
group_experiment=form.group_experiment.data,
|
||||||
assistant=form.assistant.data,
|
assistant=form.assistant.data,
|
||||||
)
|
)
|
||||||
|
|
||||||
def update_model(self, form, model):
|
def on_model_change(self, form, model, is_created):
|
||||||
if None in (form.date, form.special): # For editables
|
if None not in (form.date, form.special): # No need to check or adjust assistant for editables
|
||||||
return super().update_model(form, model)
|
if not is_created:
|
||||||
|
model.assistant = Appointment.checkAndGetAssistant(model.group_experiment, model.assistant)
|
||||||
try:
|
|
||||||
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
|
|
||||||
|
|
||||||
self.on_model_change(form, model, False)
|
|
||||||
self.session.commit()
|
|
||||||
except Exception as ex:
|
|
||||||
flash(str(ex), "error")
|
|
||||||
|
|
||||||
self.session.rollback()
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
self.after_model_change(form, model, False)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class ExperimentMarkView(SecureAdminModelView):
|
class ExperimentMarkView(SecureAdminModelView):
|
||||||
|
@ -1362,16 +1347,16 @@ class ExperimentMarkView(SecureAdminModelView):
|
||||||
return ExperimentMark(part_student=form.part_student.data, group_experiment=form.group_experiment.data)
|
return ExperimentMark(part_student=form.part_student.data, group_experiment=form.group_experiment.data)
|
||||||
|
|
||||||
def update_model(self, form, model):
|
def update_model(self, form, model):
|
||||||
if (form.oral_mark and form.oral_mark.data != model.oral_mark) or (
|
if (form.oral_mark is not None and form.oral_mark.data != model.oral_mark) or (
|
||||||
form.protocol_mark and form.protocol_mark.data != model.protocol_mark
|
form.protocol_mark is not None and form.protocol_mark.data != model.protocol_mark
|
||||||
):
|
):
|
||||||
model.admin = current_user.admin
|
model.admin = current_user.admin
|
||||||
|
|
||||||
ret = super().update_model(form, model)
|
updateSuccessful = super().update_model(form, model)
|
||||||
|
|
||||||
model.part_student.checkThenSetFinalPartMark()
|
model.part_student.checkThenSetFinalPartMark()
|
||||||
|
|
||||||
return ret
|
return updateSuccessful
|
||||||
|
|
||||||
# Nothing changed
|
# Nothing changed
|
||||||
return True
|
return True
|
||||||
|
@ -1421,8 +1406,9 @@ class ImportView(SecureAdminBaseView):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
importFromFile(filePath)
|
importFromFile(filePath)
|
||||||
except Exception as exc:
|
except Exception as ex:
|
||||||
flash(str(exc), "error")
|
if not self.handle_view_exception(ex):
|
||||||
|
flash(str(ex), "error")
|
||||||
|
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
|
|
|
@ -257,12 +257,12 @@ def importFromFile(filePath):
|
||||||
assistantEmail = appointments["assistant_email"][i]
|
assistantEmail = appointments["assistant_email"][i]
|
||||||
assistant = Assistant.query.filter(Assistant.user.has(User.email == assistantEmail)).first()
|
assistant = Assistant.query.filter(Assistant.user.has(User.email == assistantEmail)).first()
|
||||||
|
|
||||||
if not assistant:
|
if assistant is None:
|
||||||
raise DataBaseImportException(
|
raise DataBaseImportException(
|
||||||
f"Assistant with email {email} does not exist in the database! Please make sure that you create assistants in the web interface."
|
f"Assistant with email {email} does not exist in the database! Please make sure that you create assistants in the web interface."
|
||||||
)
|
)
|
||||||
|
|
||||||
dbAppointment = Appointment.customInit(
|
dbAppointment = Appointment(
|
||||||
date=datetime.strptime(date, "%d.%m.%Y").date(),
|
date=datetime.strptime(date, "%d.%m.%Y").date(),
|
||||||
special=bool(int(appointments["special"][i])),
|
special=bool(int(appointments["special"][i])),
|
||||||
group_experiment=dbGroupExperiments[int(appointments["group_experiment_id"][i])],
|
group_experiment=dbGroupExperiments[int(appointments["group_experiment_id"][i])],
|
||||||
|
|
|
@ -84,12 +84,6 @@ class PartStudent(db.Model):
|
||||||
|
|
||||||
super().__init__(student=student, part=part, group=group)
|
super().__init__(student=student, part=part, group=group)
|
||||||
|
|
||||||
def customUpdate(self, group, final_part_mark):
|
|
||||||
Part.check(group, self.part)
|
|
||||||
|
|
||||||
self.group = group
|
|
||||||
self.final_part_mark = final_part_mark
|
|
||||||
|
|
||||||
def checkThenSetFinalPartMark(self):
|
def checkThenSetFinalPartMark(self):
|
||||||
finalWeightingSum = 0
|
finalWeightingSum = 0
|
||||||
finalMark = 0
|
finalMark = 0
|
||||||
|
@ -189,11 +183,6 @@ class Group(db.Model):
|
||||||
semester=semester,
|
semester=semester,
|
||||||
)
|
)
|
||||||
|
|
||||||
def customUpdate(self, part_students):
|
|
||||||
Group.check(part_students, self.program)
|
|
||||||
|
|
||||||
self.part_students = part_students
|
|
||||||
|
|
||||||
def repr(self):
|
def repr(self):
|
||||||
return f"{self.number} {self.program.repr()} {self.semester.repr()}"
|
return f"{self.number} {self.program.repr()} {self.semester.repr()}"
|
||||||
|
|
||||||
|
@ -213,7 +202,7 @@ class GroupExperiment(db.Model):
|
||||||
|
|
||||||
__table_args__ = (db.UniqueConstraint(semester_experiment_id, group_id),)
|
__table_args__ = (db.UniqueConstraint(semester_experiment_id, group_id),)
|
||||||
|
|
||||||
def __init__(self, semester_experiment, group):
|
def check(semester_experiment, group):
|
||||||
for partStudent in group.part_students:
|
for partStudent in group.part_students:
|
||||||
student = partStudent.student
|
student = partStudent.student
|
||||||
for partStudent in student.part_students:
|
for partStudent in student.part_students:
|
||||||
|
@ -223,6 +212,9 @@ class GroupExperiment(db.Model):
|
||||||
f"{student} has already done {semester_experiment.experiment} in {partStudent.part} and had {experimentMark}!"
|
f"{student} has already done {semester_experiment.experiment} in {partStudent.part} and had {experimentMark}!"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, semester_experiment, group):
|
||||||
|
GroupExperiment.check(semester_experiment, group)
|
||||||
|
|
||||||
super().__init__(semester_experiment=semester_experiment, group=group)
|
super().__init__(semester_experiment=semester_experiment, group=group)
|
||||||
|
|
||||||
for partStudent in group.part_students:
|
for partStudent in group.part_students:
|
||||||
|
@ -369,14 +361,14 @@ class Appointment(db.Model):
|
||||||
group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False)
|
group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False)
|
||||||
assistant_id = db.Column(db.Integer, db.ForeignKey("assistant.id"), nullable=False)
|
assistant_id = db.Column(db.Integer, db.ForeignKey("assistant.id"), nullable=False)
|
||||||
|
|
||||||
def checkAndGetAssistant(assistant, groupExperiment):
|
def checkAndGetAssistant(groupExperiment, assistant=None):
|
||||||
semesterExperiment = groupExperiment.semester_experiment
|
semesterExperiment = groupExperiment.semester_experiment
|
||||||
semesterExperimentAssistants = semesterExperiment.assistants
|
semesterExperimentAssistants = semesterExperiment.assistants
|
||||||
|
|
||||||
if not semesterExperimentAssistants:
|
if not semesterExperimentAssistants:
|
||||||
raise DataBaseException(f"{semesterExperiment} does not have assistants yet!")
|
raise DataBaseException(f"{semesterExperiment} does not have assistants yet!")
|
||||||
|
|
||||||
if assistant:
|
if assistant is not None:
|
||||||
if assistant not in semesterExperimentAssistants:
|
if assistant not in semesterExperimentAssistants:
|
||||||
raise DataBaseException(f"{assistant} not responsible for {semesterExperiment}!")
|
raise DataBaseException(f"{assistant} not responsible for {semesterExperiment}!")
|
||||||
else:
|
else:
|
||||||
|
@ -389,24 +381,16 @@ class Appointment(db.Model):
|
||||||
|
|
||||||
return assistant
|
return assistant
|
||||||
|
|
||||||
def customInit(date, special, group_experiment, assistant):
|
def __init__(self, date, special, group_experiment, assistant=None):
|
||||||
assistant = Appointment.checkAndGetAssistant(assistant, group_experiment)
|
assistant = Appointment.checkAndGetAssistant(group_experiment, assistant)
|
||||||
|
|
||||||
return Appointment(
|
super().__init__(
|
||||||
date=date,
|
date=date,
|
||||||
special=special,
|
special=special,
|
||||||
group_experiment=group_experiment,
|
group_experiment=group_experiment,
|
||||||
assistant=assistant,
|
assistant=assistant,
|
||||||
)
|
)
|
||||||
|
|
||||||
def customUpdate(self, date, special, assistant, group_experiment):
|
|
||||||
assistant = Appointment.checkAndGetAssistant(assistant, group_experiment)
|
|
||||||
|
|
||||||
self.date = date
|
|
||||||
self.special = special
|
|
||||||
self.assistant = assistant
|
|
||||||
self.group_experiment = group_experiment
|
|
||||||
|
|
||||||
def repr(self):
|
def repr(self):
|
||||||
return f"{self.date} {self.group_experiment.repr()}"
|
return f"{self.date} {self.group_experiment.repr()}"
|
||||||
|
|
||||||
|
@ -528,13 +512,16 @@ class ExperimentMark(db.Model):
|
||||||
|
|
||||||
__table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),)
|
__table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),)
|
||||||
|
|
||||||
def __init__(self, part_student, group_experiment):
|
def check(part_student, group_experiment):
|
||||||
if not part_student.group:
|
if not part_student.group:
|
||||||
raise DataBaseException("The part student does not have a group yet!")
|
raise DataBaseException("The part student does not have a group yet!")
|
||||||
else:
|
else:
|
||||||
if group_experiment not in part_student.group.group_experiments:
|
if group_experiment not in part_student.group.group_experiments:
|
||||||
raise DataBaseException("The group of the part student does not have the given group experiment!")
|
raise DataBaseException("The group of the part student does not have the given group experiment!")
|
||||||
|
|
||||||
|
def __init__(self, part_student, group_experiment):
|
||||||
|
ExperimentMark(part_student, group_experiment)
|
||||||
|
|
||||||
super().__init__(part_student=part_student, group_experiment=group_experiment)
|
super().__init__(part_student=part_student, group_experiment=group_experiment)
|
||||||
|
|
||||||
def updateFinalExperimentMark(self):
|
def updateFinalExperimentMark(self):
|
||||||
|
|
|
@ -139,8 +139,8 @@ def main():
|
||||||
db.session.add(as1)
|
db.session.add(as1)
|
||||||
db.session.add(as2)
|
db.session.add(as2)
|
||||||
|
|
||||||
ap1 = Appointment.customInit(date=date(2021, 3, 21), special=True, group_experiment=gx1, assistant=as1)
|
ap1 = Appointment(date=date(2021, 3, 21), special=True, group_experiment=gx1, assistant=as1)
|
||||||
ap2 = Appointment.customInit(date=date(2021, 3, 22), special=True, group_experiment=gx2, assistant=as2)
|
ap2 = Appointment(date=date(2021, 3, 22), special=True, group_experiment=gx2, assistant=as2)
|
||||||
|
|
||||||
db.session.add(ap1)
|
db.session.add(ap1)
|
||||||
db.session.add(ap2)
|
db.session.add(ap2)
|
||||||
|
|
Loading…
Reference in a new issue