Séparation des résultats par tour

This commit is contained in:
2024-06-20 18:36:52 +02:00
parent 63e2366837
commit 3d60013670
5 changed files with 243 additions and 157 deletions

View File

@ -8,7 +8,7 @@ from tqdm import tqdm
from nupes.models.geographie import BureauVote
from nupes.models.legislatives2022 import *
DATA_DIR = Path(__file__).parent.parent.parent / 'data'
DATA_DIR = Path(__file__).parent.parent.parent.parent / 'data'
def exporter_nuances(engine: Engine, verbose: bool = False) -> None:
@ -77,10 +77,12 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None:
session.add(resultats_france)
resultats_dict = {
'france': {
'zone': {
'type': "france",
'regions': [reg.code_insee for reg in session.execute(select(Region)).scalars().all()],
'departements': [dpt.code_insee for dpt in session.execute(select(Departement)).scalars().all()],
'circonscriptions': [circo.id for circo in session.execute(select(Circonscription)).scalars().all()],
'geometry': {},
},
'tour1': {
'inscrits': resultats_france.inscrits_t1,
@ -98,7 +100,6 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None:
'blancs': resultats_france.blancs_t2,
'nuls': resultats_france.nuls_t2,
},
'geometry': {},
}
resultats_t1, resultats_t2 = {}, {}
@ -125,12 +126,18 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
regions_iterator = tqdm(regions, desc="Régions") if verbose else regions
for region in regions_iterator:
region_json = {'code_insee': region.code_insee, 'nom': region.libelle,
'departements': [dpt.code_insee for dpt in region.departements],
'circonscriptions':
[circo.id for circo in session.execute(
select(Circonscription).join(Departement).filter_by(region_code=region.code_insee))
.scalars().all()]}
region_json = {
'type': "region",
'code_insee': region.code_insee,
'nom': region.libelle,
'departements': [dpt.code_insee for dpt in region.departements],
'circonscriptions': [
circo.id for circo in session.execute(
select(Circonscription).join(Departement).filter_by(region_code=region.code_insee))
.scalars().all()
],
'geometry': region.geometry,
}
regions_json.append(region_json)
resultats_region = session.execute(select(ResultatsRegionLegislatives2022)
@ -142,7 +149,7 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
session.add(resultats_region)
resultats_dict = {
'region': region_json,
'zone': region_json,
'tour1': {
'inscrits': resultats_region.inscrits_t1,
'votants': resultats_region.votants_t1,
@ -159,7 +166,6 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
'blancs': resultats_region.blancs_t2,
'nuls': resultats_region.nuls_t2,
},
'geometry': region.geometry,
}
resultats_t1, resultats_t2 = {}, {}
@ -193,10 +199,15 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No
iterator = tqdm(departements, desc="Départements") if verbose else departements
for departement in iterator:
departement_json = {'code_insee': departement.code_insee, 'nom': departement.libelle,
'region': departement.region_code,
'circonscriptions': [circo.id for circo in departement.circonscriptions],
'communes': [commune.code_insee for commune in departement.communes]}
departement_json = {
'type': "departement",
'code_insee': departement.code_insee,
'nom': departement.libelle,
'region': departement.region_code,
'circonscriptions': [circo.id for circo in departement.circonscriptions],
'communes': [commune.code_insee for commune in departement.communes],
'geometry': departement.geometry,
}
departements_json.append(departement_json)
resultats_departement = session.execute(
@ -210,7 +221,7 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No
session.add(resultats_departement)
resultats_dict = {
'departement': departement_json,
'zone': departement_json,
'tour1': {
'inscrits': resultats_departement.inscrits_t1,
'votants': resultats_departement.votants_t1,
@ -227,7 +238,6 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No
'blancs': resultats_departement.blancs_t2,
'nuls': resultats_departement.nuls_t2,
},
'geometry': departement.geometry,
}
resultats_t1, resultats_t2 = {}, {}
@ -261,9 +271,14 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -
iterator = tqdm(circonscriptions, desc="Circonscriptions") if verbose else circonscriptions
for circonscription in iterator:
circonscription_json = {'id': circonscription.id, 'departement': circonscription.departement_code,
'numero': circonscription.numero,
'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote]}
circonscription_json = {
'type': "circonscription",
'id': circonscription.id,
'departement': circonscription.departement_code,
'numero': circonscription.numero,
'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote],
'geometry': circonscription.geometry,
}
circonscriptions_json.append(circonscription_json)
resultats_circonscription = session.execute(
@ -278,7 +293,7 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -
session.add(resultats_circonscription)
resultats_dict = {
'circonscription': circonscription_json,
'zone': circonscription_json,
'tour1': {
'inscrits': resultats_circonscription.inscrits_t1,
'votants': resultats_circonscription.votants_t1,
@ -295,7 +310,6 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -
'blancs': resultats_circonscription.blancs_t2,
'nuls': resultats_circonscription.nuls_t2,
},
'geometry': circonscription.geometry,
}
resultats_t1, resultats_t2 = {}, {}
@ -330,9 +344,14 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
iterator = tqdm(communes, desc="Communes") if verbose else communes
for commune in iterator:
commune_json = {'code_insee': commune.code_insee, 'nom': commune.libelle,
'departement': commune.departement_code,
'bureaux_vote': [bv.id for bv in commune.bureaux_vote]}
commune_json = {
'type': "commune",
'code_insee': commune.code_insee,
'nom': commune.libelle,
'departement': commune.departement_code,
'bureaux_vote': [bv.id for bv in commune.bureaux_vote],
'geometry': commune.geometry,
}
communes_json.append(commune_json)
resultats_commune = session.execute(
@ -345,7 +364,7 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
session.add(resultats_commune)
resultats_dict = {
'commune': commune_json,
'zone': commune_json,
'tour1': {
'inscrits': resultats_commune.inscrits_t1,
'votants': resultats_commune.votants_t1,
@ -362,7 +381,6 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
'blancs': resultats_commune.blancs_t2,
'nuls': resultats_commune.nuls_t2,
},
'geometry': commune.geometry,
}
resultats_t1, resultats_t2 = {}, {}
@ -396,10 +414,14 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No
iterator = tqdm(bureaux_vote, desc="Bureaux de vote") if verbose else bureaux_vote
for bureau_vote in iterator:
bureau_vote_json = {'id': bureau_vote.id,
'libelle': bureau_vote.libelle,
'commune': bureau_vote.commune_code,
'circonscription': bureau_vote.circo_code}
bureau_vote_json = {
'type': "bureau_vote",
'id': bureau_vote.id,
'libelle': bureau_vote.libelle,
'commune': bureau_vote.commune_code,
'circonscription': bureau_vote.circo_code,
'geometry': bureau_vote.geometry,
}
bureaux_vote_json.append(bureau_vote_json)
resultats_bureau_vote = session.execute(
@ -412,7 +434,7 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No
session.add(resultats_bureau_vote)
resultats_dict = {
'bureau_vote': bureau_vote_json,
'zone': bureau_vote_json,
'tour1': {
'inscrits': resultats_bureau_vote.inscrits_t1,
'votants': resultats_bureau_vote.votants_t1,
@ -429,7 +451,6 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No
'blancs': resultats_bureau_vote.blancs_t2,
'nuls': resultats_bureau_vote.nuls_t2,
},
'geometry': bureau_vote.geometry,
}
resultats_t1, resultats_t2 = {}, {}