didier/database/models.py

52 lines
1.8 KiB
Python
Raw Normal View History

from __future__ import annotations
2022-06-19 00:36:38 +02:00
from datetime import datetime
from sqlalchemy import Column, Integer, Text, ForeignKey, Boolean, DateTime
2022-06-17 01:36:47 +02:00
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
2022-06-17 01:36:47 +02:00
class UforaCourse(Base):
"""A course on Ufora"""
__tablename__ = "ufora_courses"
course_id: int = Column(Integer, primary_key=True)
name: str = Column(Text, nullable=False, unique=True)
code: str = Column(Text, nullable=False, unique=True)
year: int = Column(Integer, nullable=False)
2022-06-19 00:23:25 +02:00
log_announcements: bool = Column(Boolean, default=False, nullable=False)
2022-06-17 01:36:47 +02:00
announcements: list[UforaAnnouncement] = relationship(
2022-06-19 00:23:25 +02:00
"UforaAnnouncement", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
2022-06-17 01:36:47 +02:00
)
aliases: list[UforaCourseAlias] = relationship(
2022-06-19 00:23:25 +02:00
"UforaCourseAlias", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
2022-06-17 01:36:47 +02:00
)
class UforaCourseAlias(Base):
"""An alias for a course on Ufora that we use to refer to them"""
__tablename__ = "ufora_course_aliases"
alias_id: int = Column(Integer, primary_key=True)
alias: str = Column(Text, nullable=False, unique=True)
course_id: int = Column(Integer, ForeignKey("ufora_courses.course_id"))
2022-06-19 00:23:25 +02:00
course: UforaCourse = relationship("UforaCourse", back_populates="aliases", uselist=False, lazy="selectin")
2022-06-17 01:36:47 +02:00
class UforaAnnouncement(Base):
"""An announcement sent on Ufora"""
__tablename__ = "ufora_announcements"
announcement_id = Column(Integer, primary_key=True)
course_id = Column(Integer, ForeignKey("ufora_courses.course_id"))
2022-06-19 00:36:38 +02:00
publication_date: datetime = Column(DateTime(timezone=True))
2022-06-17 01:36:47 +02:00
2022-06-19 00:23:25 +02:00
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False, lazy="selectin")