diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index de8762d..7d36548 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -287,14 +287,7 @@ class GroupView(SecureModelView): def create_model(self, form): try: - orderedPartGroups = Group.query.filter(Group.part == form.part.data).order_by(Group.number) - lastTakenGroupNumber = orderedPartGroups[-1].number if orderedPartGroups.count() > 0 else 0 - - model = Group( - number=lastTakenGroupNumber + 1, - part_students=form.part_students.data, - part=form.part.data, - ) + model = Group.customInit(form.part.data, form.part_students.data) self.session.add(model) self.on_model_change(form, model, True) @@ -416,35 +409,17 @@ class GroupExperimentView(SecureModelView): def create_model(self, form): try: - model = GroupExperiment.checkAndInit( - semester_experiment=form.semester_experiment.data, group=form.group.data - ) + model = GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data) self.session.add(model) - for appointmentDate, special, assistant in zip( + for date, special, assistant in zip( [form.appointment1_date.data, form.appointment2_date.data], [form.appointment1_special.data, form.appointment2_special.data], [form.appointment1_assistant.data, form.appointment2_assistant.data], ): - if appointmentDate: - if assistant: - if assistant not in form.semester_experiment.data.assistants: - raise Exception(f"{assistant} not responsible for {form.semester_experiment.data}!") - else: - if len(form.semester_experiment.data.assistants) != 1: - raise Exception( - f"Experiment {form.semester_experiment.data} has more than one assistant. You have to assign one of these assistants: {form.semester_experiment.data.assistants}" - ) - - assistant = form.semester_experiment.data.assistants[0] - - appointment = Appointment( - date=appointmentDate, - special=special, - group_experiment=model, - assistant=assistant, - ) + if date: + appointment = Appointment.customInit(date, special, assistant, model) self.session.add(appointment) diff --git a/advlabdb/models.py b/advlabdb/models.py index f141ff0..d34ab8e 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -69,6 +69,16 @@ class Group(db.Model): __table_args__ = (db.UniqueConstraint(number, part_id),) + def customInit(part, partStudents): + orderedPartGroups = Group.query.filter(Group.part == part).order_by(Group.number) + lastTakenGroupNumber = orderedPartGroups[-1].number if orderedPartGroups.count() > 0 else 0 + + return Group( + number=lastTakenGroupNumber + 1, + part_students=partStudents, + part=part, + ) + def repr(self): return f"{self.number} {self.part.repr()}" @@ -86,13 +96,7 @@ class GroupExperiment(db.Model): __table_args__ = (db.UniqueConstraint(semester_experiment_id, group_id),) - def repr(self): - return f"SemExp {self.semester_experiment.repr()}; Gr {self.group.repr()}" - - def __repr__(self): - return f"" - - def checkAndInit(semester_experiment, group): + def customInit(semester_experiment, group): for partStudent in group.part_students: student = partStudent.student for partStudent in student.part_students: @@ -104,6 +108,12 @@ class GroupExperiment(db.Model): return GroupExperiment(semester_experiment=semester_experiment, group=group) + def repr(self): + return f"SemExp {self.semester_experiment.repr()}; Gr {self.group.repr()}" + + def __repr__(self): + return f"" + class Experiment(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -181,6 +191,31 @@ class Appointment(db.Model): 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) + def customInit(date, special, assistant, groupExperiment): + semesterExperiment = groupExperiment.semester_experiment + semesterExperimentAssistants = semesterExperiment.assistants + + if not semesterExperimentAssistants: + raise Exception(f"{semesterExperiment} has no assistants yet!") + + if assistant: + if assistant not in semesterExperimentAssistants: + raise Exception(f"{assistant} not responsible for {semesterExperiment}!") + else: + if len(semesterExperimentAssistants) == 1: + assistant = semesterExperimentAssistants[0] + else: + raise Exception( + f"Experiment {semesterExperiment} has more than one assistant. You have to assign one of these assistants: {semesterExperimentAssistants}" + ) + + return Appointment( + date=date, + special=special, + group_experiment=groupExperiment, + assistant=assistant, + ) + def repr(self): return f"{self.date} {self.group_experiment.repr()}"