From 8ccd417b53b26e99cb7b32bddf761d790836e150 Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Wed, 4 Sep 2024 17:30:41 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F(api)=20require=20code=5Fi?= =?UTF-8?q?nsee=5Fcommune=20field=20for=20Localisation=20and=20Statique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As stated in issue 103 [1], we need the code_insee_commune field to be filled so that we can link a station's localisation to administrative levels and validate submitted coordinates. 1. https://github.com/MTES-MCT/qualicharge/issues/103 --- src/api/CHANGELOG.md | 2 ++ ...22f_localisation_code_insee_commune_is_.py | 35 +++++++++++++++++++ src/api/qualicharge/models/static.py | 4 +-- src/api/qualicharge/schemas/core.py | 2 +- src/api/tests/schemas/test_static.py | 2 ++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/api/qualicharge/migrations/versions/d198170d222f_localisation_code_insee_commune_is_.py diff --git a/src/api/CHANGELOG.md b/src/api/CHANGELOG.md index b3add196..2e071796 100644 --- a/src/api/CHANGELOG.md +++ b/src/api/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to ### Changed +- Require the `code_insee_commune` field in both the `Statique` model and the + `Localisation` schema - Allow to submit a single item in bulk endpoints - Add create or update support for the `/statique/bulk` endpoints (with improved performances) diff --git a/src/api/qualicharge/migrations/versions/d198170d222f_localisation_code_insee_commune_is_.py b/src/api/qualicharge/migrations/versions/d198170d222f_localisation_code_insee_commune_is_.py new file mode 100644 index 00000000..600f0d9d --- /dev/null +++ b/src/api/qualicharge/migrations/versions/d198170d222f_localisation_code_insee_commune_is_.py @@ -0,0 +1,35 @@ +"""localisation_code_insee_commune_is_required + +Revision ID: d198170d222f +Revises: d1e96368f232 +Create Date: 2024-09-04 15:20:57.493230 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = "d198170d222f" +down_revision: Union[str, None] = "d1e96368f232" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column( + "localisation", "code_insee_commune", existing_type=sa.VARCHAR(), nullable=False + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column( + "localisation", "code_insee_commune", existing_type=sa.VARCHAR(), nullable=True + ) + # ### end Alembic commands ### diff --git a/src/api/qualicharge/models/static.py b/src/api/qualicharge/models/static.py index a625af82..11c0aba4 100644 --- a/src/api/qualicharge/models/static.py +++ b/src/api/qualicharge/models/static.py @@ -134,9 +134,7 @@ class Statique(ModelSchemaMixin, BaseModel): nom_station: str implantation_station: ImplantationStationEnum adresse_station: str - code_insee_commune: Optional[ - Annotated[str, Field(pattern=r"^([013-9]\d|2[AB1-9])\d{3}$")] - ] + code_insee_commune: Annotated[str, Field(pattern=r"^([013-9]\d|2[AB1-9])\d{3}$")] coordonneesXY: DataGouvCoordinate nbre_pdc: PositiveInt id_pdc_itinerance: Annotated[ diff --git a/src/api/qualicharge/schemas/core.py b/src/api/qualicharge/schemas/core.py index e415c4e1..e6272f11 100644 --- a/src/api/qualicharge/schemas/core.py +++ b/src/api/qualicharge/schemas/core.py @@ -122,7 +122,7 @@ class Localisation(BaseTimestampedSQLModel, table=True): id: UUID = Field(default_factory=uuid4, primary_key=True) adresse_station: str = Field(unique=True) - code_insee_commune: Optional[str] = Field(regex=r"^([013-9]\d|2[AB1-9])\d{3}$") + code_insee_commune: str = Field(regex=r"^([013-9]\d|2[AB1-9])\d{3}$") coordonneesXY: DataGouvCoordinate = Field( sa_type=Geometry( geometry_type="POINT", diff --git a/src/api/tests/schemas/test_static.py b/src/api/tests/schemas/test_static.py index cd74acf9..719e54f9 100644 --- a/src/api/tests/schemas/test_static.py +++ b/src/api/tests/schemas/test_static.py @@ -46,6 +46,7 @@ def test_localisation_schema_set_geometry_point_validator(db_session): # Create and save a new location loc = Localisation( adresse_station="4 baker street 75000 Tatooine", + code_insee_commune="63455", coordonneesXY=Coordinate(longitude=3.129447, latitude=45.700327), ) db_session.add(loc) @@ -68,6 +69,7 @@ def test_localisation_schema_coordonneesXY_serializer(db_session): # Create and save a new location loc = Localisation( adresse_station="221B Baker street, London", + code_insee_commune="63455", coordonneesXY=Coordinate(longitude=-3.129447, latitude=45.700327), ) assert loc.model_dump(include={"coordonneesXY"}) == {