Skip to content

Commit

Permalink
Merge branch 'main' into feat/pipeline/integration-fredo2
Browse files Browse the repository at this point in the history
  • Loading branch information
hlecuyer authored Jul 18, 2024
2 parents 37f5d95 + 6295209 commit e6ede5b
Show file tree
Hide file tree
Showing 13 changed files with 1,533 additions and 1,038 deletions.
1 change: 1 addition & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ fastapi = ["fastapi", "fastapi_pagination"]
markers = '''
with_token: inject token headers in test client
with_admin_token: inject admin token headers in test client
feature_deprecated: the marked test is deprecated
'''
testpaths = "tests"
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",
)
10 changes: 10 additions & 0 deletions api/src/data_inclusion/api/api_description.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ Les données data·inclusion sont issues d'un ensemble de sources (emplois de l'

Le endpoint `/sources` permet de lister les sources disponibles.


### Filtrer géographiquement les données

Les données renvoyées par certains endpoints peuvent être filtrées géographiquement.

Les codes communes, départements et régions utilisés sont issus du [code officiel géographique produit par l'INSEE](https://www.insee.fr/fr/information/2560452).

L'[api de la base adresse nationale](https://adresse.data.gouv.fr/api-doc/adresse) peut être utilisée afin d'automatiser l'identification de codes insee associés à partir d'adresses ou de parties d'adresses (e.g. nom de commune, code postal).


### Nous contacter

#### via notre [formulaire de contact](https://tally.so/r/w7N6Zz)
Expand Down
268 changes: 155 additions & 113 deletions api/src/data_inclusion/api/code_officiel_geo/constants.py
Original file line number Diff line number Diff line change
@@ -1,127 +1,169 @@
"""This module contains constants related to the official geographical codes.
cf https://www.insee.fr/fr/information/2560452
"""

from dataclasses import dataclass
from enum import Enum


@dataclass(frozen=True)
class _Departement:
class Departement:
slug: str
cog: str


_departements_dict = {
"AIN": _Departement("ain", "01"),
"AISNE": _Departement("aisne", "02"),
"ALLIER": _Departement("allier", "03"),
"ALPES_DE_HAUTE_PROVENCE": _Departement("alpes-de-haute-provence", "04"),
"HAUTES_ALPES": _Departement("hautes-alpes", "05"),
"ALPES_MARITIMES": _Departement("alpes-maritimes", "06"),
"ARDECHE": _Departement("ardeche", "07"),
"ARDENNES": _Departement("ardennes", "08"),
"ARIEGE": _Departement("ariege", "09"),
"AUBE": _Departement("aube", "10"),
"AUDE": _Departement("aude", "11"),
"AVEYRON": _Departement("aveyron", "12"),
"BOUCHES_DU_RHONE": _Departement("bouches-du-rhone", "13"),
"CALVADOS": _Departement("calvados", "14"),
"CANTAL": _Departement("cantal", "15"),
"CHARENTE": _Departement("charente", "16"),
"CHARENTE_MARITIME": _Departement("charente-maritime", "17"),
"CHER": _Departement("cher", "18"),
"CORREZE": _Departement("correze", "19"),
"COTE_D_OR": _Departement("cote-d-or", "21"),
"COTES_D_ARMOR": _Departement("cotes-d-armor", "22"),
"CREUSE": _Departement("creuse", "23"),
"DORDOGNE": _Departement("dordogne", "24"),
"DOUBS": _Departement("doubs", "25"),
"DROME": _Departement("drome", "26"),
"EURE": _Departement("eure", "27"),
"EURE_ET_LOIR": _Departement("eure-et-loir", "28"),
"FINISTERE": _Departement("finistere", "29"),
"CORSE_DU_SUD": _Departement("corse-du-sud", "2A"),
"HAUTE_CORSE": _Departement("haute-corse", "2B"),
"GARD": _Departement("gard", "30"),
"HAUTE_GARONNE": _Departement("haute-garonne", "31"),
"GERS": _Departement("gers", "32"),
"GIRONDE": _Departement("gironde", "33"),
"HERAULT": _Departement("herault", "34"),
"ILLE_ET_VILAINE": _Departement("ille-et-vilaine", "35"),
"INDRE": _Departement("indre", "36"),
"INDRE_ET_LOIRE": _Departement("indre-et-loire", "37"),
"ISERE": _Departement("isere", "38"),
"JURA": _Departement("jura", "39"),
"LANDES": _Departement("landes", "40"),
"LOIR_ET_CHER": _Departement("loir-et-cher", "41"),
"LOIRE": _Departement("loire", "42"),
"HAUTE_LOIRE": _Departement("haute-loire", "43"),
"LOIRE_ATLANTIQUE": _Departement("loire-atlantique", "44"),
"LOIRET": _Departement("loiret", "45"),
"LOT": _Departement("lot", "46"),
"LOT_ET_GARONNE": _Departement("lot-et-garonne", "47"),
"LOZERE": _Departement("lozere", "48"),
"MAINE_ET_LOIRE": _Departement("maine-et-loire", "49"),
"MANCHE": _Departement("manche", "50"),
"MARNE": _Departement("marne", "51"),
"HAUTE_MARNE": _Departement("haute-marne", "52"),
"MAYENNE": _Departement("mayenne", "53"),
"MEURTHE_ET_MOSELLE": _Departement("meurthe-et-moselle", "54"),
"MEUSE": _Departement("meuse", "55"),
"MORBIHAN": _Departement("morbihan", "56"),
"MOSELLE": _Departement("moselle", "57"),
"NIEVRE": _Departement("nievre", "58"),
"NORD": _Departement("nord", "59"),
"OISE": _Departement("oise", "60"),
"ORNE": _Departement("orne", "61"),
"PAS_DE_CALAIS": _Departement("pas-de-calais", "62"),
"PUY_DE_DOME": _Departement("puy-de-dome", "63"),
"PYRENEES_ATLANTIQUES": _Departement("pyrenees-atlantiques", "64"),
"HAUTES_PYRENEES": _Departement("hautes-pyrenees", "65"),
"PYRENEES_ORIENTALES": _Departement("pyrenees-orientales", "66"),
"BAS_RHIN": _Departement("bas-rhin", "67"),
"HAUT_RHIN": _Departement("haut-rhin", "68"),
"RHONE": _Departement("rhone", "69"),
"HAUTE_SAONE": _Departement("haute-saone", "70"),
"SAONE_ET_LOIRE": _Departement("saone-et-loire", "71"),
"SARTHE": _Departement("sarthe", "72"),
"SAVOIE": _Departement("savoie", "73"),
"HAUTE_SAVOIE": _Departement("haute-savoie", "74"),
"PARIS": _Departement("paris", "75"),
"SEINE_MARITIME": _Departement("seine-maritime", "76"),
"SEINE_ET_MARNE": _Departement("seine-et-marne", "77"),
"YVELINES": _Departement("yvelines", "78"),
"DEUX_SEVRES": _Departement("deux-sevres", "79"),
"SOMME": _Departement("somme", "80"),
"TARN": _Departement("tarn", "81"),
"TARN_ET_GARONNE": _Departement("tarn-et-garonne", "82"),
"VAR": _Departement("var", "83"),
"VAUCLUSE": _Departement("vaucluse", "84"),
"VENDEE": _Departement("vendee", "85"),
"VIENNE": _Departement("vienne", "86"),
"HAUTE_VIENNE": _Departement("haute-vienne", "87"),
"VOSGES": _Departement("vosges", "88"),
"YONNE": _Departement("yonne", "89"),
"TERRITOIRE_DE_BELFORT": _Departement("territoire-de-belfort", "90"),
"ESSONNE": _Departement("essonne", "91"),
"HAUTS_DE_SEINE": _Departement("hauts-de-seine", "92"),
"SEINE_SAINT_DENIS": _Departement("seine-saint-denis", "93"),
"VAL_DE_MARNE": _Departement("val-de-marne", "94"),
"VAL_D_OISE": _Departement("val-d-oise", "95"),
"GUADELOUPE": _Departement("guadeloupe", "971"),
"MARTINIQUE": _Departement("martinique", "972"),
"GUYANE": _Departement("guyane", "973"),
"LA_REUNION": _Departement("la-reunion", "974"),
"MAYOTTE": _Departement("mayotte", "976"),
}
code: str


class DepartementEnum(Enum):
AIN = Departement("ain", "01")
AISNE = Departement("aisne", "02")
ALLIER = Departement("allier", "03")
ALPES_DE_HAUTE_PROVENCE = Departement("alpes-de-haute-provence", "04")
ALPES_MARITIMES = Departement("alpes-maritimes", "06")
ARDECHE = Departement("ardeche", "07")
ARDENNES = Departement("ardennes", "08")
ARIEGE = Departement("ariege", "09")
AUBE = Departement("aube", "10")
AUDE = Departement("aude", "11")
AVEYRON = Departement("aveyron", "12")
BAS_RHIN = Departement("bas-rhin", "67")
BOUCHES_DU_RHONE = Departement("bouches-du-rhone", "13")
CALVADOS = Departement("calvados", "14")
CANTAL = Departement("cantal", "15")
CHARENTE_MARITIME = Departement("charente-maritime", "17")
CHARENTE = Departement("charente", "16")
CHER = Departement("cher", "18")
CORREZE = Departement("correze", "19")
CORSE_DU_SUD = Departement("corse-du-sud", "2A")
COTE_D_OR = Departement("cote-d-or", "21")
COTES_D_ARMOR = Departement("cotes-d-armor", "22")
CREUSE = Departement("creuse", "23")
DEUX_SEVRES = Departement("deux-sevres", "79")
DORDOGNE = Departement("dordogne", "24")
DOUBS = Departement("doubs", "25")
DROME = Departement("drome", "26")
ESSONNE = Departement("essonne", "91")
EURE_ET_LOIR = Departement("eure-et-loir", "28")
EURE = Departement("eure", "27")
FINISTERE = Departement("finistere", "29")
GARD = Departement("gard", "30")
GERS = Departement("gers", "32")
GIRONDE = Departement("gironde", "33")
GUADELOUPE = Departement("guadeloupe", "971")
GUYANE = Departement("guyane", "973")
HAUT_RHIN = Departement("haut-rhin", "68")
HAUTE_CORSE = Departement("haute-corse", "2B")
HAUTE_GARONNE = Departement("haute-garonne", "31")
HAUTE_LOIRE = Departement("haute-loire", "43")
HAUTE_MARNE = Departement("haute-marne", "52")
HAUTE_SAONE = Departement("haute-saone", "70")
HAUTE_SAVOIE = Departement("haute-savoie", "74")
HAUTE_VIENNE = Departement("haute-vienne", "87")
HAUTES_ALPES = Departement("hautes-alpes", "05")
HAUTES_PYRENEES = Departement("hautes-pyrenees", "65")
HAUTS_DE_SEINE = Departement("hauts-de-seine", "92")
HERAULT = Departement("herault", "34")
ILLE_ET_VILAINE = Departement("ille-et-vilaine", "35")
INDRE_ET_LOIRE = Departement("indre-et-loire", "37")
INDRE = Departement("indre", "36")
ISERE = Departement("isere", "38")
JURA = Departement("jura", "39")
LA_REUNION = Departement("la-reunion", "974")
LANDES = Departement("landes", "40")
LOIR_ET_CHER = Departement("loir-et-cher", "41")
LOIRE_ATLANTIQUE = Departement("loire-atlantique", "44")
LOIRE = Departement("loire", "42")
LOIRET = Departement("loiret", "45")
LOT_ET_GARONNE = Departement("lot-et-garonne", "47")
LOT = Departement("lot", "46")
LOZERE = Departement("lozere", "48")
MAINE_ET_LOIRE = Departement("maine-et-loire", "49")
MANCHE = Departement("manche", "50")
MARNE = Departement("marne", "51")
MARTINIQUE = Departement("martinique", "972")
MAYENNE = Departement("mayenne", "53")
MAYOTTE = Departement("mayotte", "976")
MEURTHE_ET_MOSELLE = Departement("meurthe-et-moselle", "54")
MEUSE = Departement("meuse", "55")
MORBIHAN = Departement("morbihan", "56")
MOSELLE = Departement("moselle", "57")
NIEVRE = Departement("nievre", "58")
NORD = Departement("nord", "59")
OISE = Departement("oise", "60")
ORNE = Departement("orne", "61")
PARIS = Departement("paris", "75")
PAS_DE_CALAIS = Departement("pas-de-calais", "62")
PUY_DE_DOME = Departement("puy-de-dome", "63")
PYRENEES_ATLANTIQUES = Departement("pyrenees-atlantiques", "64")
PYRENEES_ORIENTALES = Departement("pyrenees-orientales", "66")
RHONE = Departement("rhone", "69")
SAONE_ET_LOIRE = Departement("saone-et-loire", "71")
SARTHE = Departement("sarthe", "72")
SAVOIE = Departement("savoie", "73")
SEINE_ET_MARNE = Departement("seine-et-marne", "77")
SEINE_MARITIME = Departement("seine-maritime", "76")
SEINE_SAINT_DENIS = Departement("seine-saint-denis", "93")
SOMME = Departement("somme", "80")
TARN_ET_GARONNE = Departement("tarn-et-garonne", "82")
TARN = Departement("tarn", "81")
TERRITOIRE_DE_BELFORT = Departement("territoire-de-belfort", "90")
VAL_D_OISE = Departement("val-d-oise", "95")
VAL_DE_MARNE = Departement("val-de-marne", "94")
VAR = Departement("var", "83")
VAUCLUSE = Departement("vaucluse", "84")
VENDEE = Departement("vendee", "85")
VIENNE = Departement("vienne", "86")
VOSGES = Departement("vosges", "88")
YONNE = Departement("yonne", "89")
YVELINES = Departement("yvelines", "78")


DepartementSlugEnum = Enum(
"DepartementSlugEnum",
{member.name: member.value.slug for member in DepartementEnum},
)
DepartementCodeEnum = Enum(
"DepartementCodeEnum",
{member.name: member.value.code for member in DepartementEnum},
)


@dataclass(frozen=True)
class Region:
slug: str
code: str


class RegionEnum(Enum):
AUVERGNE_RHONE_ALPES = Region("auvergne-rhone-alpes", "84")
BOURGOGNE_FRANCHE_COMTE = Region("bourgogne-franche-comte", "27")
BRETAGNE = Region("bretagne", "53")
CENTRE_VAL_DE_LOIRE = Region("centre-val-de-loire", "24")
CORSE = Region("corse", "94")
GRAND_EST = Region("grand-est", "44")
GUADELOUPE = Region("guadeloupe", "01")
GUYANE = Region("guyane", "03")
HAUTS_DE_FRANCE = Region("hauts-de-france", "32")
ILE_DE_FRANCE = Region("ile-de-france", "11")
LA_REUNION = Region("la-reunion", "04")
MARTINIQUE = Region("martinique", "02")
MAYOTTE = Region("mayotte", "06")
NORMANDIE = Region("normandie", "28")
NOUVELLE_AQUITAINE = Region("nouvelle-aquitaine", "75")
OCCITANIE = Region("occitanie", "76")
PAYS_DE_LA_LOIRE = Region("pays-de-la-loire", "52")
PROVENCE_ALPES_COTE_D_AZUR = Region("provence-alpes-cote-d-azur", "93")

DepartementSlug = Enum(
"DepartementSlug",
{k: departement.slug for k, departement in _departements_dict.items()},

RegionSlugEnum = Enum(
"RegionSlugEnum",
{member.name: member.value.slug for member in RegionEnum},
)
DepartementCOG = Enum(
"DepartementCOG",
{k: departement.cog for k, departement in _departements_dict.items()},
RegionCodeEnum = Enum(
"RegionCodeEnum",
{member.name: member.value.code for member in RegionEnum},
)


# based on
# https://github.com/gip-inclusion/dora-back/blob/main/dora/admin_express/utils.py

Expand Down
21 changes: 21 additions & 0 deletions api/src/data_inclusion/api/code_officiel_geo/utils.py
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
Loading

0 comments on commit e6ede5b

Please sign in to comment.