availabili.tf/backend-flask/models/event.py

56 lines
1.7 KiB
Python
Raw Normal View History

2024-11-19 10:46:45 -08:00
from datetime import datetime
from sqlalchemy.orm import mapped_column, relationship
from sqlalchemy.orm.attributes import Mapped
from sqlalchemy.orm.properties import ForeignKey
2024-11-24 10:47:45 -08:00
from sqlalchemy.schema import UniqueConstraint
2024-11-19 10:46:45 -08:00
from sqlalchemy.types import TIMESTAMP, Integer, String, Text
from sqlalchemy.sql import func
from sqlalchemy_utc import UtcDateTime
import app_db
import spec
2024-11-19 10:46:45 -08:00
class Event(app_db.BaseModel):
__tablename__ = "events"
2024-11-24 10:47:45 -08:00
# surrogate key
2024-11-19 10:46:45 -08:00
id: Mapped[int] = mapped_column(Integer, autoincrement=True, primary_key=True)
2024-11-24 10:47:45 -08:00
# primary key
team_id: Mapped[int] = mapped_column(ForeignKey("teams.id"), nullable=False)
2024-11-19 10:46:45 -08:00
name: Mapped[str] = mapped_column(String(255), nullable=False)
start_time: Mapped[datetime] = mapped_column(UtcDateTime, nullable=False)
2024-11-24 10:47:45 -08:00
description: Mapped[str] = mapped_column(Text, nullable=True)
2024-11-19 10:46:45 -08:00
created_at: Mapped[datetime] = mapped_column(TIMESTAMP, server_default=func.now())
team: Mapped["Team"] = relationship("Team", back_populates="events")
players: Mapped["PlayerEvent"] = relationship("PlayerEvent", back_populates="event")
2024-11-19 10:46:45 -08:00
2024-11-24 10:47:45 -08:00
__table_args__ = (
UniqueConstraint("team_id", "name", "start_time"),
)
class EventSchema(spec.BaseModel):
id: int
team_id: int
name: str
2024-11-24 10:47:45 -08:00
description: str | None
start_time: datetime
created_at: datetime
@classmethod
def from_model(cls, model: Event) -> "EventSchema":
return cls(
id=model.id,
name=model.name,
description=model.description,
start_time=model.start_time,
team_id=model.team_id,
created_at=model.created_at,
)
2024-11-19 10:46:45 -08:00
2024-11-24 10:47:45 -08:00
2024-11-19 10:46:45 -08:00
from models.team import Team
from models.player_event import PlayerEvent