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 Circonscription, BureauVote
from nupes.models.europeennes2024 import *
DATA_DIR = Path(__file__).parent.parent.parent / 'data'
DATA_DIR = Path(__file__).parent.parent.parent.parent / 'data'
def exporter_listes(engine: Engine, verbose: bool = False) -> None:
@ -17,7 +17,11 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None:
blocs_json = []
for bloc in blocs:
bloc_json = {'id': bloc.id, 'nom': bloc.nom, 'couleur': bloc.couleur}
bloc_json = {
'id': bloc.id,
'nom': bloc.nom,
'couleur': bloc.couleur,
}
blocs_json.append(bloc_json)
file = DATA_DIR / "resultats" / "europeennes" / "2024" / "blocs.json"
@ -31,7 +35,11 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None:
nuances_json = []
for nuance in nuances:
nuance_json = {'code': nuance.code, 'nom': nuance.nom, 'couleur': nuance.couleur}
nuance_json = {
'code': nuance.code,
'nom': nuance.nom,
'couleur': nuance.couleur,
}
nuances_json.append(nuance_json)
file = DATA_DIR / "resultats" / "europeennes" / "2024" / "nuances.json"
@ -46,13 +54,25 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None:
for liste in listes:
candidats = [
{'ordre': candidat.ordre, 'nom': candidat.nom, 'prenom': candidat.prenom, 'sexe': candidat.sexe.value,
'date_naissance': candidat.date_naissance.isoformat(), 'profession': candidat.profession,
'code_personnalite': candidat.code_personnalite.value, 'sortant': candidat.sortant}
{
'ordre': candidat.ordre,
'nom': candidat.nom,
'prenom': candidat.prenom,
'sexe': candidat.sexe.value,
'date_naissance': candidat.date_naissance.isoformat(),
'profession': candidat.profession,
'code_personnalite': candidat.code_personnalite.value,
'sortant': candidat.sortant
}
for candidat in liste.candidats
]
liste_json = {'numero': liste.numero, 'nom': liste.nom, 'nuance': liste.nuance_id,
'bloc': liste.bloc.nom, 'candidats': candidats}
liste_json = {
'numero': liste.numero,
'nom': liste.nom,
'nuance': liste.nuance_id,
'bloc': liste.bloc.nom,
'candidats': candidats,
}
listes_json.append(liste_json)
file = DATA_DIR / "resultats" / "europeennes" / "2024" / "listes.json"
@ -71,20 +91,21 @@ 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,
"votants": resultats_france.votants,
"abstentions": resultats_france.abstentions,
"exprimes": resultats_france.exprimes,
"blancs": resultats_france.blancs,
"nuls": resultats_france.nuls,
'tour1': {
'inscrits': resultats_france.inscrits,
'votants': resultats_france.votants,
'abstentions': resultats_france.abstentions,
'exprimes': resultats_france.exprimes,
'blancs': resultats_france.blancs,
'nuls': resultats_france.nuls,
},
'geometry': {},
}
resultats_listes = {}
@ -109,12 +130,17 @@ 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(ResultatsRegionEuropeennes2024)
@ -126,7 +152,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,
'votants': resultats_region.votants,
@ -135,7 +161,6 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
'blancs': resultats_region.blancs,
'nuls': resultats_region.nuls,
},
'geometry': region.geometry,
}
resultats_listes = {}
@ -167,10 +192,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(
@ -184,7 +214,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,
'votants': resultats_departement.votants,
@ -193,7 +223,6 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No
'blancs': resultats_departement.blancs,
'nuls': resultats_departement.nuls,
},
'geometry': departement.geometry,
}
resultats_listes = {}
@ -225,9 +254,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(
@ -242,7 +276,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,
'votants': resultats_circonscription.votants,
@ -251,7 +285,6 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -
'blancs': resultats_circonscription.blancs,
'nuls': resultats_circonscription.nuls,
},
'geometry': circonscription.geometry,
}
resultats_listes = {}
@ -284,9 +317,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(
@ -299,7 +337,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,
'votants': resultats_commune.votants,
@ -308,7 +346,6 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
'blancs': resultats_commune.blancs,
'nuls': resultats_commune.nuls,
},
'geometry': commune.geometry,
}
resultats_listes = {}
@ -340,10 +377,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(
@ -356,7 +397,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,
'votants': resultats_bureau_vote.votants,
@ -365,7 +406,6 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No
'blancs': resultats_bureau_vote.blancs,
'nuls': resultats_bureau_vote.nuls,
},
'geometry': bureau_vote.geometry,
}
resultats_listes = {}

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 = {}, {}