[WIP] Link hike to places
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from datetime import timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Interval
|
||||
from sqlmodel import (
|
||||
Session,
|
||||
Relationship,
|
||||
@@ -18,8 +20,12 @@ from .base import (
|
||||
from .hike import (
|
||||
Hike,
|
||||
HikeTimeCalculation,
|
||||
HikeTeamLink,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .place import Place
|
||||
|
||||
# region # Route ###############################################################
|
||||
|
||||
|
||||
@@ -49,14 +55,16 @@ class RouteBase(
|
||||
sa_type=DocumentedStrFlagType(HikeTimeCalculation),
|
||||
)
|
||||
|
||||
min_time: Interval | None = Field(
|
||||
min_time: timedelta | 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",
|
||||
sa_type=Interval,
|
||||
)
|
||||
|
||||
max_time: Interval | None = Field(
|
||||
max_time: timedelta | 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",
|
||||
sa_type=Interval,
|
||||
)
|
||||
|
||||
hike_id: RowId = Field(
|
||||
@@ -64,6 +72,22 @@ class RouteBase(
|
||||
foreign_key="hike.id",
|
||||
)
|
||||
|
||||
start_id: RowId | None = Field(
|
||||
default=None,
|
||||
nullable=True,
|
||||
foreign_key="place.id",
|
||||
ondelete="SET NULL",
|
||||
description="Start place.id of the route, None for random in CIRCULAR or CIRCULAR_BACK_TO_START",
|
||||
)
|
||||
|
||||
finish_id: RowId | None = Field(
|
||||
default=None,
|
||||
nullable=True,
|
||||
foreign_key="place.id",
|
||||
ondelete="SET NULL",
|
||||
description="Finish place.id of the route, None for random or decremented",
|
||||
)
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class RouteCreate(RouteBase):
|
||||
@@ -83,6 +107,30 @@ class Route(mixin.RowId, RouteBase, table=True):
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
hike: "Hike" = Relationship(back_populates="routes")
|
||||
teams: list["HikeTeamLink"] = Relationship(back_populates="route")
|
||||
|
||||
start: "Place" = Relationship(sa_relationship_kwargs={"foreign_keys": "[Route.start_id]"})
|
||||
finish: "Place" = Relationship(sa_relationship_kwargs={"foreign_keys": "[Route.finish_id]"})
|
||||
|
||||
places: list["Place"] = Relationship(
|
||||
sa_relationship_kwargs={
|
||||
"primaryjoin": "or_("
|
||||
"Place.id==Route.start_id,"
|
||||
"Place.id==Route.finish_id,"
|
||||
"Place.id==RoutePart.place_id,"
|
||||
"Place.id==RoutePart.to_place_id,"
|
||||
")",
|
||||
"foreign_keys": "["
|
||||
"Route.start_id,"
|
||||
"Route.finish_id,"
|
||||
"RoutePart.place_id,"
|
||||
"RoutePart.to_place_id"
|
||||
"]",
|
||||
"viewonly": True,
|
||||
},
|
||||
)
|
||||
|
||||
route_parts: list["RoutePart"] = Relationship(back_populates="route")
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
@@ -118,3 +166,118 @@ class RoutesPublic(BaseSQLModel):
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region # Route / Place link ##################################################
|
||||
|
||||
|
||||
class RoutePartBase(
|
||||
mixin.NameOveride,
|
||||
mixin.ShortNameOveride,
|
||||
BaseSQLModel,
|
||||
):
|
||||
route_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="route.id",
|
||||
)
|
||||
|
||||
place_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="place.id",
|
||||
)
|
||||
|
||||
to_place_id: RowId = Field(
|
||||
nullable=False,
|
||||
foreign_key="place.id",
|
||||
)
|
||||
|
||||
travel_time: timedelta | None = Field(
|
||||
default=None,
|
||||
nullable=True,
|
||||
description="Time to travel during test walk, only used for information",
|
||||
sa_type=Interval,
|
||||
)
|
||||
travel_distance: int | None = Field(
|
||||
default=None,
|
||||
nullable=True,
|
||||
ge=0,
|
||||
description="Distance in meters to travel, only used for information",
|
||||
)
|
||||
|
||||
# TODO: Convert to time groups
|
||||
free_walk_time: bool | None = Field(
|
||||
default=False,
|
||||
description="If true, travel time is not add to calculated for travel time",
|
||||
)
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class RoutePartBaseCreate(RoutePartBase):
|
||||
pass
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class RoutePartBaseUpdate(RoutePartBase):
|
||||
route_id: RowId | None = Field(default=None) # type: ignore
|
||||
place_id: RowId | None = Field(default=None) # type: ignore
|
||||
to_place_id: RowId | None = Field(default=None) # type: ignore
|
||||
free_walk_time: bool | None = Field(default=None) # type: ignore
|
||||
|
||||
|
||||
class RoutePart(mixin.RowId, RoutePartBase, table=True):
|
||||
# --- database only items --------------------------------------------------
|
||||
|
||||
# --- read only items ------------------------------------------------------
|
||||
|
||||
# --- back_populates links -------------------------------------------------
|
||||
route: "Route" = Relationship(back_populates="route_parts")
|
||||
place: "Place" = Relationship(back_populates="route_parts", sa_relationship_kwargs={"foreign_keys": "[RoutePart.place_id]"})
|
||||
place_to: "Place" = Relationship(sa_relationship_kwargs={"foreign_keys": "[RoutePart.to_place_id]"})
|
||||
|
||||
places: list["Place"] = Relationship(
|
||||
sa_relationship_kwargs={
|
||||
"primaryjoin": "or_("
|
||||
"Place.id==RoutePart.place_id,"
|
||||
"Place.id==RoutePart.to_place_id,"
|
||||
")",
|
||||
"foreign_keys": "["
|
||||
"RoutePart.place_id,"
|
||||
"RoutePart.to_place_id"
|
||||
"]",
|
||||
"viewonly": True,
|
||||
},
|
||||
)
|
||||
|
||||
# --- CRUD actions ---------------------------------------------------------
|
||||
@classmethod
|
||||
def create(cls, *, session: Session, create_obj: RouteCreate) -> "RoutePart":
|
||||
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: "RoutePart", in_obj: RouteUpdate
|
||||
) -> "RoutePart":
|
||||
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 RoutePartPublic(mixin.RowIdPublic, RoutePartBase):
|
||||
pass
|
||||
|
||||
|
||||
class RoutePartsPublic(BaseSQLModel):
|
||||
data: list[RoutePartPublic]
|
||||
count: int
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
Reference in New Issue
Block a user