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 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) nom_majuscules: Mapped[str] = mapped_column(String(256), unique=True, nullable=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") candidats: Mapped[List["Candidat"]] = relationship("Candidat", back_populates="liste") resultats_nationaux: Mapped[List["VoixListeFrance"]] = relationship("VoixListeFrance", back_populates="liste") resultats_par_region: Mapped[List["VoixListeRegion"]] = relationship("VoixListeRegion", back_populates="liste") resultats_par_departement: Mapped[List["VoixListeDepartement"]] = relationship("VoixListeDepartement", back_populates="liste") resultats_par_commune: Mapped[List["VoixListeCommune"]] = relationship("VoixListeCommune", back_populates="liste") resultats_par_bureau_vote: Mapped[List["VoixListeBureauVote"]] = relationship("VoixListeBureauVote", back_populates="liste") 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()) liste: Mapped[Liste] = relationship(Liste, back_populates="candidats") class ResultatsFrance(Base): __tablename__ = "resultats2024_france" id: Mapped[int] = mapped_column(primary_key=True) inscrits: Mapped[int] = mapped_column(Integer(), default=0) votants: Mapped[int] = mapped_column(Integer(), default=0) abstentions: Mapped[int] = mapped_column(Integer(), default=0) exprimes: Mapped[int] = mapped_column(Integer(), default=0) blancs: Mapped[int] = mapped_column(Integer(), default=0) nuls: Mapped[int] = mapped_column(Integer(), default=0) resultats_regions: Mapped[List["ResultatsRegion"]] = relationship("ResultatsRegion", back_populates="resultats_france") voix_listes: Mapped[List["VoixListeFrance"]] = relationship("VoixListeFrance", back_populates="resultats_france") class ResultatsRegion(Base): __tablename__ = "resultats2024_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("resultats2024_france.id")) inscrits: Mapped[int] = mapped_column(Integer(), default=0) votants: Mapped[int] = mapped_column(Integer(), default=0) abstentions: Mapped[int] = mapped_column(Integer(), default=0) exprimes: Mapped[int] = mapped_column(Integer(), default=0) blancs: Mapped[int] = mapped_column(Integer(), default=0) nuls: Mapped[int] = mapped_column(Integer(), default=0) region = relationship(Region, back_populates="resultats2024") resultats_france = relationship(ResultatsFrance, back_populates="resultats_regions") resultats_departements: Mapped[List["ResultatsDepartement"]] = relationship("ResultatsDepartement", back_populates="resultats_region") voix_listes: Mapped[List["VoixListeRegion"]] = relationship("VoixListeRegion", back_populates="resultats_region") class ResultatsDepartement(Base): __tablename__ = "resultats2024_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("resultats2024_region.id")) inscrits: Mapped[int] = mapped_column(Integer(), default=0) votants: Mapped[int] = mapped_column(Integer(), default=0) abstentions: Mapped[int] = mapped_column(Integer(), default=0) exprimes: Mapped[int] = mapped_column(Integer(), default=0) blancs: Mapped[int] = mapped_column(Integer(), default=0) nuls: Mapped[int] = mapped_column(Integer(), default=0) departement = relationship(Departement, back_populates="resultats2024") resultats_region = relationship(ResultatsRegion, back_populates="resultats_departements") resultats_communes: Mapped[List["ResultatsCommune"]] = relationship("ResultatsCommune", back_populates="resultats_departement") voix_listes: Mapped[List["VoixListeDepartement"]] = relationship("VoixListeDepartement", back_populates="resultats_departement") class ResultatsCommune(Base): __tablename__ = "resultats2024_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("resultats2024_departement.id")) inscrits: Mapped[int] = mapped_column(Integer(), default=0) votants: Mapped[int] = mapped_column(Integer(), default=0) abstentions: Mapped[int] = mapped_column(Integer(), default=0) exprimes: Mapped[int] = mapped_column(Integer(), default=0) blancs: Mapped[int] = mapped_column(Integer(), default=0) nuls: Mapped[int] = mapped_column(Integer(), default=0) commune = relationship(Commune, back_populates="resultats2024") resultats_departement = relationship(ResultatsDepartement, back_populates="resultats_communes") resultats_bureaux_vote: Mapped[List["ResultatsBureauVote"]] = relationship("ResultatsBureauVote", back_populates="resultats_commune") voix_listes: Mapped[List["VoixListeCommune"]] = relationship("VoixListeCommune", back_populates="resultats_commune") class ResultatsBureauVote(Base): __tablename__ = "resultats2024_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("resultats2024_commune.id")) inscrits: Mapped[int] = mapped_column(Integer(), default=0) votants: Mapped[int] = mapped_column(Integer(), default=0) abstentions: Mapped[int] = mapped_column(Integer(), default=0) exprimes: Mapped[int] = mapped_column(Integer(), default=0) blancs: Mapped[int] = mapped_column(Integer(), default=0) nuls: Mapped[int] = mapped_column(Integer(), default=0) bureau_vote = relationship("BureauVote", back_populates="resultats2024") resultats_commune = relationship(ResultatsCommune, back_populates="resultats_bureaux_vote") voix_listes: Mapped[List["VoixListeBureauVote"]] = relationship("VoixListeBureauVote", back_populates="resultats_bureau_vote") class VoixListeFrance(Base): __tablename__ = "voix2024_france" liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id"), primary_key=True) resultats_france_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_france.id"), primary_key=True) voix: Mapped[int] = mapped_column(Integer(), default=0) liste: Mapped[Liste] = relationship(Liste, back_populates="resultats_nationaux") resultats_france: Mapped[ResultatsFrance] = relationship(ResultatsFrance, back_populates="voix_listes") class VoixListeRegion(Base): __tablename__ = "voix2024_region" liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id"), primary_key=True) resultats_region_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_region.id"), primary_key=True) voix: Mapped[int] = mapped_column(Integer(), default=0) liste: Mapped[Liste] = relationship(Liste, back_populates="resultats_par_region") resultats_region: Mapped[ResultatsRegion] = relationship(ResultatsRegion, back_populates="voix_listes") class VoixListeDepartement(Base): __tablename__ = "voix2024_departement" liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id"), primary_key=True) resultats_departement_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_departement.id"), primary_key=True) voix: Mapped[int] = mapped_column(Integer(), default=0) liste: Mapped[Liste] = relationship(Liste, back_populates="resultats_par_departement") resultats_departement: Mapped[ResultatsDepartement] = relationship(ResultatsDepartement, back_populates="voix_listes") class VoixListeCommune(Base): __tablename__ = "voix2024_commune" liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id"), primary_key=True) resultats_commune_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_commune.id"), primary_key=True) voix: Mapped[int] = mapped_column(Integer(), default=0) liste: Mapped[Liste] = relationship(Liste, back_populates="resultats_par_commune") resultats_commune: Mapped[ResultatsCommune] = relationship(ResultatsCommune, back_populates="voix_listes") class VoixListeBureauVote(Base): __tablename__ = "voix2024_bureau_vote" liste_id: Mapped[int] = mapped_column(ForeignKey("liste2024.id"), primary_key=True) resultats_bureau_vote_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_bureau_vote.id"), primary_key=True) voix: Mapped[int] = mapped_column(Integer(), default=0) liste: Mapped[Liste] = relationship(Liste, back_populates="resultats_par_bureau_vote") resultats_bureau_vote: Mapped[ResultatsBureauVote] = relationship(ResultatsBureauVote, back_populates="voix_listes")