Skip to content

Commit

Permalink
🗃️(api) require code_insee_commune field for Localisation and Statique
Browse files Browse the repository at this point in the history
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. #103
  • Loading branch information
jmaupetit committed Sep 5, 2024
1 parent b5ed26b commit 8ccd417
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ###
4 changes: 1 addition & 3 deletions src/api/qualicharge/models/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down
2 changes: 1 addition & 1 deletion src/api/qualicharge/schemas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/api/tests/schemas/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"}) == {
Expand Down

0 comments on commit 8ccd417

Please sign in to comment.