""" See the file DB.drawio for the design of the database. It can be opened in the internet browser with the website: https://app.diagrams.net For more information about the implementation, see the part to Models in the documentation of Flask-SQLAlchemy: https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/ """ # Imports from flask import flash, has_request_context from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin # Importing the database instance from advlabdb import db from advlabdb.configUtils import getConfig class Student(db.Model): id = db.Column(db.Integer, primary_key=True) student_number = db.Column(db.Integer, nullable=False, unique=True) first_name = db.Column(db.String(100), nullable=False) last_name = db.Column(db.String(100), nullable=False) uni_email = db.Column(db.String(200), nullable=False, unique=True) contact_email = db.Column(db.String(200), unique=True) bachelor_thesis = db.Column(db.String, nullable=True) bachelor_thesis_work_group = db.Column(db.String, nullable=True) note = db.Column(db.Text, nullable=True) part_students = db.relationship("PartStudent", backref="student", lazy=True) def repr(self): return f"{self.first_name} {self.last_name}" def __repr__(self): return f"" 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) 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) def repr(self): return f"{self.student.repr()} {self.part.repr()}" def __repr__(self): return f"" class Group(db.Model): id = db.Column(db.Integer, primary_key=True) number = db.Column(db.Integer, nullable=False) part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False) part_students = db.relationship("PartStudent", backref="group", lazy=True) group_experiments = db.relationship("GroupExperiment", backref="group", lazy=True) def repr(self): return f"{self.number} {self.part.repr()}" def __repr__(self): return f"" class GroupExperiment(db.Model): # An experiment specified to a group id = db.Column(db.Integer, primary_key=True) semester_experiment_id = db.Column(db.Integer, db.ForeignKey("semester_experiment.id"), nullable=False) group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=False) appointments = db.relationship("Appointment", backref="group_experiment", lazy=True) experiment_marks = db.relationship("ExperimentMark", backref="group_experiment", lazy=True) def repr(self): return f"SemExp {self.semester_experiment.repr()}; Gr {self.group.repr()}" def __repr__(self): return f"" def checkAndInit(semester_experiment, group): for partStudent in group.part_students: 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 raise Exception( f"{student} has already done {semester_experiment.experiment} in {partStudent.part}!" ) 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) class Experiment(db.Model): id = db.Column(db.Integer, primary_key=True) label = db.Column(db.String(20), nullable=False, unique=True) title = db.Column(db.String(200), nullable=False) description = db.Column(db.Text, nullable=True) wiki_link = db.Column(db.String(200), nullable=True) room = db.Column(db.String(100), nullable=False) building = db.Column(db.String(100), nullable=False) responsibility = db.Column(db.String(200), nullable=True) duration_in_days = db.Column(db.Integer, nullable=False) active = db.Column(db.Boolean, nullable=False, default=True) oral_weighting = db.Column(db.Float, nullable=False) protocol_weighting = db.Column(db.Float, nullable=False) final_weighting = db.Column(db.Float, nullable=False) semester_experiments = db.relationship("SemesterExperiment", backref="experiment", lazy=True) def repr(self): return f"{self.label}" def __repr__(self): return f"" # Helper table for the many to many relationship between Assistant and SemesterExperiment experiment_assistant = db.Table( "experiment_assistant", db.Column("semester_experiment_id", db.Integer, db.ForeignKey("semester_experiment.id"), primary_key=True), db.Column("assistant_id", db.Integer, db.ForeignKey("assistant.id"), primary_key=True), ) class SemesterExperiment(db.Model): # An experiment in a specific part 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) assistants = db.relationship( "Assistant", secondary=experiment_assistant, lazy=True, backref=db.backref("semester_experiments", lazy=True) ) group_experiments = db.relationship("GroupExperiment", backref="semester_experiment", lazy=True) def repr(self): return f"{self.experiment.repr()} {self.semester.repr()}" def __repr__(self): return f"" class Assistant(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100), nullable=False) last_name = db.Column(db.String(100), nullable=False) phone_number = db.Column(db.String(50), nullable=True) 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) appointments = db.relationship("Appointment", backref="assistant", lazy=True) experiment_marks = db.relationship("ExperimentMark", backref="assistant", lazy=True) def repr(self): return f"{self.first_name} {self.last_name}" def __repr__(self): return f"" class Appointment(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Date, nullable=False) # To be specified with the python package "datetime" special = db.Column(db.Boolean, nullable=False) # In the break or not 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 repr(self): return f"{self.date} {self.group_experiment.repr()}" def __repr__(self): return f"" class Part(db.Model): id = db.Column(db.Integer, primary_key=True) label = db.Column(db.String(100), nullable=False) # A/1, A/2, B/1, B/2 semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False) part_students = db.relationship("PartStudent", backref="part", lazy=True) groups = db.relationship("Group", backref="part", lazy=True) def repr(self): return f"{self.label} {self.semester.repr()}" def __repr__(self): return f"" class Semester(db.Model): id = db.Column(db.Integer, primary_key=True) label = db.Column(db.String(100), nullable=False, unique=True) # WS2122 for example parts = db.relationship("Part", backref="semester", lazy=True) semester_experiments = db.relationship("SemesterExperiment", backref="semester", lazy=True) def repr(self): return f"{self.label}" def __repr__(self): return f"" def transferPartsFrom(self, oldSemester): if oldSemester: partLabels = [part.label for part in oldSemester.parts] else: partLabels = getConfig("defaultPartLabels") for partLabel in partLabels: db.session.add(Part(label=partLabel, semester=self)) 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) 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 def repr(self): return f"Oral {round(self.oral_mark, 1)}; Prot {round(self.protocol_mark, 1)}" def __repr__(self): return f"" class User(db.Model, FsUserMixin): assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False) active_semester_id = db.Column(db.Integer, nullable=True) def repr(self): return f"{self.email}" def __repr__(self): return f"" class Role(db.Model, FsRoleMixin): def repr(self): return f"{self.name}" def __repr__(self): return f""