2025-08-29 18:19:46 +02:00
|
|
|
import sys
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import String, Text, ForeignKey, event, DateTime, select
|
|
|
|
|
from sqlalchemy.ext.compiler import compiles
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, declared_attr, relationship, Session
|
|
|
|
|
from sqlalchemy.sql import expression
|
|
|
|
|
|
|
|
|
|
from .base import Base
|
|
|
|
|
from .status import StatusForeignKey
|
|
|
|
|
|
|
|
|
|
__all__ = ['User', 'Versioned']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyPep8Naming
|
|
|
|
|
class utcnow(expression.FunctionElement):
|
|
|
|
|
type = DateTime()
|
|
|
|
|
inherit_cache = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@compiles(utcnow, "postgresql")
|
|
|
|
|
def pg_utcnow(*args, **kwargs):
|
|
|
|
|
return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@compiles(utcnow, "mssql")
|
|
|
|
|
def ms_utcnow(*args, **kwargs):
|
|
|
|
|
return "GETUTCDATE()"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@compiles(utcnow, "mysql")
|
|
|
|
|
def my_utcnow(*args, **kwargs):
|
|
|
|
|
return "UTC_TIMESTAMP(6)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@compiles(utcnow, "mariadb")
|
|
|
|
|
def maria_utcnow(*args, **kwargs):
|
|
|
|
|
return "UTC_TIMESTAMP(6)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@compiles(utcnow, "sqlite")
|
|
|
|
|
def sqlite_utcnow(*args, **kwargs):
|
|
|
|
|
return "strftime('%Y-%m-%d %H:%M:%S')"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Versioned:
|
|
|
|
|
|
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
|
@declared_attr
|
|
|
|
|
def __versioned__(cls):
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
_created__: Mapped[datetime] = mapped_column(server_default=utcnow(), sort_order=sys.maxsize, default=None)
|
|
|
|
|
_updated__: Mapped[datetime | None] = mapped_column(onupdate=utcnow(), sort_order=sys.maxsize)
|
|
|
|
|
_user__id: Mapped[int | None] = mapped_column(ForeignKey("user.id"), sort_order=sys.maxsize)
|
|
|
|
|
|
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
|
@declared_attr
|
|
|
|
|
def _user__(cls) -> Mapped["User"]:
|
|
|
|
|
return relationship()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class User(StatusForeignKey, Versioned, Base):
|
|
|
|
|
"""User"""
|
|
|
|
|
|
2025-08-31 07:53:17 +02:00
|
|
|
code: Mapped[str] = mapped_column(String(253), unique=True)
|
2025-08-29 18:19:46 +02:00
|
|
|
name: Mapped[str] = mapped_column(String(253))
|
|
|
|
|
password: Mapped[str | None] = mapped_column(String(255))
|
|
|
|
|
ldap_name: Mapped[str | None] = mapped_column(String(255))
|
|
|
|
|
notes: Mapped[str | None] = mapped_column(Text)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
2025-08-31 07:53:17 +02:00
|
|
|
return f'User(id={self.id!r}, code={self.code!r} name={self.name!r}, notes={self.notes!r})'
|
2025-08-29 18:19:46 +02:00
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
@event.listens_for(User.__table__, "after_create")
|
|
|
|
|
def initialize_user(target, connection, **kwargs):
|
|
|
|
|
from routers.user import get_password_hash
|
|
|
|
|
with Session(connection) as session:
|
2025-08-31 07:53:17 +02:00
|
|
|
qsys = User(code="QSYS", name="System User", notes="internal processing", status_id='X')
|
2025-08-29 18:19:46 +02:00
|
|
|
session.add(qsys)
|
|
|
|
|
session.commit()
|
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
|
|
|
qsys._user__id=qsys.id
|
|
|
|
|
session.commit()
|
|
|
|
|
for kwargs in (
|
2025-08-31 07:53:17 +02:00
|
|
|
dict(code="CTM", name="Control-M", password=get_password_hash("secret"), notes="user for automation"),
|
|
|
|
|
dict(code="exde37c8", name="Bernhard Radermacher",
|
2025-08-29 18:19:46 +02:00
|
|
|
password=get_password_hash("secret"),
|
|
|
|
|
ldap_name="a0061806@kiongroup.com",
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
kwargs.update(dict(status_id='A', _user__id=qsys.id))
|
|
|
|
|
session.add(User(**kwargs))
|
|
|
|
|
session.commit()
|