Correction Saint-Martin/Saint-Barthélémy

This commit is contained in:
Emmy D'Anello 2024-06-15 20:35:47 +02:00
parent b97dc60f2a
commit 18092fbb09
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -170,17 +170,19 @@ def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None:
for row in tqdm(reader, desc="Circonscription", disable=not verbose): for row in tqdm(reader, desc="Circonscription", disable=not verbose):
circo_code = row['Code circonscription législative'] circo_code = row['Code circonscription législative']
dpt_code, circo_code = circo_code[:-2], circo_code[-2:] dpt_code, circo_code = circo_code[:-2], circo_code[-2:]
if dpt_code == "ZX":
dpt_code = "977"
circo_id = f"{dpt_code.zfill(2)}-{circo_code.zfill(2)}" circo_id = f"{dpt_code.zfill(2)}-{circo_code.zfill(2)}"
resultats_circo = session.execute(select(ResultatsCirconscription).filter_by(circo_id=circo_id)) \ resultats_circo = session.execute(select(ResultatsCirconscription).filter_by(circo_id=circo_id)) \
.scalar_one_or_none() .scalar_one_or_none()
if not resultats_circo: if not resultats_circo:
resultats_dpt = session.execute(select(ResultatsDepartement) resultats_dpt = session.execute(select(ResultatsDepartement).filter_by(dpt_id=dpt_code)) \
.filter_by(dpt_id=row['Code département'])) \
.scalar_one_or_none() .scalar_one_or_none()
if not resultats_dpt: if not resultats_dpt:
dpt = session.execute(select(Departement) dpt = session.execute(select(Departement).filter_by(code_insee=f"{dpt_code.zfill(2)}")) \
.filter_by(code_insee=f"{row['Code département'].zfill(2)}")) \
.scalar_one() .scalar_one()
resultats_reg = session.execute(select(ResultatsRegion) resultats_reg = session.execute(select(ResultatsRegion)
.filter_by(region_id=dpt.region_code)) \ .filter_by(region_id=dpt.region_code)) \
@ -197,11 +199,11 @@ def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None:
resultats_reg = session.execute(select(ResultatsRegion) resultats_reg = session.execute(select(ResultatsRegion)
.filter_by(region_id=dpt.region_code)).scalar_one() .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) resultats_region_id=resultats_reg.id)
session.add(resultats_dpt) session.add(resultats_dpt)
resultats_dpt = session.execute(select(ResultatsDepartement) 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_circo = ResultatsCirconscription(circo_id=circo_id, resultats_circo = ResultatsCirconscription(circo_id=circo_id,
resultats_departement_id=resultats_dpt.id) resultats_departement_id=resultats_dpt.id)