121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
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
|