2025-12-14 02:38:35 +07:00
|
|
|
from datetime import datetime
|
|
|
|
|
from enum import Enum
|
2025-12-14 20:21:56 +07:00
|
|
|
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer, Boolean
|
2025-12-14 02:38:35 +07:00
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MarathonStatus(str, Enum):
|
|
|
|
|
PREPARING = "preparing"
|
|
|
|
|
ACTIVE = "active"
|
|
|
|
|
FINISHED = "finished"
|
|
|
|
|
|
|
|
|
|
|
2025-12-14 20:21:56 +07:00
|
|
|
class GameProposalMode(str, Enum):
|
|
|
|
|
ALL_PARTICIPANTS = "all_participants"
|
|
|
|
|
ORGANIZER_ONLY = "organizer_only"
|
|
|
|
|
|
|
|
|
|
|
2026-01-05 07:15:50 +07:00
|
|
|
class CertificationStatus(str, Enum):
|
|
|
|
|
NONE = "none"
|
|
|
|
|
PENDING = "pending"
|
|
|
|
|
CERTIFIED = "certified"
|
|
|
|
|
REJECTED = "rejected"
|
|
|
|
|
|
|
|
|
|
|
2025-12-14 02:38:35 +07:00
|
|
|
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)
|
2025-12-14 20:21:56 +07:00
|
|
|
creator_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
2025-12-14 02:38:35 +07:00
|
|
|
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)
|
2025-12-14 20:21:56 +07:00
|
|
|
is_public: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
|
|
|
game_proposal_mode: Mapped[str] = mapped_column(String(20), default=GameProposalMode.ALL_PARTICIPANTS.value)
|
2025-12-14 02:38:35 +07:00
|
|
|
start_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
|
|
|
end_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
2025-12-15 03:22:29 +07:00
|
|
|
auto_events_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
2025-12-21 02:52:48 +07:00
|
|
|
cover_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
|
|
|
|
cover_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
2025-12-14 02:38:35 +07:00
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
|
|
|
|
2026-01-05 07:15:50 +07:00
|
|
|
# Certification fields
|
|
|
|
|
certification_status: Mapped[str] = mapped_column(String(20), default=CertificationStatus.NONE.value)
|
|
|
|
|
certification_requested_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
|
|
|
certified_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
|
|
|
certified_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
|
|
|
|
|
certification_rejection_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
|
|
|
|
|
|
# Shop/Consumables settings
|
|
|
|
|
allow_skips: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
|
max_skips_per_participant: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
|
|
|
allow_consumables: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
|
|
2025-12-14 02:38:35 +07:00
|
|
|
# Relationships
|
2025-12-14 20:21:56 +07:00
|
|
|
creator: Mapped["User"] = relationship(
|
2025-12-14 02:38:35 +07:00
|
|
|
"User",
|
2025-12-14 20:21:56 +07:00
|
|
|
back_populates="created_marathons",
|
|
|
|
|
foreign_keys=[creator_id]
|
2025-12-14 02:38:35 +07:00
|
|
|
)
|
2026-01-05 07:15:50 +07:00
|
|
|
certified_by: Mapped["User | None"] = relationship(
|
|
|
|
|
"User",
|
|
|
|
|
foreign_keys=[certified_by_id]
|
|
|
|
|
)
|
2025-12-14 02:38:35 +07:00
|
|
|
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"
|
|
|
|
|
)
|
2025-12-15 03:22:29 +07:00
|
|
|
events: Mapped[list["Event"]] = relationship(
|
|
|
|
|
"Event",
|
|
|
|
|
back_populates="marathon",
|
|
|
|
|
cascade="all, delete-orphan"
|
|
|
|
|
)
|
2026-01-05 07:15:50 +07:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_certified(self) -> bool:
|
|
|
|
|
return self.certification_status == CertificationStatus.CERTIFIED.value
|