[WIP] Link hike to places
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
from datetime import timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import (
|
||||
@@ -14,10 +13,12 @@ from .base import (
|
||||
DocumentedIntFlag,
|
||||
DocumentedStrFlagType,
|
||||
auto_enum,
|
||||
RowId,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .route import Route
|
||||
from .team import Team
|
||||
|
||||
# region # Hike ################################################################
|
||||
|
||||
@@ -63,6 +64,21 @@ class HikeTimeCalculation(DocumentedIntFlag):
|
||||
# ##############################################################################
|
||||
|
||||
|
||||
class HikeVisitLogType(DocumentedStrEnum):
|
||||
FIRST_VISIT = auto_enum()
|
||||
LAST_VISIT = auto_enum()
|
||||
|
||||
LONGEST_VISIT = auto_enum()
|
||||
SHORTEST_VISIT = auto_enum()
|
||||
|
||||
EACH_VISIT = auto_enum()
|
||||
|
||||
DISABLE_VISIT_LOGING = auto_enum()
|
||||
|
||||
|
||||
# ##############################################################################
|
||||
|
||||
|
||||
class HikeBase(
|
||||
mixin.Name,
|
||||
mixin.Contact,
|
||||
@@ -94,6 +110,11 @@ class HikeBase(
|
||||
sa_type=DocumentedStrFlagType(HikeTimeCalculation),
|
||||
)
|
||||
|
||||
visit_log_type: HikeVisitLogType = Field(
|
||||
default=HikeVisitLogType.FIRST_VISIT,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
min_time_points: int | None = Field(
|
||||
default=0,
|
||||
ge=0,
|
||||
@@ -123,6 +144,7 @@ class HikeUpdate(HikeBase):
|
||||
is_multi_day: bool | None = Field(default=None) # type: ignore
|
||||
team_page: HikeTeamPage | None = Field(default=None) # type: ignore
|
||||
time_calculation: HikeTimeCalculation | None = Field(default=None) # type: ignore
|
||||
visit_log_type: HikeVisitLogType | None = Field(default=None) # type: ignore
|
||||
|
||||
|
||||
class Hike(mixin.RowId, HikeBase, table=True):
|
||||
@@ -132,6 +154,7 @@ class Hike(mixin.RowId, HikeBase, table=True):
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
routes: list["Route"] = Relationship(back_populates="hike", cascade_delete=True)
|
||||
teams: list["HikeTeamLink"] = Relationship(back_populates="hike", cascade_delete=True)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
@@ -167,3 +190,87 @@ class HikesPublic(BaseSQLModel):
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region # Hike / Team #########################################################
|
||||
|
||||
class HikeTeamLinkBase(
|
||||
BaseSQLModel,
|
||||
):
|
||||
hike_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="hike.id",
|
||||
)
|
||||
|
||||
team_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="team.id",
|
||||
)
|
||||
|
||||
route_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="route.id",
|
||||
)
|
||||
|
||||
start_place_id: RowId | None = Field(
|
||||
default=None,
|
||||
nullable=True,
|
||||
foreign_key="place.id",
|
||||
)
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class HikeTeamLinkCreate(HikeTeamLinkBase):
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class HikeTeamLinkUpdate(HikeTeamLinkBase):
|
||||
hike_id: RowId | None = Field(default=None, nullable=True) # type: ignore
|
||||
team_id: RowId | None = Field(default=None, nullable=True) # type: ignore
|
||||
route_id: RowId | None = Field(default=None, nullable=True) # type: ignore
|
||||
|
||||
|
||||
class HikeTeamLink(mixin.RowId, HikeTeamLinkBase, table=True):
|
||||
# --- database only items --------------------------------------------------
|
||||
|
||||
# --- read only items ------------------------------------------------------
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
team: "Team" = Relationship(back_populates="hike_links")
|
||||
hike: "Hike" = Relationship(back_populates="teams")
|
||||
route: "Route" = Relationship(back_populates="teams")
|
||||
# start_place: "Place" = Relationship(back_populates="teams")
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
def create(cls, *, session: Session, create_obj: HikeTeamLinkCreate) -> "HikeTeamLink":
|
||||
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: "HikeTeamLink", in_obj: HikeTeamLinkUpdate
|
||||
) -> "HikeTeamLink":
|
||||
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 HikeTeamLinkPublic(mixin.RowIdPublic, HikeTeamLinkBase):
|
||||
pass
|
||||
|
||||
|
||||
class HikeTeamLinksPublic(BaseSQLModel):
|
||||
data: list[HikeTeamLinkPublic]
|
||||
count: int
|
||||
|
||||
# endregion
|
||||
|
||||
Reference in New Issue
Block a user