From cf83e0074ed11c12aa48c36b83f3e0a0965e7ea7 Mon Sep 17 00:00:00 2001 From: daniil Date: Mon, 29 Aug 2022 21:06:33 +0200 Subject: [PATCH] Expand courts/regsitry models --- src/compub/models/base.py | 5 ++ src/compub/models/companies.py | 9 +- src/compub/models/courts.py | 160 +++++++++++++++++++++++++++++---- 3 files changed, 155 insertions(+), 19 deletions(-) diff --git a/src/compub/models/base.py b/src/compub/models/base.py index 3754302..064a203 100644 --- a/src/compub/models/base.py +++ b/src/compub/models/base.py @@ -10,6 +10,7 @@ from sqlalchemy.sql.schema import Column from sqlalchemy.sql.sqltypes import TIMESTAMP from sqlalchemy_utils.functions.orm import get_columns from sqlalchemy_utils.listeners import force_auto_coercion +from sqlalchemy_utils.types.choice import Choice from sqlmodel.main import Field, SQLModel from compub.exceptions import NoDatabaseConfigured @@ -91,3 +92,7 @@ class AbstractBase(SQLModel): @staticmethod def get_non_repr_fields() -> list[str]: return ['id', 'date_created', 'date_updated'] + + +def get_choice_value(obj: Choice | str) -> str: + return obj.value if isinstance(obj, Choice) else obj diff --git a/src/compub/models/companies.py b/src/compub/models/companies.py index cf01144..53afbe5 100644 --- a/src/compub/models/companies.py +++ b/src/compub/models/companies.py @@ -1,5 +1,5 @@ from datetime import date -from typing import Optional +from typing import Optional, TYPE_CHECKING from slugify import slugify 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 .geography import Address +if TYPE_CHECKING: + from .courts import RegisterNumber + __all__ = [ 'LegalForm', @@ -163,6 +166,10 @@ class Company(AbstractBase, table=True): 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: return str(self.current_name or f"") diff --git a/src/compub/models/courts.py b/src/compub/models/courts.py index 6f15135..398b9ae 100644 --- a/src/compub/models/courts.py +++ b/src/compub/models/courts.py @@ -1,24 +1,43 @@ +from enum import Enum as EnumPy from typing import Optional 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 .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))) + 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'} + back_populates='court_class', + sa_relationship_kwargs={'lazy': 'selectin'} ) def __str__(self) -> str: @@ -33,10 +52,16 @@ class CourtDepartmentCourtLink(AbstractBase, table=True): # Relationships 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( - 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 # Fields - name: str = Field(max_length=255, sa_column=Column(Unicode(255))) + name: str = Field( + max_length=255, + sa_column=Column(Unicode(255)) + ) # Relationships courts: list['Court'] = Relationship( - back_populates='court_departments', link_model=CourtDepartmentCourtLink, + back_populates='court_departments', + link_model=CourtDepartmentCourtLink, sa_relationship_kwargs={'lazy': 'selectin'} ) @@ -64,32 +93,50 @@ class Court(AbstractBase, table=True): __tablename__ = 'court' # Fields - name: str = Field(max_length=255, sa_column=Column(Unicode(255))) + 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 + foreign_key='court_class.id', + default=None, + nullable=False, + index=True ) 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( - 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( - back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} + back_populates='courts', + sa_relationship_kwargs={'lazy': 'selectin'} ) 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( - back_populates='courts', sa_relationship_kwargs={'lazy': 'selectin'} + back_populates='courts', + sa_relationship_kwargs={'lazy': 'selectin'} ) 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( back_populates='sub_courts', @@ -99,12 +146,89 @@ class Court(AbstractBase, table=True): ) ) 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( - 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: 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}'