Spaces:
Running on T4
Running on T4
File size: 7,110 Bytes
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | """Academy (= an existing team) staff and organisation. The team's owner (teams.coach_id) is the implicit admin;
other staff have a membership row. Players belong through accounts.team_id as before.
"""
import uuid
from datetime import datetime
from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Index,
Integer,
SmallInteger,
String,
Text,
UniqueConstraint,
text,
)
from sqlalchemy.dialects.postgresql import ARRAY, JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.common.base_model import Base, CreatedAtMixin, IdMixin, TimestampMixin
def _team() -> Mapped[uuid.UUID]:
return mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="CASCADE"), index=True)
def _account(nullable: bool = False) -> Mapped:
return mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL" if nullable else "CASCADE"))
class AcademyMembership(IdMixin, CreatedAtMixin, Base):
"""A staff member of a team. permissions: {review, drills, goals, library, earnings} effective booleans."""
__tablename__ = "academy_memberships"
__table_args__ = (
UniqueConstraint("team_id", "account_id"),
CheckConstraint("role IN ('admin', 'coach', 'assistant')", name="role"),
CheckConstraint("status IN ('active', 'paused', 'removed')", name="status"),
Index("ix_academy_memberships_account_id", "account_id"),
)
team_id: Mapped[uuid.UUID] = _team()
account_id: Mapped[uuid.UUID] = _account()
role: Mapped[str] = mapped_column(String(10))
status: Mapped[str] = mapped_column(String(8), server_default="active")
sees_all: Mapped[bool] = mapped_column(Boolean, server_default="false")
permissions: Mapped[dict] = mapped_column(JSONB)
joined_with: Mapped[str | None] = mapped_column(String(16))
version: Mapped[int] = mapped_column(Integer, server_default="1")
class StaffCode(IdMixin, CreatedAtMixin, Base):
"""A code staff use to apply. uses is a counter locked with the row so the last use cannot be taken twice."""
__tablename__ = "staff_codes"
__table_args__ = (
CheckConstraint("role IN ('coach', 'assistant')", name="role"),
CheckConstraint("approval IN ('admin', 'auto')", name="approval"),
CheckConstraint("uses >= 0 AND uses <= max_uses", name="uses"),
)
team_id: Mapped[uuid.UUID] = _team()
code: Mapped[str] = mapped_column(String(16), unique=True)
role: Mapped[str] = mapped_column(String(10))
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
max_uses: Mapped[int] = mapped_column(SmallInteger)
uses: Mapped[int] = mapped_column(SmallInteger, server_default="0")
approval: Mapped[str] = mapped_column(String(6))
revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
created_by_id: Mapped[uuid.UUID | None] = _account(nullable=True)
class CoachAccessRequest(IdMixin, CreatedAtMixin, Base):
"""An application to coach at a team. created_at is sent_at. At most one pending request per applicant."""
__tablename__ = "coach_access_requests"
__table_args__ = (
CheckConstraint("status IN ('pending', 'approved', 'declined')", name="status"),
Index("uq_coach_access_requests_pending", "applicant_id", unique=True,
postgresql_where=text("status = 'pending'")),
Index("ix_coach_access_requests_team_id_created_at", "team_id", text("created_at DESC")),
)
applicant_id: Mapped[uuid.UUID] = _account()
team_id: Mapped[uuid.UUID] = _team()
staff_code_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("staff_codes.id", ondelete="SET NULL")
)
code: Mapped[str] = mapped_column(String(16))
status: Mapped[str] = mapped_column(String(8), server_default="pending")
decided_by_id: Mapped[uuid.UUID | None] = _account(nullable=True)
decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
class PlayerGroup(IdMixin, CreatedAtMixin, Base):
__tablename__ = "player_groups"
__table_args__ = (
CheckConstraint("focus IN ('Hitting', 'Pitching', 'Both')", name="focus"),
Index("uq_player_groups_team_id_name", "team_id", text("lower(name)"), unique=True),
)
team_id: Mapped[uuid.UUID] = _team()
name: Mapped[str] = mapped_column(String(80))
focus: Mapped[str] = mapped_column(String(8))
class GroupPlayer(Base):
__tablename__ = "group_players"
group_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("player_groups.id", ondelete="CASCADE"), primary_key=True
)
player_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True
)
class GroupCoach(Base):
__tablename__ = "group_coaches"
group_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("player_groups.id", ondelete="CASCADE"), primary_key=True
)
coach_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True
)
class CoachPlayer(CreatedAtMixin, Base):
"""A player on one staff coach's own roster (in addition to group and sees_all visibility)."""
__tablename__ = "coach_players"
team_id: Mapped[uuid.UUID] = _team()
coach_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True
)
player_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True
)
class CoachNote(IdMixin, TimestampMixin, Base):
"""A coach's private note on a player; only that coach can read it."""
__tablename__ = "coach_notes"
__table_args__ = (UniqueConstraint("team_id", "coach_id", "player_id"),)
team_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="CASCADE"))
coach_id: Mapped[uuid.UUID] = _account()
player_id: Mapped[uuid.UUID] = _account()
text: Mapped[str] = mapped_column(Text, server_default="")
version: Mapped[int] = mapped_column(Integer, server_default="0")
class LibraryDrill(IdMixin, CreatedAtMixin, Base):
"""A team's own drill video. Assignments snapshot name/fixes, so deleting one never rewrites history."""
__tablename__ = "library_drills"
__table_args__ = (CheckConstraint("mode IN ('h', 'p')", name="mode"),)
team_id: Mapped[uuid.UUID] = _team()
author_id: Mapped[uuid.UUID | None] = _account(nullable=True)
name: Mapped[str] = mapped_column(String(120))
fixes: Mapped[str] = mapped_column(String(500), server_default="")
mode: Mapped[str] = mapped_column(String(1))
fault_tags: Mapped[list[str]] = mapped_column(ARRAY(String(64)), server_default=text("'{}'"))
upload_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("uploads.id", ondelete="SET NULL")
)
|