Implement teams in own file
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import (
|
||||
Field,
|
||||
Relationship,
|
||||
@@ -15,6 +17,9 @@ from .user import (
|
||||
User,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .team import Team
|
||||
|
||||
# region # Event ###############################################################
|
||||
|
||||
|
||||
@@ -72,7 +77,7 @@ class Event(mixin.RowId, EventBase, table=True):
|
||||
|
||||
# --- many-to-many links ---------------------------------------------------
|
||||
user_links: list["EventUserLink"] = Relationship(back_populates="event")
|
||||
team_links: list["EventTeam"] = Relationship(back_populates="event")
|
||||
team_links: list["Team"] = Relationship(back_populates="event")
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
@@ -179,70 +184,3 @@ class EventsPublic(BaseSQLModel):
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region EventTeam ############################################################
|
||||
|
||||
|
||||
class EventTeamBase(mixin.ThemeName, mixin.CheckInCheckOut, mixin.Canceled, BaseSQLModel):
|
||||
# scouting_team_id: RowId | None = Field(
|
||||
# foreign_key="ScoutingTeam.id", nullable=False, ondelete="CASCADE"
|
||||
# )
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class EventTeamCreate(EventTeamBase):
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class EventTeamUpdate(mixin.ThemeNameUpdate, EventTeamBase):
|
||||
pass
|
||||
|
||||
|
||||
class EventTeam(mixin.RowId, EventTeamBase, table=True):
|
||||
# --- database only items --------------------------------------------------
|
||||
|
||||
# --- read only items ------------------------------------------------------
|
||||
event_id: RowId = Field(
|
||||
foreign_key="event.id", nullable=False, ondelete="CASCADE"
|
||||
)
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
event: "Event" = Relationship(back_populates="team_links")#, cascade_delete=True)
|
||||
# team: "ScoutingTeam" = Relationship(back_populates="event_links", cascade_delete=True)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
def create(cls, *, session: Session, create_obj: EventTeamCreate, event: Event) -> "EventTeam":
|
||||
data_obj = create_obj.model_dump(exclude_unset=True)
|
||||
|
||||
db_obj = cls.model_validate(data_obj, update={"event_id": event.id})
|
||||
session.add(db_obj)
|
||||
session.commit()
|
||||
session.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
@classmethod
|
||||
def update(
|
||||
cls, *, session: Session, db_obj: "EventTeam", in_obj: EventTeamUpdate
|
||||
) -> "EventTeam":
|
||||
data_obj = in_obj.model_dump(exclude_unset=True)
|
||||
db_obj.sqlmodel_update(data_obj)
|
||||
session.add(db_obj)
|
||||
session.commit()
|
||||
session.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
|
||||
# Properties to return via API, id is always required
|
||||
class EventTeamPublic(mixin.RowIdPublic, EventTeamBase):
|
||||
event_id: RowId
|
||||
|
||||
|
||||
class EventTeamsPublic(BaseSQLModel):
|
||||
data: list[EventTeamPublic]
|
||||
count: int
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -10,7 +10,7 @@ from .base import RowId as RowIdType
|
||||
|
||||
|
||||
class Name(BaseModel):
|
||||
name: str | None = Field(default=None, nullable=False, unique=True, max_length=255)
|
||||
name: str | None = Field(default=None, nullable=False, unique=False, max_length=255)
|
||||
|
||||
|
||||
class FullName(BaseModel):
|
||||
|
||||
87
backend/app/models/team.py
Normal file
87
backend/app/models/team.py
Normal file
@@ -0,0 +1,87 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import (
|
||||
Field,
|
||||
Relationship,
|
||||
Session,
|
||||
)
|
||||
|
||||
from . import mixin
|
||||
from .base import (
|
||||
BaseSQLModel,
|
||||
RowId,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .event import Event
|
||||
|
||||
# region # Team ################################################################
|
||||
|
||||
|
||||
class TeamBase(
|
||||
mixin.ThemeName,
|
||||
mixin.CheckInCheckOut,
|
||||
mixin.Canceled,
|
||||
BaseSQLModel
|
||||
):
|
||||
event_id: RowId = Field(
|
||||
foreign_key="event.id", nullable=False, ondelete="CASCADE"
|
||||
)
|
||||
# scouting_team_id: RowId | None = Field(
|
||||
# foreign_key="ScoutingTeam.id", nullable=False, ondelete="CASCADE"
|
||||
# )
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class TeamCreate(TeamBase):
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class TeamUpdate(mixin.ThemeNameUpdate, TeamBase):
|
||||
event_id: RowId | None = Field(default=None)
|
||||
|
||||
|
||||
class Team(mixin.RowId, TeamBase, table=True):
|
||||
# --- database only items --------------------------------------------------
|
||||
|
||||
# --- read only items ------------------------------------------------------
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
event: "Event" = Relationship(back_populates="team_links")#, cascade_delete=True)
|
||||
# team: "ScoutingTeam" = Relationship(back_populates="event_links", cascade_delete=True)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
def create(cls, *, session: Session, create_obj: TeamCreate) -> "Team":
|
||||
data_obj = create_obj.model_dump(exclude_unset=True)
|
||||
|
||||
db_obj = cls.model_validate(data_obj)
|
||||
session.add(db_obj)
|
||||
session.commit()
|
||||
session.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
@classmethod
|
||||
def update(
|
||||
cls, *, session: Session, db_obj: "Team", in_obj: TeamUpdate
|
||||
) -> "Team":
|
||||
data_obj = in_obj.model_dump(exclude_unset=True)
|
||||
db_obj.sqlmodel_update(data_obj)
|
||||
session.add(db_obj)
|
||||
session.commit()
|
||||
session.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
|
||||
# Properties to return via API, id is always required
|
||||
class TeamPublic(mixin.RowIdPublic, TeamBase):
|
||||
event_id: RowId
|
||||
|
||||
|
||||
class TeamsPublic(BaseSQLModel):
|
||||
data: list[TeamPublic]
|
||||
count: int
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -26,6 +26,7 @@ class PermissionModule(DocumentedStrEnum):
|
||||
SYSTEM = auto_enum()
|
||||
USER = auto_enum()
|
||||
EVENT = auto_enum()
|
||||
TEAM = auto_enum()
|
||||
|
||||
|
||||
class PermissionPart(DocumentedStrEnum):
|
||||
@@ -40,9 +41,9 @@ class PermissionRight(DocumentedIntFlag):
|
||||
DELETE = auto_enum()
|
||||
|
||||
MANAGE_USERS = auto_enum()
|
||||
MANGE_TEAMS = auto_enum()
|
||||
MANAGE_TEAMS = auto_enum()
|
||||
|
||||
ADMIN = CREATE | READ | UPDATE | DELETE | MANAGE_USERS | MANGE_TEAMS
|
||||
ADMIN = CREATE | READ | UPDATE | DELETE | MANAGE_USERS | MANAGE_TEAMS
|
||||
|
||||
|
||||
class PermissionRightObject(BaseSQLModel):
|
||||
|
||||
Reference in New Issue
Block a user