-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/pipeline/integration-fredo2
- Loading branch information
Showing
13 changed files
with
1,533 additions
and
1,038 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
api/src/alembic/versions/20240527_130609_9f9a66546e3a_add_fk_structure_commune.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""add-fk-structure-commune | ||
Revision ID: 9f9a66546e3a | ||
Revises: 170af30febde | ||
Create Date: 2024-05-27 13:06:09.931428 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
from data_inclusion.api.code_officiel_geo import constants | ||
from data_inclusion.api.code_officiel_geo.models import Commune | ||
from data_inclusion.api.inclusion_data.models import Service, Structure | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "9f9a66546e3a" | ||
down_revision = "170af30febde" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
conn = op.get_bind() | ||
|
||
# must clean up the data before adding the foreign key | ||
for model in [Structure, Service]: | ||
# remove district codes | ||
for k, v in constants._DISTRICTS_BY_CITY.items(): | ||
conn.execute( | ||
sa.update(model) | ||
.where(model.code_insee.startswith(v[0][:3])) | ||
.values({model.code_insee: k}) | ||
.returning(1) | ||
) | ||
|
||
# remove invalid codes | ||
conn.execute( | ||
sa.update(model) | ||
.where(model.code_insee.not_in(sa.select(Commune.code))) | ||
.values({model.code_insee: None}) | ||
) | ||
|
||
op.create_foreign_key( | ||
op.f("fk_api__structures__code_insee__api__communes"), | ||
"api__structures", | ||
"api__communes", | ||
["code_insee"], | ||
["code"], | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_constraint( | ||
op.f("fk_api__structures__code_insee__api__communes"), | ||
"api__structures", | ||
type_="foreignkey", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 155 additions & 113 deletions
268
api/src/data_inclusion/api/code_officiel_geo/constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from data_inclusion.api.code_officiel_geo import constants | ||
|
||
|
||
def get_departement_by_code_or_slug( | ||
code: constants.DepartementCodeEnum | None = None, | ||
slug: constants.DepartementSlugEnum | None = None, | ||
) -> constants.Departement | None: | ||
if code is not None: | ||
return constants.DepartementEnum[code.name].value | ||
if slug is not None: | ||
return constants.DepartementEnum[slug.name].value | ||
|
||
|
||
def get_region_by_code_or_slug( | ||
code: constants.RegionCodeEnum | None = None, | ||
slug: constants.RegionSlugEnum | None = None, | ||
) -> constants.Region | None: | ||
if code is not None: | ||
return constants.RegionEnum[code.name].value | ||
if slug is not None: | ||
return constants.RegionEnum[slug.name].value |
Oops, something went wrong.