generated from daniil-berg/boilerplate-py
111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy.sql.schema import Column, Index
|
|
from sqlalchemy.sql.sqltypes import Unicode
|
|
from sqlmodel.main import Field, Relationship
|
|
|
|
from .base import AbstractBase, DEFAULT_PK_TYPE as PK
|
|
from .geography import StateProvince, Address
|
|
|
|
|
|
# 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'}
|
|
)
|
|
|
|
def __str__(self) -> str:
|
|
return str(self.name)
|