Implement divisions
This commit is contained in:
@@ -2,6 +2,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import (
|
||||
Session,
|
||||
Relationship,
|
||||
)
|
||||
|
||||
from . import mixin
|
||||
@@ -9,6 +10,9 @@ from .base import (
|
||||
BaseSQLModel,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .division import Division
|
||||
|
||||
# region # Association #########################################################
|
||||
|
||||
|
||||
@@ -37,6 +41,7 @@ class Association(mixin.RowId, AssociationBase, table=True):
|
||||
# --- read only items ------------------------------------------------------
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
divisions: list["Division"] = Relationship(back_populates="association", cascade_delete=True)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
|
||||
@@ -57,6 +57,7 @@ class ApiTags(DocumentedStrEnum):
|
||||
EVENTS = "Events"
|
||||
TEAMS = "Teams"
|
||||
ASSOCIATIONS = "Associations"
|
||||
DIVISIONS = "Divisions"
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
83
backend/app/models/division.py
Normal file
83
backend/app/models/division.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import (
|
||||
Session,
|
||||
Field,
|
||||
Relationship,
|
||||
)
|
||||
|
||||
from . import mixin
|
||||
from .base import (
|
||||
BaseSQLModel,
|
||||
RowId,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .association import Association
|
||||
|
||||
# region # Divisions ###########################################################
|
||||
|
||||
|
||||
class DivisionBase(
|
||||
mixin.Name,
|
||||
mixin.Contact,
|
||||
mixin.ScoutingId,
|
||||
BaseSQLModel,
|
||||
):
|
||||
association_id: RowId = Field(
|
||||
foreign_key="association.id", nullable=False, ondelete="CASCADE"
|
||||
)
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class DivisionCreate(DivisionBase):
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class DivisionUpdate(DivisionBase):
|
||||
association_id: RowId | None = Field(default=None)
|
||||
|
||||
|
||||
class Division(mixin.RowId, DivisionBase, table=True):
|
||||
# --- database only items --------------------------------------------------
|
||||
|
||||
# --- read only items ------------------------------------------------------
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
association: "Association" = Relationship(back_populates="divisions") # , cascade_delete=True)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
def create(cls, *, session: Session, create_obj: DivisionCreate) -> "Division":
|
||||
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: "Division", in_obj: DivisionUpdate
|
||||
) -> "Division":
|
||||
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 DivisionPublic(mixin.RowIdPublic, DivisionBase):
|
||||
association_id: RowId
|
||||
|
||||
|
||||
class DivisionsPublic(BaseSQLModel):
|
||||
data: list[DivisionPublic]
|
||||
count: int
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -28,6 +28,7 @@ class PermissionModule(DocumentedStrEnum):
|
||||
EVENT = auto_enum()
|
||||
TEAM = auto_enum()
|
||||
ASSOCIATION = auto_enum()
|
||||
DIVISION = auto_enum()
|
||||
|
||||
|
||||
class PermissionPart(DocumentedStrEnum):
|
||||
@@ -43,8 +44,9 @@ class PermissionRight(DocumentedIntFlag):
|
||||
|
||||
MANAGE_USERS = auto_enum()
|
||||
MANAGE_TEAMS = auto_enum()
|
||||
MANAGE_DIVISIONS = auto_enum()
|
||||
|
||||
ADMIN = CREATE | READ | UPDATE | DELETE | MANAGE_USERS | MANAGE_TEAMS
|
||||
ADMIN = CREATE | READ | UPDATE | DELETE | MANAGE_USERS | MANAGE_TEAMS | MANAGE_DIVISIONS
|
||||
|
||||
|
||||
# ##############################################################################
|
||||
|
||||
Reference in New Issue
Block a user