mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Ufora announcements
This commit is contained in:
parent
bacd2d77fb
commit
6873cab955
12 changed files with 267 additions and 10 deletions
17
database/crud/ufora_announcements.py
Normal file
17
database/crud/ufora_announcements.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.models import UforaCourse, UforaAnnouncement
|
||||
|
||||
|
||||
async def get_courses_with_announcements(session: AsyncSession) -> list[UforaCourse]:
|
||||
"""Get all courses where announcements are enabled"""
|
||||
query = select(UforaCourse).where(UforaCourse.log_announcements)
|
||||
return (await session.execute(query)).scalars().all()
|
||||
|
||||
|
||||
async def create_new_announcement(session: AsyncSession, announcement_id: int, course: UforaCourse):
|
||||
"""Add a new announcement to the database"""
|
||||
new_announcement = UforaAnnouncement(announcement_id=announcement_id, course=course)
|
||||
session.add(new_announcement)
|
||||
await session.commit()
|
||||
|
|
@ -7,7 +7,7 @@ from sqlalchemy.orm import sessionmaker
|
|||
import settings
|
||||
|
||||
# Run local tests against SQLite instead of Postgres
|
||||
if settings.DB_TEST_SQLITE:
|
||||
if settings.TESTING and settings.DB_TEST_SQLITE:
|
||||
engine = create_async_engine(
|
||||
URL.create(
|
||||
drivername="sqlite+aiosqlite",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import Column, Integer, Text, ForeignKey
|
||||
from sqlalchemy import Column, Integer, Text, ForeignKey, Boolean
|
||||
from sqlalchemy.orm import declarative_base, relationship
|
||||
|
||||
Base = declarative_base()
|
||||
|
|
@ -15,12 +15,13 @@ class UforaCourse(Base):
|
|||
name: str = Column(Text, nullable=False, unique=True)
|
||||
code: str = Column(Text, nullable=False, unique=True)
|
||||
year: int = Column(Integer, nullable=False)
|
||||
log_announcements: bool = Column(Boolean, default=False, nullable=False)
|
||||
|
||||
announcements: list[UforaAnnouncement] = relationship(
|
||||
"UforaAnnouncement", back_populates="course", cascade="all, delete-orphan"
|
||||
"UforaAnnouncement", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
|
||||
)
|
||||
aliases: list[UforaCourseAlias] = relationship(
|
||||
"UforaCourseAlias", back_populates="course", cascade="all, delete-orphan"
|
||||
"UforaCourseAlias", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -33,7 +34,7 @@ class UforaCourseAlias(Base):
|
|||
alias: str = Column(Text, nullable=False, unique=True)
|
||||
course_id: int = Column(Integer, ForeignKey("ufora_courses.course_id"))
|
||||
|
||||
course: UforaCourse = relationship("UforaCourse", back_populates="aliases", uselist=False)
|
||||
course: UforaCourse = relationship("UforaCourse", back_populates="aliases", uselist=False, lazy="selectin")
|
||||
|
||||
|
||||
class UforaAnnouncement(Base):
|
||||
|
|
@ -44,4 +45,4 @@ class UforaAnnouncement(Base):
|
|||
announcement_id = Column(Integer, primary_key=True)
|
||||
course_id = Column(Integer, ForeignKey("ufora_courses.course_id"))
|
||||
|
||||
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False)
|
||||
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False, lazy="selectin")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue