Files
yellow-bank-soal/app/models/user.py
Dwindi Ramadhana cf193d7ea0 first commit
2026-03-21 23:32:59 +07:00

73 lines
2.1 KiB
Python

"""
User model for WordPress user integration.
Represents users from WordPress that can take tryouts.
"""
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Index, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class User(Base):
"""
User model representing WordPress users.
Attributes:
id: Primary key
wp_user_id: WordPress user ID (unique per site)
website_id: Website identifier (for multi-site support)
created_at: Record creation timestamp
updated_at: Record update timestamp
sessions: User's tryout sessions
"""
__tablename__ = "users"
# Primary key
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
# WordPress user ID (unique within website context)
wp_user_id: Mapped[int] = mapped_column(
String(255), nullable=False, index=True, comment="WordPress user ID"
)
# Website identifier (for multi-site support)
website_id: Mapped[int] = mapped_column(
ForeignKey("websites.id", ondelete="CASCADE", onupdate="CASCADE"),
nullable=False,
index=True,
comment="Website identifier",
)
# Timestamps
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default="NOW()"
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
server_default="NOW()",
onupdate="NOW()",
)
# Relationships
website: Mapped["Website"] = relationship(
"Website", back_populates="users", lazy="selectin"
)
sessions: Mapped[list["Session"]] = relationship(
"Session", back_populates="user", lazy="selectin", cascade="all, delete-orphan"
)
# Indexes
__table_args__ = (
Index("ix_users_wp_user_id_website_id", "wp_user_id", "website_id", unique=True),
Index("ix_users_website_id", "website_id"),
)
def __repr__(self) -> str:
return f"<User(wp_user_id={self.wp_user_id}, website_id={self.website_id})>"