49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from datetime import datetime
|
|
from enum import Enum
|
|
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
class MarathonStatus(str, Enum):
|
|
PREPARING = "preparing"
|
|
ACTIVE = "active"
|
|
FINISHED = "finished"
|
|
|
|
|
|
class Marathon(Base):
|
|
__tablename__ = "marathons"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
organizer_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
status: Mapped[str] = mapped_column(String(20), default=MarathonStatus.PREPARING.value)
|
|
invite_code: Mapped[str] = mapped_column(String(20), unique=True, nullable=False, index=True)
|
|
start_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
end_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
organizer: Mapped["User"] = relationship(
|
|
"User",
|
|
back_populates="organized_marathons",
|
|
foreign_keys=[organizer_id]
|
|
)
|
|
participants: Mapped[list["Participant"]] = relationship(
|
|
"Participant",
|
|
back_populates="marathon",
|
|
cascade="all, delete-orphan"
|
|
)
|
|
games: Mapped[list["Game"]] = relationship(
|
|
"Game",
|
|
back_populates="marathon",
|
|
cascade="all, delete-orphan"
|
|
)
|
|
activities: Mapped[list["Activity"]] = relationship(
|
|
"Activity",
|
|
back_populates="marathon",
|
|
cascade="all, delete-orphan"
|
|
)
|