Correction import résultats Saint-Martin/Saint-Barthélémy

This commit is contained in:
Emmy D'Anello 2024-06-15 20:39:42 +02:00
parent 18092fbb09
commit ab9267cb13
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 8 additions and 4 deletions

View File

@ -102,16 +102,20 @@ def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
for row in tqdm(reader, desc="Commune", disable=not verbose):
com_code = str(row['Code commune']).zfill(5)
dpt_code = row['Code département']
if dpt_code == "ZX":
dpt_code = "977"
resultats_commune = session.execute(select(ResultatsCommune).filter_by(commune_id=com_code)) \
.scalar_one_or_none()
if not resultats_commune:
resultats_dpt = session.execute(select(ResultatsDepartement)
.filter_by(dpt_id=row['Code département'])) \
.filter_by(dpt_id=dpt_code)) \
.scalar_one_or_none()
if not resultats_dpt:
dpt = session.execute(select(Departement)
.filter_by(code_insee=f"{row['Code département'].zfill(2)}")) \
.filter_by(code_insee=f"{dpt_code.zfill(2)}")) \
.scalar_one()
resultats_reg = session.execute(select(ResultatsRegion)
.filter_by(region_id=dpt.region_code)) \
@ -128,11 +132,11 @@ def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
resultats_reg = session.execute(select(ResultatsRegion)
.filter_by(region_id=dpt.region_code)).scalar_one()
resultats_dpt = ResultatsDepartement(dpt_id=row['Code département'],
resultats_dpt = ResultatsDepartement(dpt_id=dpt_code,
resultats_region_id=resultats_reg.id)
session.add(resultats_dpt)
resultats_dpt = session.execute(select(ResultatsDepartement)
.filter_by(dpt_id=row['Code département'])).scalar_one()
.filter_by(dpt_id=dpt_code)).scalar_one()
resultats_commune = ResultatsCommune(commune_id=com_code, resultats_dpt_id=resultats_dpt.id)
session.add(resultats_commune)