Expand courts/regsitry models

This commit is contained in:
Daniil Fajnberg 2022-08-29 21:06:33 +02:00
parent 8cd13d8337
commit cf83e0074e
3 changed files with 155 additions and 19 deletions

View File

@ -10,6 +10,7 @@ from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import TIMESTAMP from sqlalchemy.sql.sqltypes import TIMESTAMP
from sqlalchemy_utils.functions.orm import get_columns from sqlalchemy_utils.functions.orm import get_columns
from sqlalchemy_utils.listeners import force_auto_coercion from sqlalchemy_utils.listeners import force_auto_coercion
from sqlalchemy_utils.types.choice import Choice
from sqlmodel.main import Field, SQLModel from sqlmodel.main import Field, SQLModel
from compub.exceptions import NoDatabaseConfigured from compub.exceptions import NoDatabaseConfigured
@ -91,3 +92,7 @@ class AbstractBase(SQLModel):
@staticmethod @staticmethod
def get_non_repr_fields() -> list[str]: def get_non_repr_fields() -> list[str]:
return ['id', 'date_created', 'date_updated'] return ['id', 'date_created', 'date_updated']
def get_choice_value(obj: Choice | str) -> str:
return obj.value if isinstance(obj, Choice) else obj

View File

@ -1,5 +1,5 @@
from datetime import date from datetime import date
from typing import Optional from typing import Optional, TYPE_CHECKING
from slugify import slugify from slugify import slugify
from sqlalchemy.engine.base import Connection from sqlalchemy.engine.base import Connection
@ -16,6 +16,9 @@ from compub.utils import multi_max
from .base import AbstractBase, DEFAULT_PK_TYPE as PK from .base import AbstractBase, DEFAULT_PK_TYPE as PK
from .geography import Address from .geography import Address
if TYPE_CHECKING:
from .courts import RegisterNumber
__all__ = [ __all__ = [
'LegalForm', 'LegalForm',
@ -163,6 +166,10 @@ class Company(AbstractBase, table=True):
back_populates='company', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='company', sa_relationship_kwargs={'lazy': 'selectin'}
) )
register_numbers: list['RegisterNumber'] = Relationship(
back_populates='company', sa_relationship_kwargs={'lazy': 'selectin'}
)
def __str__(self) -> str: def __str__(self) -> str:
return str(self.current_name or f"<Company {self.id}>") return str(self.current_name or f"<Company {self.id}>")

View File

@ -1,24 +1,43 @@
from enum import Enum as EnumPy
from typing import Optional from typing import Optional
from sqlalchemy.sql.schema import Column, Index from sqlalchemy.sql.schema import Column, Index
from sqlalchemy.sql.sqltypes import Unicode from sqlalchemy.sql.sqltypes import Unicode, Enum as EnumSQL
from sqlmodel.main import Field, Relationship from sqlmodel.main import Field, Relationship
from .base import AbstractBase, DEFAULT_PK_TYPE as PK from .base import AbstractBase, DEFAULT_PK_TYPE as PK
from .companies import Company
from .geography import StateProvince, Address from .geography import StateProvince, Address
__all__ = [
'CourtClass',
'CourtDepartment',
'Court',
'RegisterBranch',
'RegisterNumber',
]
# Amtsgericht -> Landgericht -> OLG -> BGH # Amtsgericht -> Landgericht -> OLG -> BGH
class CourtClass(AbstractBase, table=True): class CourtClass(AbstractBase, table=True):
__tablename__ = 'court_class' __tablename__ = 'court_class'
# Fields # Fields
short: str = Field(max_length=32, nullable=False, index=True) short: str = Field(
name: str = Field(max_length=255, sa_column=Column(Unicode(255))) max_length=32,
nullable=False,
index=True
)
name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
# Relationships # Relationships
courts: list['Court'] = Relationship( courts: list['Court'] = Relationship(
back_populates='court_class', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='court_class',
sa_relationship_kwargs={'lazy': 'selectin'}
) )
def __str__(self) -> str: def __str__(self) -> str:
@ -33,10 +52,16 @@ class CourtDepartmentCourtLink(AbstractBase, table=True):
# Relationships # Relationships
court_department_id: Optional[PK] = Field( court_department_id: Optional[PK] = Field(
foreign_key='court_department.id', default=None, nullable=False, primary_key=True foreign_key='court_department.id',
default=None,
nullable=False,
primary_key=True
) )
court_id: Optional[PK] = Field( court_id: Optional[PK] = Field(
foreign_key='court.id', default=None, nullable=False, primary_key=True foreign_key='court.id',
default=None,
nullable=False,
primary_key=True
) )
@ -48,11 +73,15 @@ class CourtDepartment(AbstractBase, table=True):
__INS_COURT_PK__ = 2 __INS_COURT_PK__ = 2
# Fields # Fields
name: str = Field(max_length=255, sa_column=Column(Unicode(255))) name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
# Relationships # Relationships
courts: list['Court'] = Relationship( courts: list['Court'] = Relationship(
back_populates='court_departments', link_model=CourtDepartmentCourtLink, back_populates='court_departments',
link_model=CourtDepartmentCourtLink,
sa_relationship_kwargs={'lazy': 'selectin'} sa_relationship_kwargs={'lazy': 'selectin'}
) )
@ -64,32 +93,50 @@ class Court(AbstractBase, table=True):
__tablename__ = 'court' __tablename__ = 'court'
# Fields # Fields
name: str = Field(max_length=255, sa_column=Column(Unicode(255))) name: str = Field(
max_length=255,
sa_column=Column(Unicode(255))
)
# Relationships # Relationships
court_class_id: Optional[PK] = Field( court_class_id: Optional[PK] = Field(
foreign_key='court_class.id', default=None, nullable=False, index=True foreign_key='court_class.id',
default=None,
nullable=False,
index=True
) )
court_class: Optional[CourtClass] = Relationship( court_class: Optional[CourtClass] = Relationship(
back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
) )
state_province_id: Optional[PK] = Field( state_province_id: Optional[PK] = Field(
foreign_key='state_province.id', default=None, nullable=True, index=True foreign_key='state_province.id',
default=None,
nullable=True,
index=True
) )
state_province: Optional[StateProvince] = Relationship( state_province: Optional[StateProvince] = Relationship(
back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
) )
address_id: Optional[PK] = Field( address_id: Optional[PK] = Field(
foreign_key='address.id', default=None, nullable=True, index=True foreign_key='address.id',
default=None,
nullable=True,
index=True
) )
address: Optional[Address] = Relationship( address: Optional[Address] = Relationship(
back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='courts',
sa_relationship_kwargs={'lazy': 'selectin'}
) )
parent_court_id: Optional[PK] = Field( parent_court_id: Optional[PK] = Field(
foreign_key='court.id', default=None, nullable=True, index=True foreign_key='court.id',
default=None,
nullable=True,
index=True
) )
parent_court: Optional['Court'] = Relationship( parent_court: Optional['Court'] = Relationship(
back_populates='sub_courts', back_populates='sub_courts',
@ -99,12 +146,89 @@ class Court(AbstractBase, table=True):
) )
) )
sub_courts: list['Court'] = Relationship( sub_courts: list['Court'] = Relationship(
back_populates='parent_court', sa_relationship_kwargs={'lazy': 'selectin'} back_populates='parent_court',
sa_relationship_kwargs={'lazy': 'selectin'}
) )
court_departments: list[CourtDepartment] = Relationship( court_departments: list[CourtDepartment] = Relationship(
back_populates='courts', link_model=CourtDepartmentCourtLink, sa_relationship_kwargs={'lazy': 'selectin'} 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: def __str__(self) -> str:
return str(self.name) 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}'