Files
vpsx-fast/app/alchemy/location.py

128 lines
4.6 KiB
Python
Raw Normal View History

2025-08-31 18:42:46 +02:00
from pydoc import describe
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)
2025-08-31 18:42:46 +02:00
def __repr__(self):
return f"Country(id={self.id!r}, code={self.code!r}, name={self.name!r}, notes={self.notes!r}, location_codes={self.location_codes!r})"
2025-08-29 18:19:46 +02:00
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)
2025-08-31 18:42:46 +02:00
description: Mapped[str | None] = mapped_column(String(80))
2025-08-29 18:19:46 +02:00
notes: Mapped[str | None] = mapped_column(Text)
2025-08-31 18:42:46 +02:00
def __repr__(self):
return f"LocationCode(id={self.id!r}, code={self.code!r}, description={self.description!r}, notes={self.notes!r}, locations={self.locations!r})"
2025-08-29 18:19:46 +02:00
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)
2025-08-31 18:42:46 +02:00
# noinspection PyUnusedLocal
@event.listens_for(LocationCode.__table__, "after_create")
def initialize_location_code(target, connection, **kwargs):
with Session(connection) as session:
qsys = session.scalar(select(User).where(User.code == "QSYS"))
de = session.scalar(select(Country).where(Country.code == "DE"))
for kwargs in (
dict(country=de, code="DEHAM", description="Hamburg", status_id='A'),
dict(country=de, code="DEFRA", description="Frankfurt/Main", status_id='A'),
):
kwargs['_user__'] = qsys
session.add(LocationCode(**kwargs))
session.commit()
2025-08-29 18:19:46 +02:00
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-31 18:42:46 +02:00
description: Mapped[str | None] = mapped_column(String(80))
2025-08-29 18:19:46 +02:00
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
)
2025-08-31 18:42:46 +02:00
def __repr__(self):
return f"Location(id={self.id!r}, code={self.code!r}, description={self.description!r}, notes={self.notes!r})"
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)
2025-08-31 18:42:46 +02:00
# noinspection PyUnusedLocal
@event.listens_for(Location.__table__, "after_create")
def initialize_location(target, connection, **kwargs):
with Session(connection) as session:
qsys = session.scalar(select(User).where(User.code == "QSYS"))
deham = session.scalar(select(LocationCode).where(LocationCode.code == "DEHAM"))
for kwargs in (
dict(location_code=deham, code="Andy's Wohnung"),
):
kwargs['_user__'] = qsys
session.add(Location(**kwargs))
session.commit()