👽️ Take the last IDFM format into account

This commit is contained in:
2023-06-11 22:41:44 +02:00
parent 2eaf0f4ed5
commit 5da918c04b
3 changed files with 26 additions and 46 deletions

View File

@@ -116,19 +116,26 @@ class StopArea(Struct):
record_timestamp: datetime record_timestamp: datetime
class ConnectionArea(Struct): class ConnectionAreaFields(Struct, kw_only=True):
zdcid: str zdcid: str
zdcversion: str zdcversion: str
zdccreated: datetime zdccreated: datetime
zdcchanged: datetime zdcchanged: datetime
zdcname: str zdcname: str
zdcxepsg2154: int zdcxepsg2154: int | None = None
zdcyepsg2154: int zdcyepsg2154: int | None = None
zdctown: str zdctown: str
zdcpostalregion: str zdcpostalregion: str
zdctype: StopAreaType zdctype: StopAreaType
class ConnectionArea(Struct):
datasetid: str
recordid: str
fields: ConnectionAreaFields
record_timestamp: datetime
class StopAreaStopAssociationFields(Struct, kw_only=True): class StopAreaStopAssociationFields(Struct, kw_only=True):
arrid: str # TODO: use int ? arrid: str # TODO: use int ?
artid: str | None = None artid: str | None = None
@@ -149,6 +156,7 @@ class StopAreaStopAssociation(Struct):
class IdfmLineState(Enum): class IdfmLineState(Enum):
active = "active" active = "active"
available_soon = "prochainement active"
class LinePicto(Struct, rename={"id_": "id"}): class LinePicto(Struct, rename={"id_": "id"}):

View File

@@ -1,6 +1,3 @@
from datetime import datetime
from typing import Optional
from msgspec import Struct from msgspec import Struct
@@ -13,13 +10,6 @@ class PictoFieldsFile(Struct, rename={"id_": "id"}):
format: str format: str
class PictoFields(Struct):
indices_commerciaux: str
noms_des_fichiers: Optional[PictoFieldsFile] = None
class Picto(Struct): class Picto(Struct):
datasetid: str indices_commerciaux: str
recordid: str noms_des_fichiers: PictoFieldsFile | None = None
fields: PictoFields
record_timestamp: datetime

View File

@@ -5,11 +5,7 @@ from typing import Sequence
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from fastapi_cache.decorator import cache from fastapi_cache.decorator import cache
from backend.idfm_interface import ( from backend.idfm_interface import Destinations as IdfmDestinations, TrainStatus
Destinations as IdfmDestinations,
IdfmInterface,
TrainStatus,
)
from backend.models import Stop, StopArea, StopShape from backend.models import Stop, StopArea, StopShape
from backend.schemas import ( from backend.schemas import (
NextPassage as NextPassageSchema, NextPassage as NextPassageSchema,
@@ -106,7 +102,7 @@ async def get_next_passages(stop_id: int) -> NextPassagesSchema | None:
# re.match will return None if the given journey.LineRef.value is not valid. # re.match will return None if the given journey.LineRef.value is not valid.
try: try:
line_id_match = IdfmInterface.LINE_RE.match(journey.LineRef.value) line_id_match = idfm_interface.LINE_RE.match(journey.LineRef.value)
line_id = int(line_id_match.group(1)) # type: ignore line_id = int(line_id_match.group(1)) # type: ignore
except (AttributeError, TypeError, ValueError) as err: except (AttributeError, TypeError, ValueError) as err:
raise HTTPException( raise HTTPException(
@@ -163,32 +159,18 @@ async def get_stop_destinations(
@router.get("/{stop_id}/shape") @router.get("/{stop_id}/shape")
@cache(namespace="stop-shape") @cache(namespace="stop-shape")
async def get_stop_shape(stop_id: int) -> StopShapeSchema | None: async def get_stop_shape(stop_id: int) -> StopShapeSchema | None:
connection_area = None if (await Stop.get_by_id(stop_id)) is not None or (
await StopArea.get_by_id(stop_id)
) is not None:
shape_id = stop_id
if (stop := await Stop.get_by_id(stop_id)) is not None: if (shape := await StopShape.get_by_id(shape_id)) is not None:
connection_area = stop.connection_area return StopShapeSchema(
id=shape.id,
elif (stop_area := await StopArea.get_by_id(stop_id)) is not None: type=shape.type,
connection_areas = {stop.connection_area for stop in stop_area.stops} epsg3857_bbox=shape.epsg3857_bbox,
connection_areas_len = len(connection_areas) epsg3857_points=shape.epsg3857_points,
if connection_areas_len == 1: )
connection_area = connection_areas.pop()
else:
prefix = "More than one" if connection_areas_len else "No"
msg = f"{prefix} connection area has been found for stop area #{stop_id}"
raise HTTPException(status_code=500, detail=msg)
if (
connection_area is not None
and (shape := await StopShape.get_by_id(connection_area.id)) is not None
):
return StopShapeSchema(
id=shape.id,
type=shape.type,
epsg3857_bbox=shape.epsg3857_bbox,
epsg3857_points=shape.epsg3857_points,
)
msg = f"No shape found for stop {stop_id}" msg = f"No shape found for stop {stop_id}"
raise HTTPException(status_code=404, detail=msg) raise HTTPException(status_code=404, detail=msg)