Spaces:
Running on T4
Running on T4
File size: 3,530 Bytes
fe40b87 e980186 fe40b87 e980186 fe40b87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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")
)
|