Spaces:
Running on T4
Running on T4
Download backend/app/modules/threads/models.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/threads/models.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/threads/models.py
-
curl -L -o models.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/threads/models.py
3.53 kB
| import uuid | |
| from datetime import datetime | |
| from decimal import Decimal | |
| from sqlalchemy import ( | |
| CheckConstraint, | |
| DateTime, | |
| ForeignKey, | |
| Index, | |
| Integer, | |
| Numeric, | |
| String, | |
| Text, | |
| UniqueConstraint, | |
| func, | |
| text, | |
| ) | |
| from sqlalchemy.dialects.postgresql import UUID | |
| from sqlalchemy.orm import Mapped, mapped_column | |
| from app.common.base_model import Base, CreatedAtMixin, IdMixin | |
| class Thread(IdMixin, CreatedAtMixin, Base): | |
| """A player-coach conversation: one per reviewed session, or one general thread per (player, coach, mode). | |
| Read state is a per-side watermark on the message sequence (only two participants). | |
| """ | |
| __tablename__ = "threads" | |
| __table_args__ = ( | |
| CheckConstraint("mode IN ('h', 'p')", name="mode"), | |
| CheckConstraint("status IN ('waiting_for_coach', 'reviewed')", name="status"), | |
| Index("uq_threads_general", "player_id", "coach_id", "mode", unique=True, | |
| postgresql_where=text("session_id IS NULL")), | |
| Index("ix_threads_player_id_updated_at", "player_id", text("updated_at DESC")), | |
| Index("ix_threads_coach_id_updated_at", "coach_id", text("updated_at DESC")), | |
| ) | |
| team_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="SET NULL")) | |
| player_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE")) | |
| coach_id: Mapped[uuid.UUID | None] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL") | |
| ) | |
| mode: Mapped[str] = mapped_column(String(1)) | |
| session_id: Mapped[uuid.UUID | None] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("sessions.id", ondelete="CASCADE"), unique=True | |
| ) | |
| status: Mapped[str] = mapped_column(String(20), server_default="reviewed") | |
| last_seq: Mapped[int] = mapped_column(Integer, server_default="0") | |
| player_read_seq: Mapped[int] = mapped_column(Integer, server_default="0") | |
| coach_read_seq: Mapped[int] = mapped_column(Integer, server_default="0") | |
| updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) | |
| class Message(IdMixin, CreatedAtMixin, Base): | |
| """kind text: text; voice: upload_id + seconds; image: upload_id; video: upload_id + seconds; session: session_id; | |
| review: rendered from the session's review.""" | |
| __tablename__ = "messages" | |
| __table_args__ = ( | |
| UniqueConstraint("thread_id", "seq"), | |
| CheckConstraint("kind IN ('text', 'voice', 'session', 'review', 'image', 'video')", name="kind"), | |
| CheckConstraint("kind <> 'text' OR text IS NOT NULL", name="text_body"), | |
| CheckConstraint("kind <> 'voice' OR (upload_id IS NOT NULL AND seconds > 0)", name="voice_body"), | |
| ) | |
| thread_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("threads.id", ondelete="CASCADE")) | |
| seq: Mapped[int] = mapped_column(Integer) | |
| author_id: Mapped[uuid.UUID | None] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL") | |
| ) | |
| kind: Mapped[str] = mapped_column(String(8)) | |
| text: Mapped[str | None] = mapped_column(Text) | |
| upload_id: Mapped[uuid.UUID | None] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("uploads.id", ondelete="SET NULL") | |
| ) | |
| seconds: Mapped[Decimal | None] = mapped_column(Numeric(6, 2)) | |
| session_id: Mapped[uuid.UUID | None] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("sessions.id", ondelete="SET NULL") | |
| ) | |