Début analyse résultats, importation des candidatures 2024
This commit is contained in:
commit
ed4ede0d7f
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
||||
SQLALCHEMY_URL=postgresql://nupes:nupes@localhost:5432/nupes
|
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
__pycache__
|
||||
|
||||
.venv
|
||||
|
||||
.env
|
||||
*.sqlite3
|
||||
|
||||
data/
|
||||
|
||||
migrations/versions/*
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -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
|
50
.idea/inspectionProfiles/Project_Default.xml
Normal file
50
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,50 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredPackages">
|
||||
<value>
|
||||
<list size="29">
|
||||
<item index="0" class="java.lang.String" itemvalue="mysqlclient" />
|
||||
<item index="1" class="java.lang.String" itemvalue="six" />
|
||||
<item index="2" class="java.lang.String" itemvalue="crispy-bootstrap5" />
|
||||
<item index="3" class="java.lang.String" itemvalue="django-rest-polymorphic" />
|
||||
<item index="4" class="java.lang.String" itemvalue="psycopg2-binary" />
|
||||
<item index="5" class="java.lang.String" itemvalue="django-mailer" />
|
||||
<item index="6" class="java.lang.String" itemvalue="requests" />
|
||||
<item index="7" class="java.lang.String" itemvalue="django-tables2" />
|
||||
<item index="8" class="java.lang.String" itemvalue="django-extensions" />
|
||||
<item index="9" class="java.lang.String" itemvalue="python-magic" />
|
||||
<item index="10" class="java.lang.String" itemvalue="elasticsearch" />
|
||||
<item index="11" class="java.lang.String" itemvalue="django-phonenumber-field" />
|
||||
<item index="12" class="java.lang.String" itemvalue="uvicorn" />
|
||||
<item index="13" class="java.lang.String" itemvalue="djangorestframework" />
|
||||
<item index="14" class="java.lang.String" itemvalue="sympasoap" />
|
||||
<item index="15" class="java.lang.String" itemvalue="websockets" />
|
||||
<item index="16" class="java.lang.String" itemvalue="django-polymorphic" />
|
||||
<item index="17" class="java.lang.String" itemvalue="gspread" />
|
||||
<item index="18" class="java.lang.String" itemvalue="django-crispy-forms" />
|
||||
<item index="19" class="java.lang.String" itemvalue="phonenumbers" />
|
||||
<item index="20" class="java.lang.String" itemvalue="ipython" />
|
||||
<item index="21" class="java.lang.String" itemvalue="pypdf" />
|
||||
<item index="22" class="java.lang.String" itemvalue="gunicorn" />
|
||||
<item index="23" class="java.lang.String" itemvalue="channels-redis" />
|
||||
<item index="24" class="java.lang.String" itemvalue="pandas" />
|
||||
<item index="25" class="java.lang.String" itemvalue="odfpy" />
|
||||
<item index="26" class="java.lang.String" itemvalue="channels" />
|
||||
<item index="27" class="java.lang.String" itemvalue="django-filter" />
|
||||
<item index="28" class="java.lang.String" itemvalue="Django" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredErrors">
|
||||
<list>
|
||||
<option value="N806" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
7
.idea/misc.xml
Normal file
7
.idea/misc.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.12 (nupes-elections)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (nupes-elections)" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/nupes-elections.iml" filepath="$PROJECT_DIR$/.idea/nupes-elections.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
10
.idea/nupes-elections.iml
Normal file
10
.idea/nupes-elections.iml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
116
alembic.ini
Normal file
116
alembic.ini
Normal file
@ -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
|
1
alembic/README
Normal file
1
alembic/README
Normal file
@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
81
alembic/env.py
Normal file
81
alembic/env.py
Normal file
@ -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()
|
26
alembic/script.py.mako
Normal file
26
alembic/script.py.mako
Normal file
@ -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"}
|
@ -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 ###
|
39
main.py
Executable file
39
main.py
Executable file
@ -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()
|
0
nupes/__init__.py
Normal file
0
nupes/__init__.py
Normal file
25
nupes/data.py
Normal file
25
nupes/data.py
Normal file
@ -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
|
2
nupes/models/__init__.py
Normal file
2
nupes/models/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .base import Base
|
||||
from .europeennes2024 import Bloc as Bloc2024, Nuance as Nuance2024, Liste as Liste2024, Candidat as Candidat2024
|
5
nupes/models/base.py
Normal file
5
nupes/models/base.py
Normal file
@ -0,0 +1,5 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
70
nupes/models/europeennes2024.py
Normal file
70
nupes/models/europeennes2024.py
Normal file
@ -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())
|
0
nupes/scripts/__init__.py
Normal file
0
nupes/scripts/__init__.py
Normal file
169
nupes/scripts/import_candidats_2024.py
Normal file
169
nupes/scripts/import_candidats_2024.py
Normal file
@ -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)
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
||||
alembic~=1.13.1
|
||||
python-dotenv~=1.0.1
|
||||
requests~=2.32.3
|
||||
SQLAlchemy~=2.0.30
|
Loading…
Reference in New Issue
Block a user