diff --git a/alembic/versions/42943ee95bdb_ajout_modèles_législatives.py b/alembic/versions/42943ee95bdb_ajout_modèles_législatives.py new file mode 100644 index 0000000..dbbb790 --- /dev/null +++ b/alembic/versions/42943ee95bdb_ajout_modèles_législatives.py @@ -0,0 +1,259 @@ +"""ajout modèles législatives + +Revision ID: 42943ee95bdb +Revises: c0443e979a90 +Create Date: 2024-06-19 22:03:16.934581 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '42943ee95bdb' +down_revision: Union[str, None] = 'c0443e979a90' +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_2022_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_2022_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_2022_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_2022_bloc.id'], ), + sa.PrimaryKeyConstraint('code'), + sa.UniqueConstraint('nom') + ) + op.create_table('legislatives_2022_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_2022_resultats_france.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_resultats_region.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_nuance.code'], ), + sa.ForeignKeyConstraint(['resultats_france_id'], ['legislatives_2022_resultats_france.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_france_id') + ) + op.create_table('legislatives_2022_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_2022_nuance.code'], ), + sa.ForeignKeyConstraint(['resultats_region_id'], ['legislatives_2022_resultats_region.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_region_id') + ) + op.create_table('legislatives_2022_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=True), + 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_2022_bloc.id'], ), + sa.ForeignKeyConstraint(['circonscription_id'], ['circonscription.id'], ), + sa.ForeignKeyConstraint(['nuance_id'], ['legislatives_2022_nuance.code'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_resultats_departement.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_resultats_departement.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_nuance.code'], ), + sa.ForeignKeyConstraint(['resultats_departement_id'], ['legislatives_2022_resultats_departement.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_departement_id') + ) + op.create_table('legislatives_2022_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_2022_resultats_circonscription.id'], ), + sa.ForeignKeyConstraint(['resultats_commune_id'], ['legislatives_2022_resultats_commune.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('legislatives_2022_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_2022_candidat.id'], ), + sa.ForeignKeyConstraint(['resultats_circonscription_id'], ['legislatives_2022_resultats_circonscription.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_circonscription_id') + ) + op.create_table('legislatives_2022_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_2022_nuance.code'], ), + sa.ForeignKeyConstraint(['resultats_commune_id'], ['legislatives_2022_resultats_commune.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_commune_id') + ) + op.create_table('legislatives_2022_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_2022_candidat.id'], ), + sa.ForeignKeyConstraint(['resultats_bureau_vote_id'], ['legislatives_2022_resultats_bureau_vote.id'], ), + sa.PrimaryKeyConstraint('id', 'resultats_bureau_vote_id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('legislatives_2022_voix_bureau_vote') + op.drop_table('legislatives_2022_voix_commune') + op.drop_table('legislatives_2022_voix_circonscription') + op.drop_table('legislatives_2022_resultats_bureau_vote') + op.drop_table('legislatives_2022_voix_departement') + op.drop_table('legislatives_2022_resultats_commune') + op.drop_table('legislatives_2022_resultats_circonscription') + op.drop_table('legislatives_2022_candidat') + op.drop_table('legislatives_2022_voix_region') + op.drop_table('legislatives_2022_voix_france') + op.drop_table('legislatives_2022_resultats_departement') + op.drop_table('legislatives_2022_resultats_region') + op.drop_table('legislatives_2022_nuance') + op.drop_table('legislatives_2022_resultats_france') + op.drop_table('legislatives_2022_bloc') + # ### end Alembic commands ### diff --git a/main.py b/main.py index c7447db..c437956 100755 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ from dotenv import load_dotenv from sqlalchemy import create_engine from nupes.scripts import import_geographie -from nupes.scripts import europeennes2024 +from nupes.scripts import legislatives2022, europeennes2024, legislatives2024 def parse_args(): @@ -15,7 +15,8 @@ def parse_args(): parser.add_argument('--type', '-t', type=str, help="Type d'élection", choices=["présidentielle", "législatives", "sénatoriales", "européennes", "régionales", "départementales", "municipales"], default="européennes") - parser.add_argument('--year', '-y', type=int, help="Année de l'élection", default=2024) + parser.add_argument('--year', '-y', type=int, help="Année de l'élection", + choices=[2022, 2024], default=2024) parser.add_argument('action', help="Action à réaliser", choices=["import_candidats", "import_geographie", "import_resultats", "import_resultats_web", "export_resultats"]) @@ -33,8 +34,12 @@ def main(): verbose = args.verbose match args.type, args.year: + case "législatives", 2022: + election_module = legislatives2022 case "européennes", 2024: election_module = europeennes2024 + case "législatives", 2024: + election_module = legislatives2024 case _: print(f"Type d'élection {args.type} {args.year} non reconnu") return diff --git a/nupes/models/__init__.py b/nupes/models/__init__.py index 1ba7052..eb2a0ce 100644 --- a/nupes/models/__init__.py +++ b/nupes/models/__init__.py @@ -1,3 +1,4 @@ from .base import Base from .geographie import * +from .legislatives2022 import * from .europeennes2024 import * diff --git a/nupes/models/geographie.py b/nupes/models/geographie.py index 188e3e2..606b84c 100644 --- a/nupes/models/geographie.py +++ b/nupes/models/geographie.py @@ -15,6 +15,7 @@ class Region(Base): departements: Mapped[List["Departement"]] = relationship("Departement", back_populates="region") + resultats_legislatives_2022 = relationship("ResultatsRegionLegislatives2022", back_populates="region") resultats_europeennes_2024 = relationship("ResultatsRegionEuropeennes2024", back_populates="region") @@ -30,6 +31,7 @@ class Departement(Base): circonscriptions: Mapped[List["Circonscription"]] = relationship("Circonscription", back_populates="departement") communes: Mapped[List["Commune"]] = relationship("Commune", back_populates="departement") + resultats_legislatives_2022 = relationship("ResultatsDepartementLegislatives2022", back_populates="departement") resultats_europeennes_2024 = relationship("ResultatsDepartementEuropeennes2024", back_populates="departement") @@ -44,6 +46,7 @@ class Commune(Base): departement: Mapped[Departement] = relationship(Departement, back_populates="communes") bureaux_vote: Mapped[List["BureauVote"]] = relationship("BureauVote", back_populates="commune") + resultats_legislatives_2022 = relationship("ResultatsCommuneLegislatives2022", back_populates="commune") resultats_europeennes_2024 = relationship("ResultatsCommuneEuropeennes2024", back_populates="commune") @@ -58,6 +61,9 @@ class Circonscription(Base): departement: Mapped[Departement] = relationship(Departement, back_populates="circonscriptions") bureaux_vote: Mapped[List["BureauVote"]] = relationship("BureauVote", back_populates="circonscription") + candidats_legislatives_2022 = relationship("CandidatLegislatives2022", back_populates="circonscription") + resultats_legislatives_2022 = relationship("ResultatsCirconscriptionLegislatives2022", + back_populates="circonscription") resultats_europeennes_2024 = relationship("ResultatsCirconscriptionEuropeennes2024", back_populates="circonscription") @@ -76,4 +82,5 @@ class BureauVote(Base): commune: Mapped[Commune] = relationship(Commune, back_populates="bureaux_vote") circonscription: Mapped[Circonscription] = relationship(Circonscription, back_populates="bureaux_vote") + resultats_legislatives_2022 = relationship("ResultatsBureauVoteLegislatives2022", back_populates="bureau_vote") resultats_europeennes_2024 = relationship("ResultatsBureauVoteEuropeennes2024", back_populates="bureau_vote") diff --git a/nupes/models/legislatives2022.py b/nupes/models/legislatives2022.py new file mode 100644 index 0000000..d108a42 --- /dev/null +++ b/nupes/models/legislatives2022.py @@ -0,0 +1,338 @@ +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 BlocLegislatives2022(Base): + __tablename__ = "legislatives_2022_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["CandidatLegislatives2022"]] = relationship("CandidatLegislatives2022", + back_populates="bloc") + + nuances: Mapped[List["NuanceLegislatives2022"]] = relationship( + "NuanceLegislatives2022", back_populates="bloc") + + +class NuanceLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_bloc.id")) + + bloc: Mapped[BlocLegislatives2022] = relationship(BlocLegislatives2022, back_populates="nuances") + + candidats: Mapped[List["CandidatLegislatives2022"]] = relationship( + "CandidatLegislatives2022", back_populates="nuance") + + resultats_nationaux: Mapped[List["VoixFranceLegislatives2022"]] = relationship( + "VoixFranceLegislatives2022", back_populates="nuance") + resultats_par_region: Mapped[List["VoixRegionLegislatives2022"]] = relationship( + "VoixRegionLegislatives2022", back_populates="nuance") + resultats_par_departement: Mapped[List["VoixDepartementLegislatives2022"]] = relationship( + "VoixDepartementLegislatives2022", back_populates="nuance") + resultats_par_commune: Mapped[List["VoixCommuneLegislatives2022"]] = relationship( + "VoixCommuneLegislatives2022", back_populates="nuance") + + +class CandidatLegislatives2022(Base): + class Genre(enum.Enum): + MASCULIN = "M" + FEMININ = "F" + + __tablename__ = "legislatives_2022_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_column(ForeignKey("legislatives_2022_nuance.code")) + bloc_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_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_2022") + nuance: Mapped[NuanceLegislatives2022] = relationship(NuanceLegislatives2022, back_populates="candidats") + bloc: Mapped[BlocLegislatives2022] = relationship(BlocLegislatives2022, back_populates="candidats") + resultats_par_circonscription: Mapped[List["VoixCirconscriptionLegislatives2022"]] = relationship( + "VoixCirconscriptionLegislatives2022", back_populates="candidat") + resultats_par_bureau_vote: Mapped[List["VoixBureauVoteLegislatives2022"]] = relationship( + "VoixBureauVoteLegislatives2022", back_populates="candidat") + + +class ResultatsFranceLegislatives2022(Base): + __tablename__ = "legislatives_2022_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["ResultatsRegionLegislatives2022"]] = relationship( + "ResultatsRegionLegislatives2022", back_populates="resultats_france") + voix: Mapped[List["VoixFranceLegislatives2022"]] = relationship( + "VoixFranceLegislatives2022", back_populates="resultats_france") + + +class ResultatsRegionLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_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_2022") + resultats_france = relationship(ResultatsFranceLegislatives2022, back_populates="resultats_regions") + resultats_departements: Mapped[List["ResultatsDepartementLegislatives2022"]] = relationship( + "ResultatsDepartementLegislatives2022", back_populates="resultats_region") + voix: Mapped[List["VoixRegionLegislatives2022"]] = relationship( + "VoixRegionLegislatives2022", back_populates="resultats_region") + + +class ResultatsDepartementLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_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_2022") + resultats_region = relationship(ResultatsRegionLegislatives2022, back_populates="resultats_departements") + resultats_communes: Mapped[List["ResultatsCommuneLegislatives2022"]] = relationship( + "ResultatsCommuneLegislatives2022", back_populates="resultats_departement") + resultats_circonscriptions: Mapped[List["ResultatsCirconscriptionLegislatives2022"]] = relationship( + "ResultatsCirconscriptionLegislatives2022", back_populates="resultats_departement") + voix: Mapped[List["VoixDepartementLegislatives2022"]] = relationship( + "VoixDepartementLegislatives2022", back_populates="resultats_departement") + + +class ResultatsCirconscriptionLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_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_2022") + resultats_departement = relationship(ResultatsDepartementLegislatives2022, + back_populates="resultats_circonscriptions") + resultats_bureaux_vote: Mapped[List["ResultatsBureauVoteLegislatives2022"]] = relationship( + "ResultatsBureauVoteLegislatives2022", back_populates="resultats_circonscription") + voix: Mapped[List["VoixCirconscriptionLegislatives2022"]] = relationship( + "VoixCirconscriptionLegislatives2022", back_populates="resultats_circonscription") + + +class ResultatsCommuneLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_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_2022") + resultats_departement = relationship(ResultatsDepartementLegislatives2022, back_populates="resultats_communes") + resultats_bureaux_vote: Mapped[List["ResultatsBureauVoteLegislatives2022"]] = relationship( + "ResultatsBureauVoteLegislatives2022", back_populates="resultats_commune") + voix: Mapped[List["VoixCommuneLegislatives2022"]] = relationship( + "VoixCommuneLegislatives2022", back_populates="resultats_commune") + + +class ResultatsBureauVoteLegislatives2022(Base): + __tablename__ = "legislatives_2022_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_2022_resultats_commune.id")) + resultats_circo_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_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_2022") + resultats_commune = relationship(ResultatsCommuneLegislatives2022, back_populates="resultats_bureaux_vote") + resultats_circonscription = relationship(ResultatsCirconscriptionLegislatives2022, + back_populates="resultats_bureaux_vote") + voix: Mapped[List["VoixBureauVoteLegislatives2022"]] = relationship( + "VoixBureauVoteLegislatives2022", back_populates="resultats_bureau_vote") + + +class VoixFranceLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_france" + + id: Mapped[int] = mapped_column(primary_key=True) + nuance_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_nuance.code")) + resultats_france_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_resultats_france.id"), + primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + nuance: Mapped[NuanceLegislatives2022] = relationship( + NuanceLegislatives2022, back_populates="resultats_nationaux") + resultats_france: Mapped[ResultatsFranceLegislatives2022] = relationship( + ResultatsFranceLegislatives2022, back_populates="voix") + + +class VoixRegionLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_region" + + id: Mapped[int] = mapped_column(primary_key=True) + nuance_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_nuance.code")) + resultats_region_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_resultats_region.id"), + primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + nuance: Mapped[NuanceLegislatives2022] = relationship( + NuanceLegislatives2022, back_populates="resultats_par_region") + resultats_region: Mapped[ResultatsRegionLegislatives2022] = relationship( + ResultatsRegionLegislatives2022, back_populates="voix") + + +class VoixDepartementLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_departement" + + id: Mapped[int] = mapped_column(primary_key=True) + nuance_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_nuance.code")) + resultats_departement_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_resultats_departement.id"), + primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + nuance: Mapped[NuanceLegislatives2022] = relationship( + NuanceLegislatives2022, back_populates="resultats_par_departement") + resultats_departement: Mapped[ResultatsDepartementLegislatives2022] = relationship( + ResultatsDepartementLegislatives2022, back_populates="voix") + + +class VoixCirconscriptionLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_circonscription" + + id: Mapped[int] = mapped_column(primary_key=True) + candidat_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_candidat.id")) + resultats_circonscription_id: Mapped[int] = mapped_column( + ForeignKey("legislatives_2022_resultats_circonscription.id"), primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + candidat: Mapped[CandidatLegislatives2022] = relationship( + CandidatLegislatives2022, back_populates="resultats_par_circonscription") + resultats_circonscription: Mapped[ResultatsCirconscriptionLegislatives2022] = relationship( + ResultatsCirconscriptionLegislatives2022, back_populates="voix") + + +class VoixCommuneLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_commune" + + id: Mapped[int] = mapped_column(primary_key=True) + nuance_id: Mapped[str] = mapped_column(ForeignKey("legislatives_2022_nuance.code")) + resultats_commune_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_resultats_commune.id"), + primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + nuance: Mapped[NuanceLegislatives2022] = relationship( + NuanceLegislatives2022, back_populates="resultats_par_commune") + resultats_commune: Mapped[ResultatsCommuneLegislatives2022] = relationship( + ResultatsCommuneLegislatives2022, back_populates="voix") + + +class VoixBureauVoteLegislatives2022(Base): + __tablename__ = "legislatives_2022_voix_bureau_vote" + + id: Mapped[int] = mapped_column(primary_key=True) + candidat_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_candidat.id")) + resultats_bureau_vote_id: Mapped[int] = mapped_column(ForeignKey("legislatives_2022_resultats_bureau_vote.id"), + primary_key=True) + voix_t1: Mapped[int] = mapped_column(Integer(), default=0) + voix_t2: Mapped[int] = mapped_column(Integer(), default=0) + + candidat: Mapped[CandidatLegislatives2022] = relationship(CandidatLegislatives2022, + back_populates="resultats_par_bureau_vote") + resultats_bureau_vote: Mapped[ResultatsBureauVoteLegislatives2022] = relationship( + ResultatsBureauVoteLegislatives2022, back_populates="voix") diff --git a/nupes/scripts/legislatives2022/__init__.py b/nupes/scripts/legislatives2022/__init__.py new file mode 100644 index 0000000..9bc38a4 --- /dev/null +++ b/nupes/scripts/legislatives2022/__init__.py @@ -0,0 +1 @@ +from . import import_candidats diff --git a/nupes/scripts/legislatives2022/import_candidats.py b/nupes/scripts/legislatives2022/import_candidats.py new file mode 100644 index 0000000..9b2683f --- /dev/null +++ b/nupes/scripts/legislatives2022/import_candidats.py @@ -0,0 +1,150 @@ +import csv +import datetime + +from sqlalchemy import Engine, select +from sqlalchemy.orm import Session +from tqdm import tqdm + +from nupes.cache import get_file +from nupes.models.legislatives2022 import BlocLegislatives2022, NuanceLegislatives2022, CandidatLegislatives2022 + + +def creer_blocs(engine: Engine, verbose: bool = False) -> None: + blocs = [ + {"id": 1, "nom": "Gauche", "couleur": "#BB1840"}, + {"id": 2, "nom": "Droite libérale", "couleur": "#FFEB00"}, + {"id": 3, "nom": "Droite conservatrice", "couleur": "#0066CC"}, + {"id": 4, "nom": "Extrême droite", "couleur": "#404040"}, + {"id": 5, "nom": "Autres", "couleur": "#DCBFA3"} + ] + + with Session(engine) as session: + for bloc_dict in blocs: + if bloc := session.execute(select(BlocLegislatives2022).filter_by(id=bloc_dict["id"])).scalar_one_or_none(): + bloc.nom = bloc_dict["nom"] + bloc.couleur = bloc_dict["couleur"] + else: + session.add(BlocLegislatives2022(**bloc_dict)) + + session.commit() + + +def creer_nuances(engine: Engine, verbose: bool = False) -> None: + nuances = [ + {"code": "DXG", "nom": "Divers extrême gauche", "couleur": "#BB0000", "bloc_id": 1}, + {"code": "RDG", "nom": "Radical de gauche", "couleur": "#FFD1DC", "bloc_id": 1}, + {"code": "NUP", "nom": "Nouvelle union populaire écologique et sociale", "couleur": "#E4032E", "bloc_id": 1}, + {"code": "DVG", "nom": "Divers gauche", "couleur": "#FFC0C0", "bloc_id": 1}, + {"code": "ECO", "nom": "Écologistes", "couleur": "#77FF77", "bloc_id": 5}, + {"code": "DIV", "nom": "Divers", "couleur": "#DCDCDC", "bloc_id": 5}, + {"code": "REG", "nom": "Régionalistes", "couleur": "#DCBFA3", "bloc_id": 5}, + {"code": "ENS", "nom": "Ensemble ! (Majorité présidentielle)", "couleur": "#FFEB00", "bloc_id": 2}, + {"code": "DVC", "nom": "Divers centre", "couleur": "#FAC577", "bloc_id": 2}, + {"code": "UDI", "nom": "Union des Démocrates et Indépendants", "couleur": "#00FFFF", "bloc_id": 3}, + {"code": "LR", "nom": "Les Républicains", "couleur": "#0066CC", "bloc_id": 3}, + {"code": "DVD", "nom": "Divers droite", "couleur": "#26C4EC", "bloc_id": 3}, + {"code": "DSV", "nom": "Droite souverainiste", "couleur": "#8040C0", "bloc_id": 4}, + {"code": "REC", "nom": "Reconquête !", "couleur": "#404040", "bloc_id": 4}, + {"code": "RN", "nom": "Rassemblement national", "couleur": "#0D378A", "bloc_id": 4}, + {"code": "DXD", "nom": "Divers extrême droite", "couleur": "#404040", "bloc_id": 4}, + ] + + with Session(engine) as session: + for nuance_dict in nuances: + if nuance := session.execute(select(NuanceLegislatives2022) + .filter_by(code=nuance_dict["code"])).scalar_one_or_none(): + nuance.nom = nuance_dict["nom"] + nuance.couleur = nuance_dict["couleur"] + nuance.bloc_id = nuance_dict["bloc_id"] + else: + session.add(NuanceLegislatives2022(**nuance_dict)) + + session.commit() + + +def importer_candidats(engine: Engine, verbose: bool = False) -> None: + DATASET_URL = "https://www.data.gouv.fr/fr/datasets/r/c79af2f3-1733-4df3-be37-caebacbc1321" + file = get_file(DATASET_URL, "candidats_legislatives_2022.tsv") + + with file.open('r', encoding="ISO-8859-1") as f: + with Session(engine) as session: + reader = csv.DictReader(f, delimiter='\t') + for line in tqdm(reader, desc="Candidat⋅es", disable=not verbose): + line: dict[str, str] + numero_panneau = int(line.get("N° panneau")) + code_dpt = line.get("Code du département") + + 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") + circo_id = f"{code_dpt}-{code_circo}" + + nuance_id = line.get("Nuance candidat") + nuance = session.execute(select(NuanceLegislatives2022).filter_by(code=nuance_id)).scalar_one() + + if candidat := session.execute(select(CandidatLegislatives2022) + .filter_by(circonscription_id=circo_id, numero=numero_panneau)) \ + .scalar_one_or_none(): + candidat.nuance_id = line['Nuance candidat'] + candidat.bloc_id = nuance.bloc_id + candidat.nom = line['Nom candidat'] + candidat.prenom = line['Prénom candidat'] + candidat.sexe = CandidatLegislatives2022.Genre(line['Sexe candidat']) + candidat.date_naissance = datetime.datetime.strptime(line['Date naissance candidat'], + "%d/%m/%Y").date() + candidat.profession = line['Profession candidat'] + candidat.sortant = line['Le candidat est sortant'] == "Oui" + candidat.nom_suppleance = line['Nom remplaçant'] + candidat.prenom_suppleance = line['Prénom remplaçant'] + candidat.sexe_suppleance = CandidatLegislatives2022.Genre(line['Sexe remplaçant']) + candidat.date_naissance_suppleance = datetime.datetime.strptime(line['Date naiss. remplaçant'], + "%d/%m/%Y").date() + candidat.sortant = line['Le remplaçant est sortant'] == "Oui" + else: + candidat = CandidatLegislatives2022( + circonscription_id=circo_id, + numero=numero_panneau, + nuance_id=nuance_id, + bloc_id=nuance.bloc_id, + nom=line['Nom candidat'], + prenom=line['Prénom candidat'], + sexe=CandidatLegislatives2022.Genre(line['Sexe candidat']).name, + date_naissance=datetime.datetime.strptime(line['Date naissance candidat'], "%d/%m/%Y").date(), + profession=line['Profession candidat'], + sortant=line['Le candidat est sortant'] == "Oui", + nom_suppleance=line['Nom remplaçant'], + prenom_suppleance=line['Prénom remplaçant'], + sexe_suppleance=CandidatLegislatives2022.Genre(line['Sexe remplaçant']).name, + date_naissance_suppleance=datetime.datetime.strptime(line['Date naiss. remplaçant'], + "%d/%m/%Y").date(), + sortant_suppleance=line['Le remplaçant est sortant'] == "Oui", + ) + session.add(candidat) + + session.commit() + + +def run(engine: Engine, verbose: bool = False) -> None: + creer_blocs(engine, verbose) + creer_nuances(engine, verbose) + importer_candidats(engine, verbose) diff --git a/nupes/scripts/legislatives2024/__init__.py b/nupes/scripts/legislatives2024/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nupes/scripts/legislatives2024/import_candidats.py b/nupes/scripts/legislatives2024/import_candidats.py new file mode 100644 index 0000000..31412ad --- /dev/null +++ b/nupes/scripts/legislatives2024/import_candidats.py @@ -0,0 +1,73 @@ +import csv +import datetime + +from sqlalchemy import Engine, select +from sqlalchemy.orm import Session + +from nupes.cache import get_file +from nupes.models.legislatives2024 import BlocLegislatives2024, NuanceLegislatives2024, CandidatLegislatives2024 + + +def creer_blocs(engine: Engine, verbose: bool = False) -> None: + blocs = [ + {"id": 1, "nom": "Gauche", "couleur": "#BB1840"}, + {"id": 2, "nom": "Droite libérale", "couleur": "#FFEB00"}, + {"id": 3, "nom": "Droite conservatrice", "couleur": "#0066CC"}, + {"id": 4, "nom": "Extrême droite", "couleur": "#404040"}, + {"id": 5, "nom": "Autres", "couleur": "#DCBFA3"} + ] + + with Session(engine) as session: + for bloc_dict in blocs: + if bloc := session.execute(select(BlocLegislatives2024).filter_by(id=bloc_dict["id"])).scalar_one_or_none(): + bloc.nom = bloc_dict["nom"] + bloc.couleur = bloc_dict["couleur"] + else: + session.add(BlocLegislatives2024(**bloc_dict)) + + session.commit() + + +def creer_nuances(engine: Engine, verbose: bool = False) -> None: + nuances = [ + {"code": "EXG", "nom": "Extrême gauche", "couleur": "#BB0000", "bloc": 1}, + {"code": "COM", "nom": "Parti communiste français", "couleur": "#DD0000", "bloc": 1}, + {"code": "FI", "nom": "La France insoumise", "couleur": "#CC2443", "bloc": 1}, + {"code": "SOC", "nom": "Parti socialiste", "couleur": "#FF8080", "bloc": 1}, + {"code": "UG", "nom": "Union de la gauche", "couleur": "#E4032E", "bloc": 1}, + {"code": "PRG", "nom": "Parti radical de gauche", "couleur": "#FFD1DC", "bloc": 1}, + {"code": "VEC", "nom": "Les Écologistes", "couleur": "#00C000", "bloc": 1}, + {"code": "DVG", "nom": "Divers gauche", "couleur": "#FFC0C0", "bloc": 1}, + {"code": "ECO", "nom": "Écologistes", "couleur": "#77FF77", "bloc": 5}, + {"code": "REG", "nom": "Régionalistes", "couleur": "#DCBFA3", "bloc": 5}, + {"code": "DIV", "nom": "Divers", "couleur": "#DCDCDC", "bloc": 5}, + {"code": "REN", "nom": "Renaissance", "couleur": "#FFEB00", "bloc": 2}, + {"code": "ENS", "nom": "Ensemble ! (Majorité présidentielle)", "couleur": "#45B3AB", "bloc": 2}, + {"code": "MDM", "nom": "Modem", "couleur": "#FF9900", "bloc": 2}, + {"code": "HOR", "nom": "Horizons", "couleur": "#0001B8", "bloc": 2}, + {"code": "DVC", "nom": "Divers centre", "couleur": "#FAC577", "bloc": 2}, + {"code": "UDI", "nom": "Union des Démocrates et Indépendants", "couleur": "#00FFFF", "bloc": 3}, + {"code": "LR", "nom": "Les Républicains", "couleur": "#0066CC", "bloc": 3}, + {"code": "DVD", "nom": "Divers droite", "couleur": "#26C4EC", "bloc": 3}, + {"code": "DSV", "nom": "Droite souverainiste", "couleur": "#8040C0", "bloc": 4}, + {"code": "RN", "nom": "Rassemblement national", "couleur": "#0D378A", "bloc": 4}, + {"code": "REC", "nom": "Reconquête !", "couleur": "#404040", "bloc": 4}, + {"code": "EXD", "nom": "Extrême droite", "couleur": "#404040", "bloc": 4}, + {"code": "UXD", "nom": "Union de l'extrême droite", "couleur": "#404040", "bloc": 4}, + ] + + with Session(engine) as session: + for nuance_dict in nuances: + if nuance := session.execute(select(NuanceLegislatives2024) + .filter_by(code=nuance_dict["code"])).scalar_one_or_none(): + nuance.nom = nuance_dict["nom"] + nuance.couleur = nuance_dict["couleur"] + else: + session.add(NuanceLegislatives2024(**nuance_dict)) + + session.commit() + + +def run(engine: Engine, verbose: bool = False) -> None: + creer_blocs(engine, verbose) + creer_nuances(engine, verbose)