80 lines
2.2 KiB
Python
80 lines
2.2 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, UniqueConstraint, func
|
|
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[str] = 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,
|
|
comment="Website identifier",
|
|
)
|
|
|
|
# Timestamps
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=func.now(),
|
|
onupdate=func.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",
|
|
overlaps="sessions,tryout",
|
|
)
|
|
|
|
# Indexes
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"wp_user_id",
|
|
"website_id",
|
|
name="uq_users_wp_user_id_website_id",
|
|
),
|
|
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})>"
|