This commit is contained in:
Bernhard Radermacher (hakisto)
2025-08-31 18:42:46 +02:00
parent bd510016de
commit eb1d8d793c
6 changed files with 356 additions and 10 deletions

View File

@@ -1,3 +1,5 @@
from pydoc import describe
from sqlalchemy import String, Text, ForeignKey, event, select, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, declared_attr, Session
@@ -17,6 +19,8 @@ class Country(StatusForeignKey, Versioned, Base):
name: Mapped[str] = mapped_column(String(80))
notes: Mapped[str | None] = mapped_column(Text)
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})"
class CountryForeignKey:
@@ -50,9 +54,12 @@ class LocationCode(CountryForeignKey, ContactForeignKey, StatusForeignKey, Versi
"""Location Code"""
code: Mapped[str] = mapped_column(String(8), unique=True)
description: Mapped[str] = mapped_column(String(256))
description: Mapped[str | None] = mapped_column(String(80))
notes: Mapped[str | None] = mapped_column(Text)
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})"
class LocationCodeForeignKey:
"""Foreign Key Mixin for :py:class:`LocationCode`"""
@@ -65,17 +72,35 @@ class LocationCodeForeignKey:
return bidirectional_relationship(cls, LocationCode)
# 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()
class Location(LocationCodeForeignKey, ContactForeignKey, StatusForeignKey, Versioned, Base):
"""Location"""
code: Mapped[str] = mapped_column(String(30))
description: Mapped[str] = mapped_column(String(256))
description: Mapped[str | None] = mapped_column(String(80))
notes: Mapped[str | None] = mapped_column(Text)
__table_args__ = (
UniqueConstraint('location_code_id', code),
)
def __repr__(self):
return f"Location(id={self.id!r}, code={self.code!r}, description={self.description!r}, notes={self.notes!r})"
class LocationForeignKey:
"""Foreign Key Mixin for :py:class:`Location`"""
@@ -86,3 +111,17 @@ class LocationForeignKey:
@declared_attr
def location(cls) -> Mapped["Location"]:
return bidirectional_relationship(cls, Location)
# 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()