Spaces:
Sleeping
Sleeping
| """User model for authentication and authorization | |
| Represents an authenticated reader with account credentials. | |
| Aligns with data-model.md User entity specification. | |
| """ | |
| from datetime import datetime | |
| from uuid import UUID, uuid4 | |
| from sqlalchemy import Column, String, TIMESTAMP | |
| from sqlalchemy.dialects.postgresql import UUID as PGUUID | |
| from sqlalchemy.orm import relationship | |
| from src.config.database import Base | |
| class User(Base): | |
| """User model for authentication | |
| Attributes: | |
| id: Unique user identifier (UUID) | |
| email: User's email address (unique) | |
| password_hash: Hashed password (managed by auth service) | |
| created_at: Account creation timestamp | |
| updated_at: Last account modification timestamp | |
| """ | |
| __tablename__ = "users" | |
| id = Column( | |
| PGUUID(as_uuid=True), | |
| primary_key=True, | |
| default=uuid4, | |
| server_default="gen_random_uuid()" | |
| ) | |
| email = Column(String(255), unique=True, nullable=False, index=True) | |
| password_hash = Column(String(255), nullable=False) | |
| created_at = Column(TIMESTAMP, nullable=False, default=datetime.utcnow, server_default="NOW()") | |
| updated_at = Column(TIMESTAMP, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow, server_default="NOW()") | |
| # Relationships | |
| sessions = relationship("Session", back_populates="user", cascade="all, delete-orphan") | |
| chat_messages = relationship("ChatMessage", back_populates="user", cascade="all, delete-orphan") | |
| def __repr__(self) -> str: | |
| return f"<User(id={self.id}, email={self.email})>" | |