from enum import Enum as EnumPy from typing import Optional from sqlalchemy.sql.schema import Column, Index from sqlalchemy.sql.sqltypes import Unicode, Enum as EnumSQL from sqlmodel.main import Field, Relationship from .base import AbstractBase, DEFAULT_PK_TYPE as PK from .companies import Company from .geography import StateProvince, Address __all__ = [ 'CourtClass', 'CourtDepartment', 'Court', 'RegisterBranch', 'RegisterNumber', ] # Amtsgericht -> Landgericht -> OLG -> BGH class CourtClass(AbstractBase, table=True): __tablename__ = 'court_class' # Fields short: str = Field( max_length=32, nullable=False, index=True ) name: str = Field( max_length=255, sa_column=Column(Unicode(255)) ) # Relationships courts: list['Court'] = Relationship( back_populates='court_class', sa_relationship_kwargs={'lazy': 'selectin'} ) def __str__(self) -> str: return str(self.name) class CourtDepartmentCourtLink(AbstractBase, table=True): __tablename__ = 'court_department_court' __table_args__ = ( Index('ux_court_department_court', 'court_department_id', 'court_id', unique=True), ) # Relationships court_department_id: Optional[PK] = Field( foreign_key='court_department.id', default=None, nullable=False, primary_key=True ) court_id: Optional[PK] = Field( foreign_key='court.id', default=None, nullable=False, primary_key=True ) # Abteilung innerhalb eines Amtsgerichts (z. B. Registergericht, Insolvenzgericht etc.) class CourtDepartment(AbstractBase, table=True): __tablename__ = 'court_department' __REG_COURT_PK__ = 1 __INS_COURT_PK__ = 2 # Fields name: str = Field( max_length=255, sa_column=Column(Unicode(255)) ) # Relationships courts: list['Court'] = Relationship( back_populates='court_departments', link_model=CourtDepartmentCourtLink, sa_relationship_kwargs={'lazy': 'selectin'} ) def __str__(self) -> str: return str(self.name) class Court(AbstractBase, table=True): __tablename__ = 'court' # Fields name: str = Field( max_length=255, sa_column=Column(Unicode(255)) ) # Relationships court_class_id: Optional[PK] = Field( foreign_key='court_class.id', default=None, nullable=False, index=True ) court_class: Optional[CourtClass] = Relationship( back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} ) state_province_id: Optional[PK] = Field( foreign_key='state_province.id', default=None, nullable=True, index=True ) state_province: Optional[StateProvince] = Relationship( back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} ) address_id: Optional[PK] = Field( foreign_key='address.id', default=None, nullable=True, index=True ) address: Optional[Address] = Relationship( back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} ) parent_court_id: Optional[PK] = Field( foreign_key='court.id', default=None, nullable=True, index=True ) parent_court: Optional['Court'] = Relationship( back_populates='sub_courts', sa_relationship_kwargs=dict( lazy='selectin', remote_side='Court.id' ) ) sub_courts: list['Court'] = Relationship( back_populates='parent_court', sa_relationship_kwargs={'lazy': 'selectin'} ) court_departments: list[CourtDepartment] = Relationship( back_populates='courts', link_model=CourtDepartmentCourtLink, sa_relationship_kwargs={'lazy': 'selectin'} ) register_numbers: list['RegisterNumber'] = Relationship( back_populates='court', sa_relationship_kwargs={'lazy': 'selectin'} ) def __str__(self) -> str: return str(self.name) class RegisterBranch(str, EnumPy): HRA = 'HRA' HRB = 'HRB' GnR = 'GnR' PR = 'PR' VR = 'VR' class RegisterNumber(AbstractBase, table=True): __tablename__ = 'register_number' # Fields branch: Optional[RegisterBranch] = Field( default=None, sa_column=Column( EnumSQL(RegisterBranch), nullable=True, index=True ) ) number: str = Field( max_length=255, index=True ) suffix: Optional[str] = Field( max_length=4, nullable=True ) ureg_company_id: Optional[int] = Field( nullable=True, index=True ) # Relationships court_id: Optional[PK] = Field( foreign_key='court.id', default=None, index=True ) court: Optional[Court] = Relationship( back_populates='register_numbers', sa_relationship_kwargs=dict( lazy='selectin' ) ) company_id: Optional[PK] = Field( foreign_key='company.id', default=None, index=True ) company: Optional[Company] = Relationship( back_populates='register_numbers', sa_relationship_kwargs=dict( lazy='selectin' ) ) def __str__(self) -> str: return f'{self.with_branch_code} ({self.company.current_name})' @property def with_branch_code(self) -> str: return f'{self.branch} {self.number}' if self.branch else str(self.number) @property def verbose_id(self) -> str: return f'{self.court} {self.with_branch_code}'