66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
# app/platform/users/models.py
|
|
"""User and UserAuth ORM models."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, DateTime, ForeignKey, String, func
|
|
from sqlalchemy import Enum as SAEnum
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.core.database import Base
|
|
from app.platform.users.enums import UserStatus
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
|
first_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
last_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
email: Mapped[str] = mapped_column(
|
|
String(255), unique=True, index=True, nullable=False
|
|
)
|
|
|
|
status: Mapped[UserStatus] = mapped_column(
|
|
SAEnum(UserStatus, native_enum=False, length=32),
|
|
default=UserStatus.ACTIVE,
|
|
nullable=False,
|
|
)
|
|
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
|
)
|
|
last_login_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
|
|
auth: Mapped["UserAuth"] = relationship(
|
|
back_populates="user", uselist=False, cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class UserAuth(Base):
|
|
__tablename__ = "user_auth"
|
|
|
|
# Shared primary key pattern: user_id is both the PK and the FK. This
|
|
# makes a second UserAuth row for the same user structurally
|
|
# impossible, rather than something you'd enforce with a separate
|
|
# unique constraint on a regular FK column.
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
hashed_password: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
must_change_password: Mapped[bool] = mapped_column(
|
|
Boolean, default=False, nullable=False
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
|
)
|
|
|
|
user: Mapped["User"] = relationship(back_populates="auth")
|