From 5da918c04b23ab288045ed83f9e351cea6e1596b Mon Sep 17 00:00:00 2001 From: Adrien Date: Sun, 11 Jun 2023 22:41:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BD=EF=B8=8F=20Take=20the=20last=20IDF?= =?UTF-8?q?M=20format=20into=20account?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/backend/idfm_interface/idfm_types.py | 14 +++++-- backend/backend/idfm_interface/ratp_types.py | 14 +------ backend/routers/stop.py | 44 ++++++-------------- 3 files changed, 26 insertions(+), 46 deletions(-) diff --git a/backend/backend/idfm_interface/idfm_types.py b/backend/backend/idfm_interface/idfm_types.py index e742171..c9530f4 100644 --- a/backend/backend/idfm_interface/idfm_types.py +++ b/backend/backend/idfm_interface/idfm_types.py @@ -116,19 +116,26 @@ class StopArea(Struct): record_timestamp: datetime -class ConnectionArea(Struct): +class ConnectionAreaFields(Struct, kw_only=True): zdcid: str zdcversion: str zdccreated: datetime zdcchanged: datetime zdcname: str - zdcxepsg2154: int - zdcyepsg2154: int + zdcxepsg2154: int | None = None + zdcyepsg2154: int | None = None zdctown: str zdcpostalregion: str zdctype: StopAreaType +class ConnectionArea(Struct): + datasetid: str + recordid: str + fields: ConnectionAreaFields + record_timestamp: datetime + + class StopAreaStopAssociationFields(Struct, kw_only=True): arrid: str # TODO: use int ? artid: str | None = None @@ -149,6 +156,7 @@ class StopAreaStopAssociation(Struct): class IdfmLineState(Enum): active = "active" + available_soon = "prochainement active" class LinePicto(Struct, rename={"id_": "id"}): diff --git a/backend/backend/idfm_interface/ratp_types.py b/backend/backend/idfm_interface/ratp_types.py index 222fad7..d9707ef 100644 --- a/backend/backend/idfm_interface/ratp_types.py +++ b/backend/backend/idfm_interface/ratp_types.py @@ -1,6 +1,3 @@ -from datetime import datetime -from typing import Optional - from msgspec import Struct @@ -13,13 +10,6 @@ class PictoFieldsFile(Struct, rename={"id_": "id"}): format: str -class PictoFields(Struct): - indices_commerciaux: str - noms_des_fichiers: Optional[PictoFieldsFile] = None - - class Picto(Struct): - datasetid: str - recordid: str - fields: PictoFields - record_timestamp: datetime + indices_commerciaux: str + noms_des_fichiers: PictoFieldsFile | None = None diff --git a/backend/routers/stop.py b/backend/routers/stop.py index a0cf645..1e59f8d 100644 --- a/backend/routers/stop.py +++ b/backend/routers/stop.py @@ -5,11 +5,7 @@ from typing import Sequence from fastapi import APIRouter, HTTPException from fastapi_cache.decorator import cache -from backend.idfm_interface import ( - Destinations as IdfmDestinations, - IdfmInterface, - TrainStatus, -) +from backend.idfm_interface import Destinations as IdfmDestinations, TrainStatus from backend.models import Stop, StopArea, StopShape from backend.schemas import ( 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. 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 except (AttributeError, TypeError, ValueError) as err: raise HTTPException( @@ -163,32 +159,18 @@ async def get_stop_destinations( @router.get("/{stop_id}/shape") @cache(namespace="stop-shape") 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: - connection_area = stop.connection_area - - elif (stop_area := await StopArea.get_by_id(stop_id)) is not None: - connection_areas = {stop.connection_area for stop in stop_area.stops} - connection_areas_len = len(connection_areas) - 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, - ) + if (shape := await StopShape.get_by_id(shape_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}" raise HTTPException(status_code=404, detail=msg)