compub/src/compub/models/courts.py

235 lines
5.6 KiB
Python
Raw Normal View History

2022-08-29 21:06:33 +02:00
from enum import Enum as EnumPy
2022-08-16 21:53:17 +02:00
from typing import Optional
from sqlalchemy.sql.schema import Column, Index
2022-08-29 21:06:33 +02:00
from sqlalchemy.sql.sqltypes import Unicode, Enum as EnumSQL
2022-08-16 21:53:17 +02:00
from sqlmodel.main import Field, Relationship
2022-08-17 15:03:41 +02:00
from .base import AbstractBase, DEFAULT_PK_TYPE as PK
2022-08-29 21:06:33 +02:00
from .companies import Company
2022-08-16 21:53:17 +02:00
from .geography import StateProvince, Address
2022-08-29 21:06:33 +02:00
__all__ = [
'CourtClass',
'CourtDepartment',
'Court',
'RegisterBranch',
'RegisterNumber',
]
2022-08-16 21:53:17 +02:00
# Amtsgericht -> Landgericht -> OLG -> BGH
class CourtClass(AbstractBase, table=True):
__tablename__ = 'court_class'
# Fields
2022-08-29 21:06:33 +02:00
short: str = Field(
max_length=32,
nullable=False,
index=True
)
name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
2022-08-16 21:53:17 +02:00
# Relationships
courts: list['Court'] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='court_class',
sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
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
2022-08-17 15:03:41 +02:00
court_department_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='court_department.id',
default=None,
nullable=False,
primary_key=True
2022-08-16 21:53:17 +02:00
)
2022-08-17 15:03:41 +02:00
court_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='court.id',
default=None,
nullable=False,
primary_key=True
2022-08-16 21:53:17 +02:00
)
# 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
2022-08-29 21:06:33 +02:00
name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
2022-08-16 21:53:17 +02:00
# Relationships
courts: list['Court'] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='court_departments',
link_model=CourtDepartmentCourtLink,
2022-08-16 21:53:17 +02:00
sa_relationship_kwargs={'lazy': 'selectin'}
)
def __str__(self) -> str:
return str(self.name)
class Court(AbstractBase, table=True):
__tablename__ = 'court'
# Fields
2022-08-29 21:06:33 +02:00
name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
2022-08-16 21:53:17 +02:00
# Relationships
2022-08-17 15:03:41 +02:00
court_class_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='court_class.id',
default=None,
nullable=False,
index=True
2022-08-16 21:53:17 +02:00
)
court_class: Optional[CourtClass] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
2022-08-17 15:03:41 +02:00
state_province_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='state_province.id',
default=None,
nullable=True,
index=True
2022-08-16 21:53:17 +02:00
)
state_province: Optional[StateProvince] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
2022-08-17 15:03:41 +02:00
address_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='address.id',
default=None,
nullable=True,
index=True
2022-08-16 21:53:17 +02:00
)
address: Optional[Address] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
2022-08-17 15:03:41 +02:00
parent_court_id: Optional[PK] = Field(
2022-08-29 21:06:33 +02:00
foreign_key='court.id',
default=None,
nullable=True,
index=True
2022-08-16 21:53:17 +02:00
)
parent_court: Optional['Court'] = Relationship(
back_populates='sub_courts',
sa_relationship_kwargs=dict(
lazy='selectin',
remote_side='Court.id'
)
)
sub_courts: list['Court'] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='parent_court',
sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
court_departments: list[CourtDepartment] = Relationship(
2022-08-29 21:06:33 +02:00
back_populates='courts',
link_model=CourtDepartmentCourtLink,
sa_relationship_kwargs={'lazy': 'selectin'}
)
register_numbers: list['RegisterNumber'] = Relationship(
back_populates='court', sa_relationship_kwargs={'lazy': 'selectin'}
2022-08-16 21:53:17 +02:00
)
def __str__(self) -> str:
return str(self.name)
2022-08-29 21:06:33 +02:00
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}'