Compare commits

...

2 Commits

Author SHA1 Message Date
ada3941d44
Import candidat⋅es législatives 2024 2024-06-20 16:43:42 +02:00
df8c8db32d
Export données législatives 2024 2024-06-20 16:05:24 +02:00
10 changed files with 1238 additions and 72 deletions

View File

@ -0,0 +1,259 @@
"""ajout modèles législatives 2024
Revision ID: 0222cf0ef39b
Revises: 99948a33112b
Create Date: 2024-06-20 16:34:47.315588
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '0222cf0ef39b'
down_revision: Union[str, None] = '99948a33112b'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('legislatives_2024_bloc',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nom', sa.String(length=32), nullable=False),
sa.Column('couleur', sa.String(length=7), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('nom')
)
op.create_table('legislatives_2024_resultats_france',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_nuance',
sa.Column('code', sa.String(length=8), nullable=False),
sa.Column('nom', sa.String(length=64), nullable=False),
sa.Column('couleur', sa.String(length=7), nullable=False),
sa.Column('bloc_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['bloc_id'], ['legislatives_2024_bloc.id'], ),
sa.PrimaryKeyConstraint('code'),
sa.UniqueConstraint('nom')
)
op.create_table('legislatives_2024_resultats_region',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('region_id', sa.String(length=3), nullable=False),
sa.Column('resultats_france_id', sa.Integer(), nullable=False),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['region_id'], ['region.code_insee'], ),
sa.ForeignKeyConstraint(['resultats_france_id'], ['legislatives_2024_resultats_france.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_resultats_departement',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('dpt_id', sa.String(length=3), nullable=False),
sa.Column('resultats_region_id', sa.Integer(), nullable=True),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['dpt_id'], ['departement.code_insee'], ),
sa.ForeignKeyConstraint(['resultats_region_id'], ['legislatives_2024_resultats_region.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_france',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nuance_id', sa.String(length=8), nullable=False),
sa.Column('resultats_france_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2024_nuance.code'], ),
sa.ForeignKeyConstraint(['resultats_france_id'], ['legislatives_2024_resultats_france.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_region',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nuance_id', sa.String(length=8), nullable=False),
sa.Column('resultats_region_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2024_nuance.code'], ),
sa.ForeignKeyConstraint(['resultats_region_id'], ['legislatives_2024_resultats_region.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_candidat',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('circonscription_id', sa.String(length=6), nullable=False),
sa.Column('numero', sa.Integer(), nullable=False),
sa.Column('nuance_id', sa.String(length=8), nullable=False),
sa.Column('bloc_id', sa.Integer(), nullable=False),
sa.Column('nom', sa.String(length=256), nullable=False),
sa.Column('prenom', sa.String(length=256), nullable=False),
sa.Column('sexe', sa.Enum('MASCULIN', 'FEMININ', name='genre'), nullable=False),
sa.Column('date_naissance', sa.Date(), nullable=False),
sa.Column('profession', sa.String(length=256), nullable=False),
sa.Column('sortant', sa.Boolean(), nullable=False),
sa.Column('nom_suppleance', sa.String(length=256), nullable=False),
sa.Column('prenom_suppleance', sa.String(length=256), nullable=False),
sa.Column('sexe_suppleance', sa.Enum('MASCULIN', 'FEMININ', name='genre'), nullable=False),
sa.Column('date_naissance_suppleance', sa.Date(), nullable=False),
sa.Column('sortant_suppleance', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['bloc_id'], ['legislatives_2024_bloc.id'], ),
sa.ForeignKeyConstraint(['circonscription_id'], ['circonscription.id'], ),
sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2024_nuance.code'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_resultats_circonscription',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('circo_id', sa.String(length=6), nullable=False),
sa.Column('resultats_departement_id', sa.Integer(), nullable=False),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['circo_id'], ['circonscription.id'], ),
sa.ForeignKeyConstraint(['resultats_departement_id'], ['legislatives_2024_resultats_departement.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_resultats_commune',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('commune_id', sa.String(length=5), nullable=False),
sa.Column('resultats_dpt_id', sa.Integer(), nullable=False),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['commune_id'], ['commune.code_insee'], ),
sa.ForeignKeyConstraint(['resultats_dpt_id'], ['legislatives_2024_resultats_departement.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_departement',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nuance_id', sa.String(length=8), nullable=False),
sa.Column('resultats_departement_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2024_nuance.code'], ),
sa.ForeignKeyConstraint(['resultats_departement_id'], ['legislatives_2024_resultats_departement.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_resultats_bureau_vote',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('bv_id', sa.String(length=16), nullable=False),
sa.Column('resultats_commune_id', sa.Integer(), nullable=False),
sa.Column('resultats_circo_id', sa.Integer(), nullable=True),
sa.Column('inscrits_t1', sa.Integer(), nullable=False),
sa.Column('votants_t1', sa.Integer(), nullable=False),
sa.Column('abstentions_t1', sa.Integer(), nullable=False),
sa.Column('exprimes_t1', sa.Integer(), nullable=False),
sa.Column('blancs_t1', sa.Integer(), nullable=False),
sa.Column('nuls_t1', sa.Integer(), nullable=False),
sa.Column('inscrits_t2', sa.Integer(), nullable=False),
sa.Column('votants_t2', sa.Integer(), nullable=False),
sa.Column('abstentions_t2', sa.Integer(), nullable=False),
sa.Column('exprimes_t2', sa.Integer(), nullable=False),
sa.Column('blancs_t2', sa.Integer(), nullable=False),
sa.Column('nuls_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['bv_id'], ['bureau_vote.id'], ),
sa.ForeignKeyConstraint(['resultats_circo_id'], ['legislatives_2024_resultats_circonscription.id'], ),
sa.ForeignKeyConstraint(['resultats_commune_id'], ['legislatives_2024_resultats_commune.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_circonscription',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('candidat_id', sa.Integer(), nullable=False),
sa.Column('resultats_circonscription_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['candidat_id'], ['legislatives_2024_candidat.id'], ),
sa.ForeignKeyConstraint(['resultats_circonscription_id'], ['legislatives_2024_resultats_circonscription.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_commune',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nuance_id', sa.String(length=8), nullable=False),
sa.Column('resultats_commune_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2024_nuance.code'], ),
sa.ForeignKeyConstraint(['resultats_commune_id'], ['legislatives_2024_resultats_commune.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('legislatives_2024_voix_bureau_vote',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('candidat_id', sa.Integer(), nullable=False),
sa.Column('resultats_bureau_vote_id', sa.Integer(), nullable=False),
sa.Column('voix_t1', sa.Integer(), nullable=False),
sa.Column('voix_t2', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['candidat_id'], ['legislatives_2024_candidat.id'], ),
sa.ForeignKeyConstraint(['resultats_bureau_vote_id'], ['legislatives_2024_resultats_bureau_vote.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('legislatives_2024_voix_bureau_vote')
op.drop_table('legislatives_2024_voix_commune')
op.drop_table('legislatives_2024_voix_circonscription')
op.drop_table('legislatives_2024_resultats_bureau_vote')
op.drop_table('legislatives_2024_voix_departement')
op.drop_table('legislatives_2024_resultats_commune')
op.drop_table('legislatives_2024_resultats_circonscription')
op.drop_table('legislatives_2024_candidat')
op.drop_table('legislatives_2024_voix_region')
op.drop_table('legislatives_2024_voix_france')
op.drop_table('legislatives_2024_resultats_departement')
op.drop_table('legislatives_2024_resultats_region')
op.drop_table('legislatives_2024_nuance')
op.drop_table('legislatives_2024_resultats_france')
op.drop_table('legislatives_2024_bloc')
# ### end Alembic commands ###

View File

@ -2,3 +2,4 @@ from .base import Base
from .geographie import * from .geographie import *
from .legislatives2022 import * from .legislatives2022 import *
from .europeennes2024 import * from .europeennes2024 import *
from .legislatives2024 import *

View File

@ -17,6 +17,7 @@ class Region(Base):
resultats_legislatives_2022 = relationship("ResultatsRegionLegislatives2022", back_populates="region") resultats_legislatives_2022 = relationship("ResultatsRegionLegislatives2022", back_populates="region")
resultats_europeennes_2024 = relationship("ResultatsRegionEuropeennes2024", back_populates="region") resultats_europeennes_2024 = relationship("ResultatsRegionEuropeennes2024", back_populates="region")
resultats_legislatives_2024 = relationship("ResultatsRegionLegislatives2024", back_populates="region")
class Departement(Base): class Departement(Base):
@ -33,6 +34,7 @@ class Departement(Base):
resultats_legislatives_2022 = relationship("ResultatsDepartementLegislatives2022", back_populates="departement") resultats_legislatives_2022 = relationship("ResultatsDepartementLegislatives2022", back_populates="departement")
resultats_europeennes_2024 = relationship("ResultatsDepartementEuropeennes2024", back_populates="departement") resultats_europeennes_2024 = relationship("ResultatsDepartementEuropeennes2024", back_populates="departement")
resultats_legislatives_2024 = relationship("ResultatsDepartementLegislatives2024", back_populates="departement")
class Commune(Base): class Commune(Base):
@ -48,6 +50,7 @@ class Commune(Base):
resultats_legislatives_2022 = relationship("ResultatsCommuneLegislatives2022", back_populates="commune") resultats_legislatives_2022 = relationship("ResultatsCommuneLegislatives2022", back_populates="commune")
resultats_europeennes_2024 = relationship("ResultatsCommuneEuropeennes2024", back_populates="commune") resultats_europeennes_2024 = relationship("ResultatsCommuneEuropeennes2024", back_populates="commune")
resultats_legislatives_2024 = relationship("ResultatsCommuneLegislatives2024", back_populates="commune")
class Circonscription(Base): class Circonscription(Base):
@ -63,9 +66,12 @@ class Circonscription(Base):
candidats_legislatives_2022 = relationship("CandidatLegislatives2022", back_populates="circonscription") candidats_legislatives_2022 = relationship("CandidatLegislatives2022", back_populates="circonscription")
resultats_legislatives_2022 = relationship("ResultatsCirconscriptionLegislatives2022", resultats_legislatives_2022 = relationship("ResultatsCirconscriptionLegislatives2022",
back_populates="circonscription") back_populates="circonscription")
resultats_europeennes_2024 = relationship("ResultatsCirconscriptionEuropeennes2024", resultats_europeennes_2024 = relationship("ResultatsCirconscriptionEuropeennes2024",
back_populates="circonscription") back_populates="circonscription")
candidats_legislatives_2024 = relationship("CandidatLegislatives2024", back_populates="circonscription")
resultats_legislatives_2024 = relationship("ResultatsCirconscriptionLegislatives2024",
back_populates="circonscription")
class BureauVote(Base): class BureauVote(Base):
@ -84,3 +90,4 @@ class BureauVote(Base):
resultats_legislatives_2022 = relationship("ResultatsBureauVoteLegislatives2022", back_populates="bureau_vote") resultats_legislatives_2022 = relationship("ResultatsBureauVoteLegislatives2022", back_populates="bureau_vote")
resultats_europeennes_2024 = relationship("ResultatsBureauVoteEuropeennes2024", back_populates="bureau_vote") resultats_europeennes_2024 = relationship("ResultatsBureauVoteEuropeennes2024", back_populates="bureau_vote")
resultats_legislatives_2024 = relationship("ResultatsBureauVoteLegislatives2024", back_populates="bureau_vote")

View File

@ -0,0 +1,333 @@
import enum
from datetime import date
from typing import List
from sqlalchemy import Boolean, Date, Enum, ForeignKey, Integer, String
from sqlalchemy.orm import mapped_column, Mapped, relationship
from nupes.models import Base, Region, Departement, Commune, Circonscription
class BlocLegislatives2024(Base):
__tablename__ = "legislatives_2024_bloc"
id: Mapped[int] = mapped_column(primary_key=True)
nom: Mapped[str] = mapped_column(String(32), unique=True)
couleur: Mapped[str] = mapped_column(String(7))
candidats: Mapped[List["CandidatLegislatives2024"]] = relationship("CandidatLegislatives2024",
back_populates="bloc")
nuances: Mapped[List["NuanceLegislatives2024"]] = relationship(
"NuanceLegislatives2024", back_populates="bloc")
class NuanceLegislatives2024(Base):
__tablename__ = "legislatives_2024_nuance"
code: Mapped[str] = mapped_column(String(8), primary_key=True)
nom: Mapped[str] = mapped_column(String(64), unique=True)
couleur: Mapped[str] = mapped_column(String(7))
bloc_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_bloc.id"))
bloc: Mapped[BlocLegislatives2024] = relationship(BlocLegislatives2024, back_populates="nuances")
candidats: Mapped[List["CandidatLegislatives2024"]] = relationship(
"CandidatLegislatives2024", back_populates="nuance")
resultats_nationaux: Mapped[List["VoixFranceLegislatives2024"]] = relationship(
"VoixFranceLegislatives2024", back_populates="nuance")
resultats_par_region: Mapped[List["VoixRegionLegislatives2024"]] = relationship(
"VoixRegionLegislatives2024", back_populates="nuance")
resultats_par_departement: Mapped[List["VoixDepartementLegislatives2024"]] = relationship(
"VoixDepartementLegislatives2024", back_populates="nuance")
resultats_par_commune: Mapped[List["VoixCommuneLegislatives2024"]] = relationship(
"VoixCommuneLegislatives2024", back_populates="nuance")
class CandidatLegislatives2024(Base):
class Genre(enum.Enum):
MASCULIN = "M"
FEMININ = "F"
__tablename__ = "legislatives_2024_candidat"
id: Mapped[int] = mapped_column(primary_key=True)
circonscription_id: Mapped[str] = mapped_column(ForeignKey("circonscription.id"))
numero: Mapped[int] = mapped_column(Integer())
nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2024_nuance.code"))
bloc_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_bloc.id"))
nom: Mapped[str] = mapped_column(String(256))
prenom: Mapped[str] = mapped_column(String(256))
sexe: Mapped[str] = mapped_column(Enum(Genre))
date_naissance: Mapped[date] = mapped_column(Date())
profession: Mapped[str] = mapped_column(String(256))
sortant: Mapped[bool] = mapped_column(Boolean())
nom_suppleance: Mapped[str] = mapped_column(String(256))
prenom_suppleance: Mapped[str] = mapped_column(String(256))
sexe_suppleance: Mapped[str] = mapped_column(Enum(Genre))
date_naissance_suppleance: Mapped[date] = mapped_column(Date())
sortant_suppleance: Mapped[bool] = mapped_column(Boolean())
circonscription: Mapped[Circonscription] = relationship(
"Circonscription", back_populates="candidats_legislatives_2024")
nuance: Mapped[NuanceLegislatives2024] = relationship(NuanceLegislatives2024, back_populates="candidats")
bloc: Mapped[BlocLegislatives2024] = relationship(BlocLegislatives2024, back_populates="candidats")
resultats_par_circonscription: Mapped[List["VoixCirconscriptionLegislatives2024"]] = relationship(
"VoixCirconscriptionLegislatives2024", back_populates="candidat")
resultats_par_bureau_vote: Mapped[List["VoixBureauVoteLegislatives2024"]] = relationship(
"VoixBureauVoteLegislatives2024", back_populates="candidat")
class ResultatsFranceLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_france"
id: Mapped[int] = mapped_column(primary_key=True)
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
resultats_regions: Mapped[List["ResultatsRegionLegislatives2024"]] = relationship(
"ResultatsRegionLegislatives2024", back_populates="resultats_france")
voix: Mapped[List["VoixFranceLegislatives2024"]] = relationship(
"VoixFranceLegislatives2024", back_populates="resultats_france")
class ResultatsRegionLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_region"
id: Mapped[int] = mapped_column(primary_key=True)
region_id: Mapped[str] = mapped_column(ForeignKey("region.code_insee"))
resultats_france_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_france.id"))
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
region = relationship(Region, back_populates="resultats_legislatives_2024")
resultats_france = relationship(ResultatsFranceLegislatives2024, back_populates="resultats_regions")
resultats_departements: Mapped[List["ResultatsDepartementLegislatives2024"]] = relationship(
"ResultatsDepartementLegislatives2024", back_populates="resultats_region")
voix: Mapped[List["VoixRegionLegislatives2024"]] = relationship(
"VoixRegionLegislatives2024", back_populates="resultats_region")
class ResultatsDepartementLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_departement"
id: Mapped[int] = mapped_column(primary_key=True)
dpt_id: Mapped[str] = mapped_column(ForeignKey("departement.code_insee"))
resultats_region_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_region.id"), nullable=True)
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
departement = relationship(Departement, back_populates="resultats_legislatives_2024")
resultats_region = relationship(ResultatsRegionLegislatives2024, back_populates="resultats_departements")
resultats_communes: Mapped[List["ResultatsCommuneLegislatives2024"]] = relationship(
"ResultatsCommuneLegislatives2024", back_populates="resultats_departement")
resultats_circonscriptions: Mapped[List["ResultatsCirconscriptionLegislatives2024"]] = relationship(
"ResultatsCirconscriptionLegislatives2024", back_populates="resultats_departement")
voix: Mapped[List["VoixDepartementLegislatives2024"]] = relationship(
"VoixDepartementLegislatives2024", back_populates="resultats_departement")
class ResultatsCirconscriptionLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_circonscription"
id: Mapped[int] = mapped_column(primary_key=True)
circo_id: Mapped[str] = mapped_column(ForeignKey("circonscription.id"))
resultats_departement_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_departement.id"))
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
circonscription = relationship("Circonscription", back_populates="resultats_legislatives_2024")
resultats_departement = relationship(ResultatsDepartementLegislatives2024,
back_populates="resultats_circonscriptions")
resultats_bureaux_vote: Mapped[List["ResultatsBureauVoteLegislatives2024"]] = relationship(
"ResultatsBureauVoteLegislatives2024", back_populates="resultats_circonscription")
voix: Mapped[List["VoixCirconscriptionLegislatives2024"]] = relationship(
"VoixCirconscriptionLegislatives2024", back_populates="resultats_circonscription")
class ResultatsCommuneLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_commune"
id: Mapped[int] = mapped_column(primary_key=True)
commune_id: Mapped[str] = mapped_column(ForeignKey("commune.code_insee"))
resultats_dpt_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_departement.id"))
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
commune = relationship(Commune, back_populates="resultats_legislatives_2024")
resultats_departement = relationship(ResultatsDepartementLegislatives2024, back_populates="resultats_communes")
resultats_bureaux_vote: Mapped[List["ResultatsBureauVoteLegislatives2024"]] = relationship(
"ResultatsBureauVoteLegislatives2024", back_populates="resultats_commune")
voix: Mapped[List["VoixCommuneLegislatives2024"]] = relationship(
"VoixCommuneLegislatives2024", back_populates="resultats_commune")
class ResultatsBureauVoteLegislatives2024(Base):
__tablename__ = "legislatives_2024_resultats_bureau_vote"
id: Mapped[int] = mapped_column(primary_key=True)
bv_id: Mapped[str] = mapped_column(ForeignKey("bureau_vote.id"))
resultats_commune_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_commune.id"))
resultats_circo_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_circonscription.id"),
nullable=True)
inscrits_t1: Mapped[int] = mapped_column(Integer(), default=0)
votants_t1: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t1: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t1: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t1: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t1: Mapped[int] = mapped_column(Integer(), default=0)
inscrits_t2: Mapped[int] = mapped_column(Integer(), default=0)
votants_t2: Mapped[int] = mapped_column(Integer(), default=0)
abstentions_t2: Mapped[int] = mapped_column(Integer(), default=0)
exprimes_t2: Mapped[int] = mapped_column(Integer(), default=0)
blancs_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuls_t2: Mapped[int] = mapped_column(Integer(), default=0)
bureau_vote = relationship("BureauVote", back_populates="resultats_legislatives_2024")
resultats_commune = relationship(ResultatsCommuneLegislatives2024, back_populates="resultats_bureaux_vote")
resultats_circonscription = relationship(ResultatsCirconscriptionLegislatives2024,
back_populates="resultats_bureaux_vote")
voix: Mapped[List["VoixBureauVoteLegislatives2024"]] = relationship(
"VoixBureauVoteLegislatives2024", back_populates="resultats_bureau_vote")
class VoixFranceLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_france"
id: Mapped[int] = mapped_column(primary_key=True)
nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2024_nuance.code"))
resultats_france_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_france.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuance: Mapped[NuanceLegislatives2024] = relationship(
NuanceLegislatives2024, back_populates="resultats_nationaux")
resultats_france: Mapped[ResultatsFranceLegislatives2024] = relationship(
ResultatsFranceLegislatives2024, back_populates="voix")
class VoixRegionLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_region"
id: Mapped[int] = mapped_column(primary_key=True)
nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2024_nuance.code"))
resultats_region_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_region.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuance: Mapped[NuanceLegislatives2024] = relationship(
NuanceLegislatives2024, back_populates="resultats_par_region")
resultats_region: Mapped[ResultatsRegionLegislatives2024] = relationship(
ResultatsRegionLegislatives2024, back_populates="voix")
class VoixDepartementLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_departement"
id: Mapped[int] = mapped_column(primary_key=True)
nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2024_nuance.code"))
resultats_departement_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_departement.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuance: Mapped[NuanceLegislatives2024] = relationship(
NuanceLegislatives2024, back_populates="resultats_par_departement")
resultats_departement: Mapped[ResultatsDepartementLegislatives2024] = relationship(
ResultatsDepartementLegislatives2024, back_populates="voix")
class VoixCirconscriptionLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_circonscription"
id: Mapped[int] = mapped_column(primary_key=True)
candidat_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_candidat.id"))
resultats_circonscription_id: Mapped[int] = mapped_column(
ForeignKey("legislatives_2024_resultats_circonscription.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
candidat: Mapped[CandidatLegislatives2024] = relationship(
CandidatLegislatives2024, back_populates="resultats_par_circonscription")
resultats_circonscription: Mapped[ResultatsCirconscriptionLegislatives2024] = relationship(
ResultatsCirconscriptionLegislatives2024, back_populates="voix")
class VoixCommuneLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_commune"
id: Mapped[int] = mapped_column(primary_key=True)
nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2024_nuance.code"))
resultats_commune_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_commune.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
nuance: Mapped[NuanceLegislatives2024] = relationship(
NuanceLegislatives2024, back_populates="resultats_par_commune")
resultats_commune: Mapped[ResultatsCommuneLegislatives2024] = relationship(
ResultatsCommuneLegislatives2024, back_populates="voix")
class VoixBureauVoteLegislatives2024(Base):
__tablename__ = "legislatives_2024_voix_bureau_vote"
id: Mapped[int] = mapped_column(primary_key=True)
candidat_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_candidat.id"))
resultats_bureau_vote_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2024_resultats_bureau_vote.id"))
voix_t1: Mapped[int] = mapped_column(Integer(), default=0)
voix_t2: Mapped[int] = mapped_column(Integer(), default=0)
candidat: Mapped[CandidatLegislatives2024] = relationship(CandidatLegislatives2024,
back_populates="resultats_par_bureau_vote")
resultats_bureau_vote: Mapped[ResultatsBureauVoteLegislatives2024] = relationship(
ResultatsBureauVoteLegislatives2024, back_populates="voix")

View File

@ -5,7 +5,7 @@ from sqlalchemy import Engine, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from tqdm import tqdm from tqdm import tqdm
from nupes.models.geographie import Region, Departement, Commune, Circonscription, BureauVote from nupes.models.geographie import Circonscription, BureauVote
from nupes.models.europeennes2024 import * from nupes.models.europeennes2024 import *
DATA_DIR = Path(__file__).parent.parent.parent / 'data' DATA_DIR = Path(__file__).parent.parent.parent / 'data'
@ -76,17 +76,19 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None:
'departements': [dpt.code_insee for dpt in session.execute(select(Departement)).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()], 'circonscriptions': [circo.id for circo in session.execute(select(Circonscription)).scalars().all()],
}, },
"inscrits": resultats_france.inscrits, "tour1": {
"votants": resultats_france.votants, "inscrits": resultats_france.inscrits,
"abstentions": resultats_france.abstentions, "votants": resultats_france.votants,
"exprimes": resultats_france.exprimes, "abstentions": resultats_france.abstentions,
"blancs": resultats_france.blancs, "exprimes": resultats_france.exprimes,
"nuls": resultats_france.nuls, "blancs": resultats_france.blancs,
"nuls": resultats_france.nuls,
},
'geometry': {}, 'geometry': {},
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_france.voix_listes: for voix_liste in resultats_france.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix
@ -124,18 +126,20 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
session.add(resultats_region) session.add(resultats_region)
resultats_dict = { resultats_dict = {
"inscrits": resultats_region.inscrits,
"votants": resultats_region.votants,
"abstentions": resultats_region.abstentions,
"exprimes": resultats_region.exprimes,
"blancs": resultats_region.blancs,
"nuls": resultats_region.nuls,
'region': region_json, 'region': region_json,
'tour1': {
'inscrits': resultats_region.inscrits,
'votants': resultats_region.votants,
'abstentions': resultats_region.abstentions,
'exprimes': resultats_region.exprimes,
'blancs': resultats_region.blancs,
'nuls': resultats_region.nuls,
},
'geometry': region.geometry, 'geometry': region.geometry,
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_region.voix_listes: for voix_liste in resultats_region.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix
@ -180,18 +184,20 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No
session.add(resultats_departement) session.add(resultats_departement)
resultats_dict = { resultats_dict = {
"inscrits": resultats_departement.inscrits,
"votants": resultats_departement.votants,
"abstentions": resultats_departement.abstentions,
"exprimes": resultats_departement.exprimes,
"blancs": resultats_departement.blancs,
"nuls": resultats_departement.nuls,
'departement': departement_json, 'departement': departement_json,
'tour1': {
'inscrits': resultats_departement.inscrits,
'votants': resultats_departement.votants,
'abstentions': resultats_departement.abstentions,
'exprimes': resultats_departement.exprimes,
'blancs': resultats_departement.blancs,
'nuls': resultats_departement.nuls,
},
'geometry': departement.geometry, 'geometry': departement.geometry,
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_departement.voix_listes: for voix_liste in resultats_departement.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix
@ -236,18 +242,20 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -
session.add(resultats_circonscription) session.add(resultats_circonscription)
resultats_dict = { resultats_dict = {
"inscrits": resultats_circonscription.inscrits,
"votants": resultats_circonscription.votants,
"abstentions": resultats_circonscription.abstentions,
"exprimes": resultats_circonscription.exprimes,
"blancs": resultats_circonscription.blancs,
"nuls": resultats_circonscription.nuls,
'circonscription': circonscription_json, 'circonscription': circonscription_json,
'tour1': {
'inscrits': resultats_circonscription.inscrits,
'votants': resultats_circonscription.votants,
'abstentions': resultats_circonscription.abstentions,
'exprimes': resultats_circonscription.exprimes,
'blancs': resultats_circonscription.blancs,
'nuls': resultats_circonscription.nuls,
},
'geometry': circonscription.geometry, 'geometry': circonscription.geometry,
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_circonscription.voix_listes: for voix_liste in resultats_circonscription.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix
@ -291,18 +299,20 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
session.add(resultats_commune) session.add(resultats_commune)
resultats_dict = { resultats_dict = {
"inscrits": resultats_commune.inscrits,
"votants": resultats_commune.votants,
"abstentions": resultats_commune.abstentions,
"exprimes": resultats_commune.exprimes,
"blancs": resultats_commune.blancs,
"nuls": resultats_commune.nuls,
'commune': commune_json, 'commune': commune_json,
'tour1': {
'inscrits': resultats_commune.inscrits,
'votants': resultats_commune.votants,
'abstentions': resultats_commune.abstentions,
'exprimes': resultats_commune.exprimes,
'blancs': resultats_commune.blancs,
'nuls': resultats_commune.nuls,
},
'geometry': commune.geometry, 'geometry': commune.geometry,
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_commune.voix_listes: for voix_liste in resultats_commune.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix
@ -346,18 +356,20 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No
session.add(resultats_bureau_vote) session.add(resultats_bureau_vote)
resultats_dict = { resultats_dict = {
"inscrits": resultats_bureau_vote.inscrits,
"votants": resultats_bureau_vote.votants,
"abstentions": resultats_bureau_vote.abstentions,
"exprimes": resultats_bureau_vote.exprimes,
"blancs": resultats_bureau_vote.blancs,
"nuls": resultats_bureau_vote.nuls,
'bureau_vote': bureau_vote_json, 'bureau_vote': bureau_vote_json,
'tour1': {
'inscrits': resultats_bureau_vote.inscrits,
'votants': resultats_bureau_vote.votants,
'abstentions': resultats_bureau_vote.abstentions,
'exprimes': resultats_bureau_vote.exprimes,
'blancs': resultats_bureau_vote.blancs,
'nuls': resultats_bureau_vote.nuls,
},
'geometry': bureau_vote.geometry, 'geometry': bureau_vote.geometry,
} }
resultats_listes = {} resultats_listes = {}
resultats_dict['voix'] = resultats_listes resultats_dict['tour1']['voix'] = resultats_listes
for voix_liste in resultats_bureau_vote.voix_listes: for voix_liste in resultats_bureau_vote.voix_listes:
resultats_listes[voix_liste.liste.numero] = voix_liste.voix resultats_listes[voix_liste.liste.numero] = voix_liste.voix

View File

@ -1 +1 @@
from . import import_candidats, import_resultats from . import export_resultats, import_candidats, import_resultats

View File

@ -0,0 +1,467 @@
import json
from pathlib import Path
from sqlalchemy import Engine, select
from sqlalchemy.orm import Session
from tqdm import tqdm
from nupes.models.geographie import BureauVote
from nupes.models.legislatives2022 import *
DATA_DIR = Path(__file__).parent.parent.parent / 'data'
def exporter_nuances(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
blocs = session.execute(select(BlocLegislatives2022)).scalars().all()
blocs_json = []
for bloc in blocs:
bloc_json = {'id': bloc.id, 'nom': bloc.nom, 'couleur': bloc.couleur}
blocs_json.append(bloc_json)
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "blocs.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(blocs_json, f)
nuances = session.execute(select(NuanceLegislatives2022)).scalars().all()
nuances_json = []
for nuance in nuances:
nuance_json = {'code': nuance.code, 'nom': nuance.nom, 'couleur': nuance.couleur, "bloc": nuance.bloc_id}
nuances_json.append(nuance_json)
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "nuances.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(nuances_json, f)
def exporter_candidats(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
circonscriptions = session.execute(select(Circonscription)).scalars().all()
for circonscription in tqdm(circonscriptions, desc="Candidat⋅es", disable=not verbose):
candidats = session.execute(select(CandidatLegislatives2022)
.filter_by(circonscription_id=circonscription.id)).scalars().all()
candidats_json = []
for candidat in candidats:
candidat_json = {
'numero': candidat.numero,
'nuance': candidat.nuance_id,
'nom': candidat.nom,
'prenom': candidat.prenom,
'nom_suppleance': candidat.nom_suppleance,
'prenom_suppleance': candidat.prenom_suppleance,
}
candidats_json.append(candidat_json)
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "candidats" / f"{circonscription.id}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(candidats_json, f)
def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
resultats_france = session.execute(select(ResultatsFranceLegislatives2022)).scalar_one_or_none()
if not resultats_france:
resultats_france = ResultatsFranceLegislatives2022()
session.add(resultats_france)
resultats_dict = {
'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()],
},
'tour1': {
'inscrits': resultats_france.inscrits_t1,
'votants': resultats_france.votants_t1,
'abstentions': resultats_france.abstentions_t1,
'exprimes': resultats_france.exprimes_t1,
'blancs': resultats_france.blancs_t1,
'nuls': resultats_france.nuls_t1,
},
'tour2': {
'inscrits': resultats_france.inscrits_t2,
'votants': resultats_france.votants_t2,
'abstentions': resultats_france.abstentions_t2,
'exprimes': resultats_france.exprimes_t2,
'blancs': resultats_france.blancs_t2,
'nuls': resultats_france.nuls_t2,
},
'geometry': {},
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_nuance in resultats_france.voix:
resultats_t1[voix_nuance.nuance_id] = voix_nuance.voix_t1
resultats_t2[voix_nuance.nuance_id] = voix_nuance.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "france.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
regions = session.execute(select(Region)).scalars().all()
regions_json = []
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()]}
regions_json.append(region_json)
resultats_region = session.execute(select(ResultatsRegionLegislatives2022)
.filter_by(region_id=region.code_insee)).scalar_one_or_none()
if not resultats_region:
resultats_france = session.execute(select(ResultatsFranceLegislatives2022)).scalar_one()
resultats_region = ResultatsRegionLegislatives2022(region_id=region.code_insee,
resultats_france_id=resultats_france.id)
session.add(resultats_region)
resultats_dict = {
'region': region_json,
'tour1': {
'inscrits': resultats_region.inscrits_t1,
'votants': resultats_region.votants_t1,
'abstentions': resultats_region.abstentions_t1,
'exprimes': resultats_region.exprimes_t1,
'blancs': resultats_region.blancs_t1,
'nuls': resultats_region.nuls_t1,
},
'tour2': {
'inscrits': resultats_region.inscrits_t2,
'votants': resultats_region.votants_t2,
'abstentions': resultats_region.abstentions_t2,
'exprimes': resultats_region.exprimes_t2,
'blancs': resultats_region.blancs_t2,
'nuls': resultats_region.nuls_t2,
},
'geometry': region.geometry,
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_nuance in resultats_region.voix:
resultats_t1[voix_nuance.nuance_id] = voix_nuance.voix_t1
resultats_t2[voix_nuance.nuance_id] = voix_nuance.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "region" / f"{region.code_insee}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
regions_file = DATA_DIR / "resultats" / "legislatives" / "2022" / "region" / "regions.json"
if not regions_file.parent.is_dir():
regions_file.parent.mkdir(parents=True)
with regions_file.open('w') as f:
json.dump(regions_json, f)
def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
departements = session.execute(select(Departement)).scalars().all()
departements_json = []
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]}
departements_json.append(departement_json)
resultats_departement = session.execute(
select(ResultatsDepartementLegislatives2022)
.filter_by(dpt_id=departement.code_insee)).scalar_one_or_none()
if not resultats_departement:
resultats_region = session.execute(select(ResultatsRegionLegislatives2022)
.filter_by(region_id=departement.region_code)).scalar_one()
resultats_departement = ResultatsDepartementLegislatives2022(dpt_id=departement.code_insee,
resultats_region_id=resultats_region.id)
session.add(resultats_departement)
resultats_dict = {
'departement': departement_json,
'tour1': {
'inscrits': resultats_departement.inscrits_t1,
'votants': resultats_departement.votants_t1,
'abstentions': resultats_departement.abstentions_t1,
'exprimes': resultats_departement.exprimes_t1,
'blancs': resultats_departement.blancs_t1,
'nuls': resultats_departement.nuls_t1,
},
'tour2': {
'inscrits': resultats_departement.inscrits_t2,
'votants': resultats_departement.votants_t2,
'abstentions': resultats_departement.abstentions_t2,
'exprimes': resultats_departement.exprimes_t2,
'blancs': resultats_departement.blancs_t2,
'nuls': resultats_departement.nuls_t2,
},
'geometry': departement.geometry,
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_nuance in resultats_departement.voix:
resultats_t1[voix_nuance.nuance_id] = voix_nuance.voix_t1
resultats_t2[voix_nuance.nuance_id] = voix_nuance.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "departement" / f"{departement.code_insee}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
departements_file = DATA_DIR / "resultats" / "legislatives" / "2022" / "departement" / "departements.json"
if not departements_file.parent.is_dir():
departements_file.parent.mkdir(parents=True)
with departements_file.open('w') as f:
json.dump(departements_json, f)
def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
circonscriptions = session.execute(select(Circonscription)).scalars().all()
circonscriptions_json = []
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]}
circonscriptions_json.append(circonscription_json)
resultats_circonscription = session.execute(
select(ResultatsCirconscriptionLegislatives2022)
.filter_by(circo_id=circonscription.id)).scalar_one_or_none()
if not resultats_circonscription:
resultats_departement = session.execute(select(ResultatsDepartementLegislatives2022)
.filter_by(dpt_id=circonscription.departement_code)) \
.scalar_one()
resultats_circonscription = ResultatsCirconscriptionLegislatives2022(
circo_id=circonscription.id, resultats_departement_id=resultats_departement.id)
session.add(resultats_circonscription)
resultats_dict = {
'circonscription': circonscription_json,
'tour1': {
'inscrits': resultats_circonscription.inscrits_t1,
'votants': resultats_circonscription.votants_t1,
'abstentions': resultats_circonscription.abstentions_t1,
'exprimes': resultats_circonscription.exprimes_t1,
'blancs': resultats_circonscription.blancs_t1,
'nuls': resultats_circonscription.nuls_t1,
},
'tour2': {
'inscrits': resultats_circonscription.inscrits_t2,
'votants': resultats_circonscription.votants_t2,
'abstentions': resultats_circonscription.abstentions_t2,
'exprimes': resultats_circonscription.exprimes_t2,
'blancs': resultats_circonscription.blancs_t2,
'nuls': resultats_circonscription.nuls_t2,
},
'geometry': circonscription.geometry,
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_candidat in resultats_circonscription.voix:
resultats_t1[voix_candidat.candidat.id] = voix_candidat.voix_t1
resultats_t2[voix_candidat.candidat.id] = voix_candidat.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "circonscription" / f"{circonscription.id}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
circonscriptions_file = (DATA_DIR / "resultats" / "legislatives" / "2022"
/ "circonscription" / "circonscriptions.json")
if not circonscriptions_file.parent.is_dir():
circonscriptions_file.parent.mkdir(parents=True)
with circonscriptions_file.open('w') as f:
json.dump(circonscriptions_json, f)
def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
communes = session.execute(select(Commune)).scalars().all()
communes_json = []
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]}
communes_json.append(commune_json)
resultats_commune = session.execute(
select(ResultatsCommuneLegislatives2022).filter_by(commune_id=commune.code_insee)).scalar_one_or_none()
if not resultats_commune:
resultats_departement = session.execute(select(ResultatsDepartementLegislatives2022)
.filter_by(dpt_id=commune.departement_code)).scalar_one()
resultats_commune = ResultatsCommuneLegislatives2022(commune_id=commune.code_insee,
resultats_dpt_id=resultats_departement.id)
session.add(resultats_commune)
resultats_dict = {
'commune': commune_json,
'tour1': {
'inscrits': resultats_commune.inscrits_t1,
'votants': resultats_commune.votants_t1,
'abstentions': resultats_commune.abstentions_t1,
'exprimes': resultats_commune.exprimes_t1,
'blancs': resultats_commune.blancs_t1,
'nuls': resultats_commune.nuls_t1,
},
'tour2': {
'inscrits': resultats_commune.inscrits_t2,
'votants': resultats_commune.votants_t2,
'abstentions': resultats_commune.abstentions_t2,
'exprimes': resultats_commune.exprimes_t2,
'blancs': resultats_commune.blancs_t2,
'nuls': resultats_commune.nuls_t2,
},
'geometry': commune.geometry,
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_nuance in resultats_commune.voix:
resultats_t1[voix_nuance.nuance_id] = voix_nuance.voix_t1
resultats_t2[voix_nuance.nuance_id] = voix_nuance.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "commune" / f"{commune.code_insee}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
communes_file = DATA_DIR / "resultats" / "legislatives" / "2022" / "commune" / "communes.json"
if not communes_file.parent.is_dir():
communes_file.parent.mkdir(parents=True)
with communes_file.open('w') as f:
json.dump(communes_json, f)
def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
with Session(engine) as session:
bureaux_vote = session.execute(select(BureauVote)).scalars().all()
bureaux_vote_json = []
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}
bureaux_vote_json.append(bureau_vote_json)
resultats_bureau_vote = session.execute(
select(ResultatsBureauVoteLegislatives2022).filter_by(bv_id=bureau_vote.id)).scalar_one_or_none()
if not resultats_bureau_vote:
resultats_commune = session.execute(select(ResultatsCommuneLegislatives2022)
.filter_by(commune_id=bureau_vote.commune_code)).scalar_one()
resultats_bureau_vote = ResultatsBureauVoteLegislatives2022(bv_id=bureau_vote.id,
resultats_commune_id=resultats_commune.id)
session.add(resultats_bureau_vote)
resultats_dict = {
'bureau_vote': bureau_vote_json,
'tour1': {
'inscrits': resultats_bureau_vote.inscrits_t1,
'votants': resultats_bureau_vote.votants_t1,
'abstentions': resultats_bureau_vote.abstentions_t1,
'exprimes': resultats_bureau_vote.exprimes_t1,
'blancs': resultats_bureau_vote.blancs_t1,
'nuls': resultats_bureau_vote.nuls_t1,
},
'tour2': {
'inscrits': resultats_bureau_vote.inscrits_t2,
'votants': resultats_bureau_vote.votants_t2,
'abstentions': resultats_bureau_vote.abstentions_t2,
'exprimes': resultats_bureau_vote.exprimes_t2,
'blancs': resultats_bureau_vote.blancs_t2,
'nuls': resultats_bureau_vote.nuls_t2,
},
'geometry': bureau_vote.geometry,
}
resultats_t1, resultats_t2 = {}, {}
resultats_dict['tour1']['voix'] = resultats_t1
resultats_dict['tour2']['voix'] = resultats_t2
for voix_candidat in resultats_bureau_vote.voix:
resultats_t1[voix_candidat.candidat.id] = voix_candidat.voix_t1
resultats_t2[voix_candidat.candidat.id] = voix_candidat.voix_t2
file = DATA_DIR / "resultats" / "legislatives" / "2022" / "bureau_vote" / f"{bureau_vote.id}.json"
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
with file.open('w') as f:
json.dump(resultats_dict, f)
session.commit()
bureaux_vote_file = DATA_DIR / "resultats" / "legislatives" / "2022" / "bureau_vote" / "bureaux_vote.json"
if not bureaux_vote_file.parent.is_dir():
bureaux_vote_file.parent.mkdir(parents=True)
with bureaux_vote_file.open('w') as f:
json.dump(bureaux_vote_json, f)
def run(engine: Engine, verbose: bool = False) -> None:
exporter_nuances(engine, verbose)
exporter_candidats(engine, verbose)
exporter_resultats_france(engine, verbose)
exporter_resultats_regions(engine, verbose)
exporter_resultats_departements(engine, verbose)
exporter_resultats_circonscriptions(engine, verbose)
exporter_resultats_communes(engine, verbose)
exporter_resultats_bureaux_vote(engine, verbose)

View File

@ -297,7 +297,8 @@ def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
.scalar_one_or_none() .scalar_one_or_none()
if not voix_nuance_commune: if not voix_nuance_commune:
voix_nuance_commune = VoixCommuneLegislatives2022(resultats_commune_id=resultats_commune.id, voix_nuance_commune = VoixCommuneLegislatives2022(resultats_commune_id=resultats_commune.id,
nuance_id=nuance_code) nuance_id=nuance_code,
voix_t1=0, voix_t2=0)
session.add(voix_nuance_commune) session.add(voix_nuance_commune)
if tour == 1: if tour == 1:
@ -305,7 +306,7 @@ def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
elif tour == 2: elif tour == 2:
voix_nuance_commune.voix_t2 += voix voix_nuance_commune.voix_t2 += voix
session.commit() session.commit()
def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None: def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None:

View File

@ -0,0 +1 @@
from . import import_candidats

View File

@ -3,6 +3,7 @@ import datetime
from sqlalchemy import Engine, select from sqlalchemy import Engine, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from tqdm import tqdm
from nupes.cache import get_file from nupes.cache import get_file
from nupes.models.legislatives2024 import BlocLegislatives2024, NuanceLegislatives2024, CandidatLegislatives2024 from nupes.models.legislatives2024 import BlocLegislatives2024, NuanceLegislatives2024, CandidatLegislatives2024
@ -30,36 +31,36 @@ def creer_blocs(engine: Engine, verbose: bool = False) -> None:
def creer_nuances(engine: Engine, verbose: bool = False) -> None: def creer_nuances(engine: Engine, verbose: bool = False) -> None:
nuances = [ nuances = [
{"code": "EXG", "nom": "Extrême gauche", "couleur": "#BB0000", "bloc": 1}, {"code": "EXG", "nom": "Extrême gauche", "couleur": "#BB0000", "bloc_id": 1},
{"code": "COM", "nom": "Parti communiste français", "couleur": "#DD0000", "bloc": 1}, {"code": "COM", "nom": "Parti communiste français", "couleur": "#DD0000", "bloc_id": 1},
{"code": "FI", "nom": "La France insoumise", "couleur": "#CC2443", "bloc": 1}, {"code": "FI", "nom": "La France insoumise", "couleur": "#CC2443", "bloc_id": 1},
{"code": "SOC", "nom": "Parti socialiste", "couleur": "#FF8080", "bloc": 1}, {"code": "SOC", "nom": "Parti socialiste", "couleur": "#FF8080", "bloc_id": 1},
{"code": "UG", "nom": "Union de la gauche", "couleur": "#E4032E", "bloc": 1}, {"code": "RDG", "nom": "Parti radical de gauche", "couleur": "#FFD1DC", "bloc_id": 1},
{"code": "PRG", "nom": "Parti radical de gauche", "couleur": "#FFD1DC", "bloc": 1}, {"code": "UG", "nom": "Union de la gauche", "couleur": "#E4032E", "bloc_id": 1},
{"code": "VEC", "nom": "Les Écologistes", "couleur": "#00C000", "bloc": 1}, {"code": "VEC", "nom": "Les Écologistes", "couleur": "#00C000", "bloc_id": 1},
{"code": "DVG", "nom": "Divers gauche", "couleur": "#FFC0C0", "bloc": 1}, {"code": "DVG", "nom": "Divers gauche", "couleur": "#FFC0C0", "bloc_id": 1},
{"code": "ECO", "nom": "Écologistes", "couleur": "#77FF77", "bloc": 5}, {"code": "ECO", "nom": "Écologistes", "couleur": "#77FF77", "bloc_id": 5},
{"code": "REG", "nom": "Régionalistes", "couleur": "#DCBFA3", "bloc": 5}, {"code": "REG", "nom": "Régionalistes", "couleur": "#DCBFA3", "bloc_id": 5},
{"code": "DIV", "nom": "Divers", "couleur": "#DCDCDC", "bloc": 5}, {"code": "DIV", "nom": "Divers", "couleur": "#DCDCDC", "bloc_id": 5},
{"code": "REN", "nom": "Renaissance", "couleur": "#FFEB00", "bloc": 2}, {"code": "REN", "nom": "Renaissance", "couleur": "#FFEB00", "bloc_id": 2},
{"code": "ENS", "nom": "Ensemble ! (Majorité présidentielle)", "couleur": "#45B3AB", "bloc": 2}, {"code": "ENS", "nom": "Ensemble ! (Majorité présidentielle)", "couleur": "#45B3AB", "bloc_id": 2},
{"code": "MDM", "nom": "Modem", "couleur": "#FF9900", "bloc": 2}, {"code": "MDM", "nom": "Modem", "couleur": "#FF9900", "bloc_id": 2},
{"code": "HOR", "nom": "Horizons", "couleur": "#0001B8", "bloc": 2}, {"code": "HOR", "nom": "Horizons", "couleur": "#0001B8", "bloc_id": 2},
{"code": "DVC", "nom": "Divers centre", "couleur": "#FAC577", "bloc": 2}, {"code": "DVC", "nom": "Divers centre", "couleur": "#FAC577", "bloc_id": 2},
{"code": "UDI", "nom": "Union des Démocrates et Indépendants", "couleur": "#00FFFF", "bloc": 3}, {"code": "UDI", "nom": "Union des Démocrates et Indépendants", "couleur": "#00FFFF", "bloc_id": 3},
{"code": "LR", "nom": "Les Républicains", "couleur": "#0066CC", "bloc": 3}, {"code": "LR", "nom": "Les Républicains", "couleur": "#0066CC", "bloc_id": 3},
{"code": "DVD", "nom": "Divers droite", "couleur": "#26C4EC", "bloc": 3}, {"code": "DVD", "nom": "Divers droite", "couleur": "#26C4EC", "bloc_id": 3},
{"code": "DSV", "nom": "Droite souverainiste", "couleur": "#8040C0", "bloc": 4}, {"code": "DSV", "nom": "Droite souverainiste", "couleur": "#8040C0", "bloc_id": 4},
{"code": "RN", "nom": "Rassemblement national", "couleur": "#0D378A", "bloc": 4}, {"code": "RN", "nom": "Rassemblement national", "couleur": "#0D378A", "bloc_id": 4},
{"code": "REC", "nom": "Reconquête !", "couleur": "#404040", "bloc": 4}, {"code": "REC", "nom": "Reconquête !", "couleur": "#404040", "bloc_id": 4},
{"code": "EXD", "nom": "Extrême droite", "couleur": "#404040", "bloc": 4}, {"code": "EXD", "nom": "Extrême droite", "couleur": "#404040", "bloc_id": 4},
{"code": "UXD", "nom": "Union de l'extrême droite", "couleur": "#404040", "bloc": 4}, {"code": "UXD", "nom": "Union de l'extrême droite", "couleur": "#404040", "bloc_id": 4},
] ]
with Session(engine) as session: with Session(engine) as session:
for nuance_dict in nuances: for nuance_dict in nuances:
if nuance := session.execute(select(NuanceLegislatives2024) if nuance := session.execute(select(NuanceLegislatives2024)
.filter_by(code=nuance_dict["code"])).scalar_one_or_none(): .filter_by(code=nuance_dict["code"])).scalar_one_or_none():
nuance.nom = nuance_dict["nom"] nuance.nom = nuance_dict["nom"]
nuance.couleur = nuance_dict["couleur"] nuance.couleur = nuance_dict["couleur"]
else: else:
@ -68,6 +69,90 @@ def creer_nuances(engine: Engine, verbose: bool = False) -> None:
session.commit() session.commit()
def importer_candidats(engine: Engine, verbose: bool = False) -> None:
DATASET_URL = "https://www.data.gouv.fr/fr/datasets/r/e02a62a1-9f4b-49d6-bb32-05dc4da8c581"
file = get_file(DATASET_URL, "candidats_legislatives_2024.csv")
with file.open('r') as f:
with Session(engine) as session:
reader = csv.DictReader(f)
for line in tqdm(reader, desc="Candidat⋅es", disable=not verbose):
line: dict[str, str]
numero_panneau = int(line.get("Numéro de panneau"))
code_dpt = line.get("Code département").zfill(2)
match code_dpt:
case "ZA":
code_dpt = "971"
case "ZB":
code_dpt = "972"
case "ZC":
code_dpt = "973"
case "ZD":
code_dpt = "974"
case "ZS":
code_dpt = "975"
case "ZM":
code_dpt = "976"
case "ZX":
code_dpt = "977"
case "ZW":
code_dpt = "986"
case "ZP":
code_dpt = "987"
case "ZN":
code_dpt = "988"
code_circo = line.get("Code circonscription")[-2:]
circo_id = f"{code_dpt}-{code_circo}"
nuance_id = line.get("Code nuance")
nuance = session.execute(select(NuanceLegislatives2024).filter_by(code=nuance_id)).scalar_one()
if candidat := session.execute(select(CandidatLegislatives2024)
.filter_by(circonscription_id=circo_id, numero=numero_panneau)) \
.scalar_one_or_none():
candidat.nuance_id = nuance_id
candidat.bloc_id = nuance.bloc_id
candidat.nom = line['Nom du candidat']
candidat.prenom = line['Prénom du candidat']
candidat.sexe = CandidatLegislatives2024.Genre(line['Sexe du candidat'])
candidat.date_naissance = datetime.datetime.strptime(line['Date de naissance du candidat'],
"%d/%m/%Y").date()
candidat.profession = line['Profession']
candidat.sortant = line['Sortant'] == "OUI"
candidat.nom_suppleance = line['Nom remplaçant']
candidat.prenom_suppleance = line['Prénom remplaçant']
candidat.sexe_suppleance = CandidatLegislatives2024.Genre(line['Sexe remplaçant'])
candidat.date_naissance_suppleance = datetime.datetime.strptime(
line['Date de naissance remplaçant'], "%d/%m/%Y").date()
candidat.sortant = line['Sortant remplaçant'] == "OUI"
else:
candidat = CandidatLegislatives2024(
circonscription_id=circo_id,
numero=numero_panneau,
nuance_id=nuance_id,
bloc_id=nuance.bloc_id,
nom=line['Nom du candidat'],
prenom=line['Prénom du candidat'],
sexe=CandidatLegislatives2024.Genre(line['Sexe du candidat']).name,
date_naissance=datetime.datetime.strptime(line['Date de naissance du candidat'],
"%d/%m/%Y").date(),
profession=line['Profession'],
sortant=line['Sortant'] == "OUI",
nom_suppleance=line['Nom remplaçant'],
prenom_suppleance=line['Prénom remplaçant'],
sexe_suppleance=CandidatLegislatives2024.Genre(line['Sexe remplaçant']).name,
date_naissance_suppleance=datetime.datetime.strptime(line['Date de naissance remplaçant'],
"%d/%m/%Y").date(),
sortant_suppleance=line['Sortant remplaçant'] == "OUI",
)
session.add(candidat)
session.commit()
def run(engine: Engine, verbose: bool = False) -> None: def run(engine: Engine, verbose: bool = False) -> None:
creer_blocs(engine, verbose) creer_blocs(engine, verbose)
creer_nuances(engine, verbose) creer_nuances(engine, verbose)
importer_candidats(engine, verbose)