diff --git a/advlabdb/models.py b/advlabdb/models.py index 3891da3..1f1fb24 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -57,6 +57,23 @@ class PartStudent(db.Model): db.UniqueConstraint(student_id, group_id), ) + def check(group, part): + if group and group.program != part.program: + raise DataBaseException( + f"Group's program {group.program} and student part's program {part.program} do not match!" + ) + + def customInit(student, part, group=None): + PartStudent.check(group, part) + + return PartStudent(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 repr(self): return f"{self.student.repr()} {self.part.repr()}" @@ -76,30 +93,46 @@ class Group(db.Model): __table_args__ = (db.UniqueConstraint(number, semester_id, program_id),) - def checkPartStudents(partStudents): - if partStudents: - program = partStudents[0].part.program - semester = partStudents[0].part.semester - for partStudent in partStudents[1:]: - if partStudent.part.program != program: - raise DataBaseException(f"Part Students {partStudents} are not in the same program!") + def check(part_students, program=None): + commonProgram = part_students[0].part.program + if program and program != commonProgram: + raise DataBaseException("Group's program and students' program mismatch!") - if partStudent.part.semester != semester: - raise DataBaseException(f"Part Students {partStudents} are not in the same semester!") + commonSemester = part_students[0].part.semester - def customInit(semester, program, partStudents): - checkPartStudents(partStudents) + for partStudent in part_students[1:]: + if partStudent.part.program != commonProgram: + raise DataBaseException(f"Part Students {part_students} are not in the same program!") - orderedGroups = Group.query.filter(Group.semester == semester, Group.program == program).order_by(Group.number) - lastTakenGroupNumber = orderedGroups[-1].number if orderedPartGroups.count() > 0 else 0 + if partStudent.part.semester != commonSemester: + raise DataBaseException(f"Part Students {part_students} are not in the same semester!") + + def customInit(part_students): + Group.check(part_students) + + semester = part_students[0].part.semester + program = part_students[0].part.program + + lastTakenGroupNumber = ( + Group.query.filter(Group.semester == semester, Group.program == program) + .order_by(Group.number.desc()) + .first() + .number + or 0 + ) return Group( program=program, number=lastTakenGroupNumber + 1, - part_students=partStudents, + part_students=part_students, semester=semester, ) + def customUpdate(self, part_students): + Group.check(part_students, self.program) + + self.part_students = part_students + def repr(self): return f"{self.number} {self.program.repr()} {self.semester.repr()}" @@ -169,7 +202,7 @@ class Experiment(db.Model): nullable=False, ) final_weighting = db.Column( - db.Float, db.CheckConstraint("final_weighting >= 0"), db.CheckConstraint("final_weightin <= 1"), nullable=False + db.Float, db.CheckConstraint("final_weighting >= 0"), db.CheckConstraint("final_weighting <= 1"), nullable=False ) program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False) @@ -263,23 +296,23 @@ class Appointment(db.Model): return assistant - def customInit(date, special, assistant, groupExperiment): - assistant = Appointment.checkAndGetAssistant(assistant, groupExperiment) + def customInit(date, special, assistant, group_experiment): + assistant = Appointment.checkAndGetAssistant(assistant, group_experiment) return Appointment( date=date, special=special, - group_experiment=groupExperiment, + group_experiment=group_experiment, assistant=assistant, ) - def customUpdate(self, date, special, assistant, groupExperiment): - assistant = Appointment.checkAndGetAssistant(assistant, groupExperiment) + 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 = groupExperiment + self.group_experiment = group_experiment def repr(self): return f"{self.date} {self.group_experiment.repr()}"