1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Added UniqueConstrains

This commit is contained in:
Mo 2021-07-11 03:21:54 +02:00
parent aa7ab40f1e
commit e50ad68d97

View file

@ -37,12 +37,17 @@ class Student(db.Model):
class PartStudent(db.Model):
# A student doing a specific part
id = db.Column(db.Integer, primary_key=True)
final_part_mark = db.Column(db.Float, nullable=True)
final_part_mark = db.Column(db.Integer, nullable=True)
student_id = db.Column(db.Integer, db.ForeignKey("student.id"), nullable=False)
part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False)
group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=True)
experiment_marks = db.relationship("ExperimentMark", backref="part_student", lazy=True)
__table_args__ = (
db.UniqueConstraint(student_id, part_id),
db.UniqueConstraint(student_id, group_id),
)
def repr(self):
return f"{self.student.repr()} {self.part.repr()}"
@ -57,6 +62,8 @@ class Group(db.Model):
part_students = db.relationship("PartStudent", backref="group", lazy=True)
group_experiments = db.relationship("GroupExperiment", backref="group", lazy=True)
__table_args__ = (db.UniqueConstraint(number, part_id),)
def repr(self):
return f"{self.number} {self.part.repr()}"
@ -72,6 +79,8 @@ class GroupExperiment(db.Model):
appointments = db.relationship("Appointment", backref="group_experiment", lazy=True)
experiment_marks = db.relationship("ExperimentMark", backref="group_experiment", lazy=True)
__table_args__ = (db.UniqueConstraint(semester_experiment_id, group_id),)
def repr(self):
return f"SemExp {self.semester_experiment.repr()}; Gr {self.group.repr()}"
@ -83,17 +92,11 @@ class GroupExperiment(db.Model):
student = partStudent.student
for partStudent in student.part_students:
for experimentMark in partStudent.experiment_marks:
if (
experimentMark.group_experiment.semester_experiment.experiment == semester_experiment.experiment
): # TODO Check if passed
if experimentMark.group_experiment.semester_experiment.experiment == semester_experiment.experiment:
raise Exception(
f"{student} has already done {semester_experiment.experiment} in {partStudent.part}!"
f"{student} has already done {semester_experiment.experiment} in {partStudent.part} and had {experimentMark}!"
)
for groupExperiment in group.group_experiments:
if groupExperiment.semester_experiment == semester_experiment: # TODO: What if did not pass?
raise Exception(f"{group} already has {semester_experiment}!")
return GroupExperiment(semester_experiment=semester_experiment, group=group)
@ -129,7 +132,7 @@ experiment_assistant = db.Table(
class SemesterExperiment(db.Model):
# An experiment in a specific part
# An experiment in a specific semester
id = db.Column(db.Integer, primary_key=True)
experiment_id = db.Column(db.Integer, db.ForeignKey("experiment.id"), nullable=False)
semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False)
@ -138,6 +141,8 @@ class SemesterExperiment(db.Model):
)
group_experiments = db.relationship("GroupExperiment", backref="semester_experiment", lazy=True)
__table_args__ = (db.UniqueConstraint(experiment_id, semester_id),)
def repr(self):
return f"{self.experiment.repr()} {self.semester.repr()}"
@ -153,7 +158,7 @@ class Assistant(db.Model):
mobile_phone_number = db.Column(db.String(50), nullable=True)
room = db.Column(db.String(100), nullable=True)
building = db.Column(db.String(100), nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False, unique=True)
appointments = db.relationship("Appointment", backref="assistant", lazy=True)
experiment_marks = db.relationship("ExperimentMark", backref="assistant", lazy=True)
@ -185,6 +190,8 @@ class Part(db.Model):
part_students = db.relationship("PartStudent", backref="part", lazy=True)
groups = db.relationship("Group", backref="part", lazy=True)
__table_args__ = (db.UniqueConstraint(label, semester_id),)
def repr(self):
return f"{self.label} {self.semester.repr()}"
@ -217,14 +224,16 @@ class Semester(db.Model):
class ExperimentMark(db.Model):
# A mark for a student after a specific experiment
id = db.Column(db.Integer, primary_key=True)
oral_mark = db.Column(db.Float, nullable=True)
protocol_mark = db.Column(db.Float, nullable=True)
oral_mark = db.Column(db.Integer, nullable=True)
protocol_mark = db.Column(db.Integer, nullable=True)
part_student_id = db.Column(db.Integer, db.ForeignKey("part_student.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=True
) # The assistant who gave the mark
__table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),)
def repr(self):
return f"Oral {round(self.oral_mark, 1)}; Prot {round(self.protocol_mark, 1)}"