class Professor(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) course_id = db.Column(db.Integer, db.ForeignKey('course.id')) enrolments = db.relationship('Enrolments', backref='professor', lazy=True) def __repr__(self): return f"Professor('{self.name}')"
class Course(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), nullable=False) semester = db.Column(db.Integer, nullable=False) professor = db.relationship('Professor', backref='course', uselist=False) enrolments = db.relationship('Enrolments', backref='course', lazy=True) def __repr__(self): return f"Course('{self.title}', '{self.semester}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) enrolments = db.relationship('Enrolments', backref='user', lazy=True) role_id = db.Column(db.Integer, nullable=False) def __repr__(self): return f"User('{self.name}', '{self.email}')"
class Enrolments(db.Model): id = db.Column(db.Integer, primary_key=True) grade = db.Column(db.Float, nullable=False) student_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) course_id = db.Column(db.Integer, db.ForeignKey("course.id"), nullable=False) professor_id = db.Column(db.Integer, db.ForeignKey('professor.id'), nullable=False) def __repr__(self): return f"Student: {self.student_id}, Professor: {self.professor_id}, Course: {self.course_id}, Grade: {self.grade})"
class Course(db.Model): __tablename__ = 'courses' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), index=True, nullable=False) start_date = db.Column(db.DateTime, nullable=False) end_date = db.Column(db.DateTime, nullable=False) lectures_number = db.Column(db.Integer, nullable=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if kwargs.get('start_date') and kwargs.get('end_date') and kwargs.get('lectures_number'): self.start_date = self.str_to_datetime(kwargs['start_date']) self.end_date = self.str_to_datetime(kwargs['end_date']) self.lectures_number = int(kwargs['lectures_number']) def __repr__(self): return f'Course {self.title}' def to_dict(self) -> dict: return { 'title': self.title, 'start_date': self.datetime_to_str(self.start_date), 'end_date': self.datetime_to_str(self.end_date), 'lectures_number': self.lectures_number, "_links": { "self": url_for('api.get_course', id=self.id), } } @staticmethod def str_to_datetime(date_as_str: str) -> datetime: return datetime.strptime(date_as_str, '%d/%m/%Y') @staticmethod def datetime_to_str(date_as_datetime: datetime) -> str: return date_as_datetime.strftime('%d/%m/%Y')