nupes-elections/nupes/models/geographie.py

75 lines
3.0 KiB
Python

from typing import List
from sqlalchemy import Float, ForeignKey, Integer, JSON, String
from sqlalchemy.orm import mapped_column, Mapped, relationship
from nupes.models import Base
class Region(Base):
__tablename__ = "region"
code_insee: Mapped[str] = mapped_column(String(3), primary_key=True)
libelle: Mapped[str] = mapped_column(String(64), unique=True)
geometry: Mapped[dict] = mapped_column(JSON())
departements: Mapped[List["Departement"]] = relationship("Departement", back_populates="region")
resultats2024 = relationship("ResultatsRegion", back_populates="region")
class Departement(Base):
__tablename__ = "departement"
code_insee: Mapped[str] = mapped_column(String(3), primary_key=True)
libelle: Mapped[str] = mapped_column(String(64), unique=True)
region_code: Mapped[int] = mapped_column(ForeignKey("region.code_insee"))
geometry: Mapped[dict] = mapped_column(JSON())
region: Mapped[Region] = relationship(Region, back_populates="departements")
communes: Mapped[List["Commune"]] = relationship("Commune", back_populates="departement")
resultats2024 = relationship("ResultatsDepartement", back_populates="departement")
class Commune(Base):
__tablename__ = "commune"
code_insee: Mapped[str] = mapped_column(String(5), primary_key=True)
libelle: Mapped[str] = mapped_column(String(64))
departement_code: Mapped[str] = mapped_column(ForeignKey("departement.code_insee"))
geometry: Mapped[dict] = mapped_column(JSON())
departement: Mapped[Departement] = relationship(Departement, back_populates="communes")
bureaux_vote: Mapped[List["BureauVote"]] = relationship("BureauVote", back_populates="commune")
resultats2024 = relationship("ResultatsCommune", back_populates="commune")
class Circonscription(Base):
__tablename__ = "circonscription"
id: Mapped[str] = mapped_column(String(6), primary_key=True)
departement_code: Mapped[str] = mapped_column(ForeignKey("departement.code_insee"))
numero: Mapped[int] = mapped_column(Integer())
departement: Mapped[Departement] = relationship(Departement)
bureaux_vote: Mapped[List["BureauVote"]] = relationship("BureauVote", back_populates="circonscription")
class BureauVote(Base):
__tablename__ = "bureau_vote"
id: Mapped[str] = mapped_column(String(16), primary_key=True)
commune_code: Mapped[str] = mapped_column(ForeignKey("commune.code_insee"))
code_bureau: Mapped[str] = mapped_column(String(8))
circo_code: Mapped[str] = mapped_column(ForeignKey("circonscription.id"))
libelle: Mapped[str] = mapped_column(String(256))
adresse: Mapped[str] = mapped_column(String(256), nullable=True)
geometry: Mapped[dict] = mapped_column(JSON())
commune: Mapped[Commune] = relationship(Commune, back_populates="bureaux_vote")
circonscription: Mapped[Circonscription] = relationship(Circonscription, back_populates="bureaux_vote")
resultats2024 = relationship("ResultatsBureauVote", back_populates="bureau_vote")