From ed4ede0d7f651ef281aea8a4f8ca608caaeb931a Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 8 Jun 2024 13:08:22 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20analyse=20r=C3=A9sultats,=20import?= =?UTF-8?q?ation=20des=20candidatures=202024?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + .gitignore | 10 ++ .idea/.gitignore | 8 + .idea/inspectionProfiles/Project_Default.xml | 50 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/nupes-elections.iml | 10 ++ .idea/vcs.xml | 6 + alembic.ini | 116 ++++++++++++ alembic/README | 1 + alembic/env.py | 81 +++++++++ alembic/script.py.mako | 26 +++ ...6_ajout_des_modèles_de_candidat_es_2024.py | 72 ++++++++ main.py | 39 ++++ nupes/__init__.py | 0 nupes/data.py | 25 +++ nupes/models/__init__.py | 2 + nupes/models/base.py | 5 + nupes/models/europeennes2024.py | 70 ++++++++ nupes/scripts/__init__.py | 0 nupes/scripts/import_candidats_2024.py | 169 ++++++++++++++++++ requirements.txt | 4 + 23 files changed, 716 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/nupes-elections.iml create mode 100644 .idea/vcs.xml create mode 100644 alembic.ini create mode 100644 alembic/README create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/9d99f3ea6b66_ajout_des_modèles_de_candidat_es_2024.py create mode 100755 main.py create mode 100644 nupes/__init__.py create mode 100644 nupes/data.py create mode 100644 nupes/models/__init__.py create mode 100644 nupes/models/base.py create mode 100644 nupes/models/europeennes2024.py create mode 100644 nupes/scripts/__init__.py create mode 100644 nupes/scripts/import_candidats_2024.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7f47a14 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +SQLALCHEMY_URL=postgresql://nupes:nupes@localhost:5432/nupes diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27dfa4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +__pycache__ + +.venv + +.env +*.sqlite3 + +data/ + +migrations/versions/* diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8ef7560 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,50 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ac88ce8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..44a669d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/nupes-elections.iml b/.idea/nupes-elections.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/nupes-elections.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..6e3c8c0 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,116 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = sqlite:///db.sqlite3 + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..2f23215 --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,81 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +from nupes.models import Base + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +# target_metadata = None +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/9d99f3ea6b66_ajout_des_modèles_de_candidat_es_2024.py b/alembic/versions/9d99f3ea6b66_ajout_des_modèles_de_candidat_es_2024.py new file mode 100644 index 0000000..cc7b93f --- /dev/null +++ b/alembic/versions/9d99f3ea6b66_ajout_des_modèles_de_candidat_es_2024.py @@ -0,0 +1,72 @@ +"""Ajout des modèles de candidat⋅es 2024 + +Revision ID: 9d99f3ea6b66 +Revises: +Create Date: 2024-06-08 13:01:08.629780 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '9d99f3ea6b66' +down_revision: Union[str, None] = None +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('bloc2024', + 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('nuance2024', + sa.Column('code', sa.String(length=8), nullable=False), + sa.Column('nom', sa.String(length=32), nullable=False), + sa.Column('couleur', sa.String(length=7), nullable=False), + sa.PrimaryKeyConstraint('code'), + sa.UniqueConstraint('nom') + ) + op.create_table('liste2024', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('nom', sa.String(length=256), 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.ForeignKeyConstraint(['bloc_id'], ['bloc2024.id'], ), + sa.ForeignKeyConstraint(['nuance_id'], ['nuance2024.code'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('nom'), + sa.UniqueConstraint('numero') + ) + op.create_table('candidat2024', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('liste_id', sa.Integer(), nullable=False), + sa.Column('ordre', 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('code_personnalite', sa.Enum('DEFAUT', 'EURODEPUTE', 'DEPUTE', 'SENATEUR', 'MINISTRE', 'PRESIDENT_CONSEIL_REGIONAL', 'PRESIDENT_CONSEIL_DEPARTEMENTAL', 'MAIRE', name='personnalite'), nullable=False), + sa.Column('sortant', sa.Boolean(), nullable=False), + sa.ForeignKeyConstraint(['liste_id'], ['liste2024.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('candidat2024') + op.drop_table('liste2024') + op.drop_table('nuance2024') + op.drop_table('bloc2024') + # ### end Alembic commands ### diff --git a/main.py b/main.py new file mode 100755 index 0000000..fc76a28 --- /dev/null +++ b/main.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import argparse +import os + +from dotenv import load_dotenv +from sqlalchemy import create_engine + +from nupes.scripts import import_candidats_2024 + + +def parse_args(): + parser = argparse.ArgumentParser(description="Gestion d'élections") + 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('action', help="Action à réaliser", choices=["import_candidats"]) + parser.add_argument('--debug', '-d', action='store_true', help="Mode debug") + return parser.parse_args() + + +def main(): + load_dotenv() + + args = parse_args() + + engine = create_engine(os.getenv("SQLALCHEMY_URL"), echo=args.debug) + + match args.action: + case "import_candidats": + print(f"Import des candidats pour les élections {args.type} {args.year}") + import_candidats_2024.run(engine) + case _: + print(f"Action {args.action} non reconnue") + + +if __name__ == "__main__": + main() diff --git a/nupes/__init__.py b/nupes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nupes/data.py b/nupes/data.py new file mode 100644 index 0000000..335fd76 --- /dev/null +++ b/nupes/data.py @@ -0,0 +1,25 @@ +from pathlib import Path + +import requests + + +DATA_DIR = Path(__file__).parent / 'data' + + +def get_file(url, filename): + if not DATA_DIR.is_dir(): + DATA_DIR.mkdir() + + head_response = requests.head(url, allow_redirects=True) + headers = head_response.headers + etag = headers.get('ETag').split('/')[-1].replace('"', '') + + file = DATA_DIR / f"{etag}_{filename}" + if file.exists(): + return file + + response = requests.get(url, allow_redirects=True) + with file.open('wb') as f: + f.write(response.content) + + return file diff --git a/nupes/models/__init__.py b/nupes/models/__init__.py new file mode 100644 index 0000000..246ed55 --- /dev/null +++ b/nupes/models/__init__.py @@ -0,0 +1,2 @@ +from .base import Base +from .europeennes2024 import Bloc as Bloc2024, Nuance as Nuance2024, Liste as Liste2024, Candidat as Candidat2024 diff --git a/nupes/models/base.py b/nupes/models/base.py new file mode 100644 index 0000000..fa2b68a --- /dev/null +++ b/nupes/models/base.py @@ -0,0 +1,5 @@ +from sqlalchemy.orm import DeclarativeBase + + +class Base(DeclarativeBase): + pass diff --git a/nupes/models/europeennes2024.py b/nupes/models/europeennes2024.py new file mode 100644 index 0000000..7e0c706 --- /dev/null +++ b/nupes/models/europeennes2024.py @@ -0,0 +1,70 @@ +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 + + +class Bloc(Base): + __tablename__ = "bloc2024" + + id: Mapped[int] = mapped_column(primary_key=True) + nom: Mapped[str] = mapped_column(String(32), unique=True) + couleur: Mapped[str] = mapped_column(String(7)) + + listes: Mapped[List["Liste"]] = relationship("Liste", back_populates="bloc") + + +class Nuance(Base): + __tablename__ = "nuance2024" + + code: Mapped[str] = mapped_column(String(8), primary_key=True) + nom: Mapped[str] = mapped_column(String(32), unique=True) + couleur: Mapped[str] = mapped_column(String(7)) + + listes: Mapped[List["Liste"]] = relationship("Liste", back_populates="nuance") + + +class Liste(Base): + __tablename__ = "liste2024" + + id: Mapped[int] = mapped_column(primary_key=True) + nom: Mapped[str] = mapped_column(String(256), unique=True) + numero: Mapped[int] = mapped_column(Integer(), unique=True) + nuance_id: Mapped[str] = mapped_column(ForeignKey("nuance2024.code")) + bloc_id: Mapped[int] = mapped_column(ForeignKey("bloc2024.id")) + + nuance: Mapped[Nuance] = relationship(Nuance, back_populates="listes") + bloc: Mapped[Bloc] = relationship(Bloc, back_populates="listes") + + +class Candidat(Base): + class Genre(enum.Enum): + MASCULIN = "M" + FEMININ = "F" + + class Personnalite(enum.Enum): + DEFAUT = "" + EURODEPUTE = "RPE" + DEPUTE = "DEP" + SENATEUR = "SEN" + MINISTRE = "MIN" + PRESIDENT_CONSEIL_REGIONAL = "PCR" + PRESIDENT_CONSEIL_DEPARTEMENTAL = "PCD" + MAIRE = "MAI" + + __tablename__ = "candidat2024" + + id: Mapped[int] = mapped_column(primary_key=True) + liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id")) + ordre: Mapped[int] = mapped_column(Integer()) + 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)) + code_personnalite: Mapped[str] = mapped_column(Enum(Personnalite)) + sortant: Mapped[bool] = mapped_column(Boolean()) diff --git a/nupes/scripts/__init__.py b/nupes/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nupes/scripts/import_candidats_2024.py b/nupes/scripts/import_candidats_2024.py new file mode 100644 index 0000000..632f7ac --- /dev/null +++ b/nupes/scripts/import_candidats_2024.py @@ -0,0 +1,169 @@ +import csv +import datetime + +from sqlalchemy import Engine, select +from sqlalchemy.orm import Session + +from nupes.data import get_file +from nupes.models.europeennes2024 import Bloc, Nuance, Liste, Candidat + + +def creer_blocs(engine: Engine) -> 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": "#0D378A"}, + {"id": 5, "nom": "Autres", "couleur": "#DCBFA3"} + ] + + with Session(engine) as session: + for bloc_dict in blocs: + if bloc := session.execute(select(Bloc).filter_by(id=bloc_dict["id"])).scalar_one_or_none(): + bloc.nom = bloc_dict["nom"] + bloc.couleur = bloc_dict["couleur"] + else: + session.add(Bloc(**bloc_dict)) + + session.commit() + + +def creer_nuances(engine: Engine) -> None: + nuances = [ + {"code": "LEXG", "nom": "Liste d'extrême gauche", "couleur": "#BB0000"}, + {"code": "LCOM", "nom": "Liste du Parti communiste français", "couleur": "#DD0000"}, + {"code": "LFI", "nom": "Liste de La France insoumise", "couleur": "#CC2443"}, + {"code": "LUG", "nom": "Liste d'union de la gauche", "couleur": "#FF8080"}, + {"code": "LVEC", "nom": "Liste Les Écologistes", "couleur": "#00C000"}, + {"code": "LDVG", "nom": "Liste divers gauche", "couleur": "#FFC0C0"}, + {"code": "LECO", "nom": "Liste écologiste", "couleur": "#77FF77"}, + {"code": "LDIV", "nom": "Liste Divers", "couleur": "#DCDCDC"}, + {"code": "LENS", "nom": "Liste Ensemble", "couleur": "#FFEB00"}, + {"code": "LLR", "nom": "Liste des Républicains", "couleur": "#0066CC"}, + {"code": "LDVD", "nom": "Liste divers droite", "couleur": "#26C4EC"}, + {"code": "LRN", "nom": "Liste du Rassemblement national", "couleur": "#0D378A"}, + {"code": "LREC", "nom": "Liste Reconquête !", "couleur": "#404040"}, + {"code": "LEXD", "nom": "Liste d'extrême droite", "couleur": "#404040"}, + ] + + with Session(engine) as session: + for nuance_dict in nuances: + if nuance := session.execute(select(Nuance).filter_by(code=nuance_dict["code"])).scalar_one_or_none(): + nuance.nom = nuance_dict["nom"] + nuance.couleur = nuance_dict["couleur"] + else: + session.add(Nuance(**nuance_dict)) + + session.commit() + + +def importer_listes(engine: Engine) -> None: + listes = [ + {"numero": 1, "nom": "Pour une humanité souveraine", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 2, "nom": "Pour une démocratie réelle : Décidons nous-mêmes !", + "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 3, "nom": "La France fière, menée par Marion Maréchal et soutenue par Éric Zemmour", + "nuance_id": "LREC", "bloc_id": 4}, + {"numero": 4, "nom": "La France insoumise - Union populaire", "nuance_id": "LFI", "bloc_id": 1}, + {"numero": 5, "nom": "La France revient ! Avec Jordan Bardella et Marine Le Pen", + "nuance_id": "LRN", "bloc_id": 4}, + {"numero": 6, "nom": "Europe Écologie", "nuance_id": "LVEC", "bloc_id": 1}, + {"numero": 7, "nom": "Free Palestine", "nuance_id": "LDIV", "bloc_id": 1}, + {"numero": 8, "nom": "Parti animaliste - Les animaux comptent, votre voix aussi", + "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 9, "nom": "Parti révolutionnaire Communistes", "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 10, "nom": "Parti pirate", "nuance_id": "LDIV", "bloc_id": 1}, + {"numero": 11, "nom": "Besoin d'Europe", "nuance_id": "LENS", "bloc_id": 2}, + {"numero": 12, "nom": "PACE - Parti des citoyens européens, pour l'armée européenne, " + "pour l'Europe sociale, pour la planète !", "nuance_id": "LDIV", "bloc_id": 1}, + {"numero": 13, "nom": "Équinoxe : écologie pratique et renouveau démocratique", + "nuance_id": "LECO", "bloc_id": 5}, + {"numero": 14, "nom": "Écologie positive et territoires", "nuance_id": "LECO", "bloc_id": 5}, + {"numero": 15, "nom": "Liste Asselineau-Frexit, pour le pouvoir d'achat et pour la paix", + "nuance_id": "LDIV", "bloc_id": 4}, + {"numero": 16, "nom": "Paix et Décroissance", "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 17, "nom": "Pour une autre Europe", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 18, "nom": "La droite pour faire entendre la voix de la France en Europe", + "nuance_id": "LLR", "bloc_id": 4}, + {"numero": 19, "nom": "Lutte ouvrière le camp des travailleurs", "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 20, "nom": "Changer l'Europe", "nuance_id": "LDVG", "bloc_id": 1}, + {"numero": 21, "nom": "Nous le peuple", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 22, "nom": "Pour un monde sans frontières ni patrons, urgence révolution !", + "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 23, "nom": "\"Pour le pain, la paix, la liberté !\" présentée par le Parti des travailleurs", + "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 24, "nom": "L'Europe ça suffit !", "nuance_id": "LEXD", "bloc_id": 4}, + {"numero": 25, "nom": "Non ! Prenons-nous en mains", "nuance_id": "LDIV", "bloc_id": 4}, + {"numero": 26, "nom": "Forteresse Europe — Liste d'unité nationaliste", "nuance_id": "LEXD", "bloc_id": 4}, + {"numero": 27, "nom": "Réveiller l'Europe", "nuance_id": "LUG", "bloc_id": 1}, + {"numero": 28, "nom": "Non à l'UE et à l'OTAN, communistes pour la paix et le progrès social", + "nuance_id": "LEXG", "bloc_id": 1}, + {"numero": 29, "nom": "Alliance rurale", "nuance_id": "LDVD", "bloc_id": 3}, + {"numero": 30, "nom": "France libre", "nuance_id": "LDIV", "bloc_id": 4}, + {"numero": 31, "nom": "Europe Territoires Écologie", "nuance_id": "LDVG", "bloc_id": 1}, + {"numero": 32, "nom": "La ruche citoyenne", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 33, "nom": "Gauche unie pour le monde du travail soutenue par Fabien Roussel", + "nuance_id": "LCOM", "bloc_id": 1}, + {"numero": 34, "nom": "Défendre les enfants", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 35, "nom": "Écologie au centre", "nuance_id": "LECO", "bloc_id": 2}, + {"numero": 36, "nom": "Démocratie représentative", "nuance_id": "LDIV", "bloc_id": 1}, + {"numero": 37, "nom": "Espéranto langue commune", "nuance_id": "LDIV", "bloc_id": 5}, + {"numero": 38, "nom": "Liberté démocratique française", "nuance_id": "LDIV", "bloc_id": 4}, + ] + + with Session(engine) as session: + for liste_dict in listes: + if liste := session.execute(select(Liste).filter_by(numero=liste_dict["numero"])).scalar_one_or_none(): + liste.nom = liste_dict["nom"] + liste.nuance_id = liste_dict["nuance_id"] + liste.bloc_id = liste_dict["bloc_id"] + else: + session.add(Liste(**liste_dict)) + + session.commit() + + +def importer_candidats(engine: Engine) -> None: + DATASET_URL = "https://www.data.gouv.fr/fr/datasets/r/483cd4bd-1b0e-4b52-a923-aadadf9c8f62" + file = get_file(DATASET_URL, "candidats_2024.csv") + + with file.open('r') as f: + with Session(engine) as session: + reader = csv.DictReader(f, delimiter=';') + for line in reader: + line: dict[str, str] + numero_panneau = line.get("Numéro de panneau") + liste = session.execute(select(Liste).filter_by(numero=numero_panneau)).scalar_one() + + if candidat := session.execute(select(Candidat).filter_by(liste_id=liste.id, ordre=line['Ordre'])) \ + .scalar_one_or_none(): + candidat.liste_id = liste.id + candidat.nom = line['Nom sur le bulletin de vote'] + candidat.prenom = line['Prénom sur le bulletin de vote'] + candidat.sexe = Candidat.Genre(line['Sexe']) + candidat.date_naissance = datetime.datetime.strptime(line['Date de naissance'], "%d/%m/%Y").date() + candidat.profession = line['Profession'] + candidat.code_personnalite = Candidat.Personnalite(line['Code personnalité']) + candidat.sortant = line['Sortant'] == "OUI" + else: + candidat = Candidat( + liste_id=liste.id, + ordre=int(line['Ordre']), + nom=line['Nom sur le bulletin de vote'], + prenom=line['Prénom sur le bulletin de vote'], + sexe=Candidat.Genre(line['Sexe']).name, + date_naissance=datetime.datetime.strptime(line['Date de naissance'], "%d/%m/%Y").date(), + profession=line['Profession'], + code_personnalite=Candidat.Personnalite(line['Code personnalité']).name, + sortant=line['Sortant'] == "OUI", + ) + session.add(candidat) + + session.commit() + + +def run(engine: Engine) -> None: + creer_blocs(engine) + creer_nuances(engine) + importer_listes(engine) + importer_candidats(engine) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c872d26 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +alembic~=1.13.1 +python-dotenv~=1.0.1 +requests~=2.32.3 +SQLAlchemy~=2.0.30