users and status working
This commit is contained in:
88
app/alchemy/location.py
Normal file
88
app/alchemy/location.py
Normal file
@@ -0,0 +1,88 @@
|
||||
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):
|
||||
|
||||
iso: Mapped[str] = mapped_column(String(2), unique=True)
|
||||
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:
|
||||
qsys = session.scalar(select(User).where(User.username == "QSYS"))
|
||||
for kwargs in (
|
||||
dict(iso="DE", name="Germany", status_id='A'),
|
||||
dict(iso="IT", name="Italy", status_id='A'),
|
||||
dict(iso="US", name="United States"),
|
||||
dict(iso="CA", name="Canada"),
|
||||
dict(iso="MX", name="Mexico"),
|
||||
dict(iso="ES", name="Spain", status_id='A'),
|
||||
):
|
||||
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"""
|
||||
|
||||
location: Mapped[str] = mapped_column(String(30))
|
||||
description: Mapped[str] = mapped_column(String(256))
|
||||
notes: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint('location_code_id', location),
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user