2025-08-29 18:19:46 +02:00
|
|
|
from sqlalchemy import String, Text, ForeignKey, event, select, UniqueConstraint
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, declared_attr, Session
|
|
|
|
|
|
|
|
|
|
from .base import Base, bidirectional_relationship
|
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
|
from .contact import ContactForeignKey
|
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
|
from .status import StatusForeignKey
|
|
|
|
|
from .user import Versioned, User
|
|
|
|
|
|
|
|
|
|
__all__ = ["Country", "LocationCode", "Location"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Country(StatusForeignKey, Versioned, Base):
|
|
|
|
|
|
2025-08-31 07:53:17 +02:00
|
|
|
code: Mapped[str] = mapped_column(String(2), unique=True)
|
2025-08-29 18:19:46 +02:00
|
|
|
name: Mapped[str] = mapped_column(String(80))
|
|
|
|
|
notes: Mapped[str | None] = mapped_column(Text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CountryForeignKey:
|
|
|
|
|
|
|
|
|
|
country_id: Mapped[int] = mapped_column(ForeignKey("country.id"))
|
|
|
|
|
|
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
|
@declared_attr
|
|
|
|
|
def country(cls) -> Mapped["Country"]:
|
|
|
|
|
return bidirectional_relationship(cls, Country)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@event.listens_for(Country.__table__, "after_create")
|
|
|
|
|
def initialize_country(target, connection, **kwargs):
|
|
|
|
|
with Session(connection) as session:
|
2025-08-31 07:53:17 +02:00
|
|
|
qsys = session.scalar(select(User).where(User.code == "QSYS"))
|
2025-08-29 18:19:46 +02:00
|
|
|
for kwargs in (
|
2025-08-31 07:53:17 +02:00
|
|
|
dict(code="DE", name="Germany", status_id='A'),
|
|
|
|
|
dict(code="IT", name="Italy", status_id='A'),
|
|
|
|
|
dict(code="US", name="United States"),
|
|
|
|
|
dict(code="CA", name="Canada"),
|
|
|
|
|
dict(code="MX", name="Mexico"),
|
|
|
|
|
dict(code="ES", name="Spain", status_id='A'),
|
2025-08-29 18:19:46 +02:00
|
|
|
):
|
|
|
|
|
kwargs['_user__'] = qsys
|
|
|
|
|
session.add(Country(**kwargs))
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocationCode(CountryForeignKey, ContactForeignKey, StatusForeignKey, Versioned, Base):
|
|
|
|
|
"""Location Code"""
|
|
|
|
|
|
|
|
|
|
code: Mapped[str] = mapped_column(String(8), unique=True)
|
|
|
|
|
description: Mapped[str] = mapped_column(String(256))
|
|
|
|
|
notes: Mapped[str | None] = mapped_column(Text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocationCodeForeignKey:
|
|
|
|
|
"""Foreign Key Mixin for :py:class:`LocationCode`"""
|
|
|
|
|
|
|
|
|
|
location_code_id: Mapped[int] = mapped_column(ForeignKey("location_code.id"))
|
|
|
|
|
|
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
|
@declared_attr
|
|
|
|
|
def location_code(cls) -> Mapped["LocationCode"]:
|
|
|
|
|
return bidirectional_relationship(cls, LocationCode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Location(LocationCodeForeignKey, ContactForeignKey, StatusForeignKey, Versioned, Base):
|
|
|
|
|
"""Location"""
|
|
|
|
|
|
2025-08-31 07:53:17 +02:00
|
|
|
code: Mapped[str] = mapped_column(String(30))
|
2025-08-29 18:19:46 +02:00
|
|
|
description: Mapped[str] = mapped_column(String(256))
|
|
|
|
|
notes: Mapped[str | None] = mapped_column(Text)
|
|
|
|
|
|
|
|
|
|
__table_args__ = (
|
2025-08-31 07:53:17 +02:00
|
|
|
UniqueConstraint('location_code_id', code),
|
2025-08-29 18:19:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocationForeignKey:
|
|
|
|
|
"""Foreign Key Mixin for :py:class:`Location`"""
|
|
|
|
|
|
|
|
|
|
location_id: Mapped[int | None] = mapped_column(ForeignKey("location.id"))
|
|
|
|
|
|
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
|
@declared_attr
|
|
|
|
|
def location(cls) -> Mapped["Location"]:
|
|
|
|
|
return bidirectional_relationship(cls, Location)
|