Add base for route

This commit is contained in:
Sebastiaan
2025-10-31 13:42:16 +01:00
parent 23d7d63103
commit 84d75e21ca
11 changed files with 582 additions and 20 deletions

View File

@@ -139,6 +139,7 @@ class ApiTags(DocumentedStrEnum):
MEMBERS = "Members"
HIKES = "Hikes"
ROUTES = "Routes"
# endregion

View File

@@ -1,3 +1,4 @@
from datetime import timedelta
from typing import TYPE_CHECKING
from sqlmodel import (
@@ -16,10 +17,11 @@ from .base import (
)
if TYPE_CHECKING:
from .event import Event
from .route import Route
# region # Hike ################################################################
class HikeTeamPage(DocumentedIntFlag):
ROUTE = auto_enum() # Route steps info
QUESTIONS = auto_enum() # Visible questions
@@ -101,7 +103,7 @@ class HikeBase(
max_time_points: int | None = Field(
default=100,
ge=0, # TODO: ge > min_time_points
ge=0, # TODO: ge > min_time_points
description="Max points for time",
)
@@ -129,6 +131,7 @@ class Hike(mixin.RowId, HikeBase, table=True):
# --- read only items ------------------------------------------------------
# --- back_populates links -------------------------------------------------
routes: list["Route"] = Relationship(back_populates="hike", cascade_delete=True)
# --- CRUD actions ---------------------------------------------------------
@classmethod
@@ -164,18 +167,3 @@ class HikesPublic(BaseSQLModel):
# endregion
# region # Hike / Route ########################################################
class RouteType(DocumentedStrEnum):
START_FINISH = auto_enum() # Start at the start and end at the finish
CIRCULAR = auto_enum() # Start some ware, finish at the last new place
CIRCULAR_BACK_TO_START = auto_enum() # Start and finish on the same random place (CIRCULAR + next to start)
# ##############################################################################
# endregion

120
backend/app/models/route.py Normal file
View File

@@ -0,0 +1,120 @@
from datetime import timedelta
from sqlmodel import (
Session,
Relationship,
Field,
)
from . import mixin
from .base import (
BaseSQLModel,
DocumentedStrEnum,
DocumentedStrFlagType,
auto_enum,
RowId,
)
from .hike import (
Hike,
HikeTimeCalculation,
)
# region # Route ###############################################################
class RouteType(DocumentedStrEnum):
START_FINISH = auto_enum() # Start at the start and end at the finish
CIRCULAR = auto_enum() # Start some ware, finish at the last new place
CIRCULAR_BACK_TO_START = auto_enum() # Start and finish on the same random place (CIRCULAR + next to start)
# ##############################################################################
class RouteBase(
mixin.Name,
mixin.Contact,
BaseSQLModel,
):
route_type: RouteType = Field(
default=RouteType.START_FINISH,
nullable=False,
)
time_calculation_override: HikeTimeCalculation | None = Field(
default=None,
nullable=True,
description="Should this route be calculated different then the main hike",
sa_type=DocumentedStrFlagType(HikeTimeCalculation),
)
min_time: Interval | None = Field(
default=None,
description="Min time correction, None = min of all, positive is used as 0:00, negative is subtracted from min of all time",
)
max_time: Interval | None = Field(
default=None,
description="Max time correction, None = max of all, positive is used as max, negative is subtracted from max of all time",
)
hike_id: RowId = Field(
nullable=False,
foreign_key="hike.id",
)
# Properties to receive via API on creation
class RouteCreate(RouteBase):
pass
# Properties to receive via API on update, all are optional
class RouteUpdate(RouteBase):
route_type: RouteType | None = Field(default=None) # type: ignore
hike_id: RowId | None = Field(default=None) # type: ignore
class Route(mixin.RowId, RouteBase, table=True):
# --- database only items --------------------------------------------------
# --- read only items ------------------------------------------------------
# --- back_populates links -------------------------------------------------
hike: "Hike" = Relationship(back_populates="routes")
# --- CRUD actions ---------------------------------------------------------
@classmethod
def create(cls, *, session: Session, create_obj: RouteCreate) -> "Route":
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: "Route", in_obj: RouteUpdate
) -> "Route":
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 RoutePublic(mixin.RowIdPublic, RouteBase):
pass
class RoutesPublic(BaseSQLModel):
data: list[RoutePublic]
count: int
# endregion

View File

@@ -33,6 +33,7 @@ class PermissionModule(DocumentedStrEnum):
MEMBER = auto_enum()
HIKE = auto_enum()
ROUTE = auto_enum()
class PermissionPart(DocumentedStrEnum):
@@ -51,6 +52,8 @@ class PermissionRight(DocumentedIntFlag):
MANAGE_DIVISIONS = auto_enum()
MANAGE_MEMBERS = auto_enum()
MANAGE_HIKES = auto_enum()
ADMIN = ( CREATE
| READ
| UPDATE
@@ -59,6 +62,7 @@ class PermissionRight(DocumentedIntFlag):
| MANAGE_TEAMS
| MANAGE_DIVISIONS
| MANAGE_MEMBERS
| MANAGE_HIKES
)