Accélération import résultats bureaux vote

This commit is contained in:
Emmy D'Anello 2024-06-21 11:06:00 +02:00
parent afc1d3f85f
commit d78ac420d8
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -13,6 +13,12 @@ def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/a1f73b85-8194-44f4-a2b7-c343edb47d32"),
(2, "https://www.data.gouv.fr/fr/datasets/r/96ffddda-59b4-41b8-a6a3-dfe1adb7fa36")]
with Session(engine) as session:
candidats = {(candidat.circonscription_id, candidat.numero): candidat
for candidat in session.execute(select(CandidatLegislatives2022)).scalars().all()}
bv_ids = {(bv.commune_code, bv.code_bureau): bv.id
for bv in session.execute(select(BureauVote)).scalars().all()}
for tour, file_url in tours:
file = get_file("https://www.data.gouv.fr/fr/datasets/r/a1f73b85-8194-44f4-a2b7-c343edb47d32",
"resultats-legislatives-2022-t1-par-bureau-de-vote.csv")
@ -61,12 +67,10 @@ def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
bv_code = row[6]
if bv := session.execute(select(BureauVote).filter_by(commune_code=com_code, code_bureau=bv_code)) \
.scalar_one_or_none():
bv_id = bv.id
else:
if (com_code, bv_code) not in bv_ids:
print(f"Bureau de vote {com_code}_{bv_code} non trouvé")
continue
bv_id = bv_ids[(com_code, bv_code)]
circo_code = row[2]
circo_id = f"{dpt_code.zfill(2)}-{circo_code.zfill(2)}"
@ -144,9 +148,7 @@ def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
numero = bloc_voix[0]
voix = int(bloc_voix[5])
candidat = session.execute(select(CandidatLegislatives2022)
.filter_by(circonscription_id=circo_id, numero=numero)) \
.scalar_one_or_none()
candidat = candidats[(circo_id, int(numero))]
voix_candidat_bv = session.execute(select(VoixBureauVoteLegislatives2022)
.filter_by(resultats_bureau_vote_id=resultats_bv.id,