Spaces:
Runtime error
Runtime error
File size: 2,724 Bytes
18ade12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
User model for the Todo application.
Represents an application user. Foundation for authentication (handled by Better Auth).
Per data-model.md specification.
"""
import uuid
from datetime import datetime, timezone
from typing import TYPE_CHECKING, List, Optional
from sqlalchemy import Column, Index, String
from sqlmodel import Field, Relationship, SQLModel
if TYPE_CHECKING:
from .category import Category
from .todo import Todo
class User(SQLModel, table=True):
"""
User entity representing an application user.
Attributes:
id: UUID primary key
email: Unique email address (max 255 chars)
display_name: User's display name (max 100 chars)
created_at: Timestamp when record was created
updated_at: Timestamp when record was last updated
"""
__tablename__ = "users"
# Primary key
id: uuid.UUID = Field(
default_factory=uuid.uuid4,
primary_key=True,
description="Unique identifier"
)
# Required fields
email: str = Field(
sa_column=Column(String(255), unique=True, nullable=False, index=True),
description="User's email address"
)
display_name: str = Field(
sa_column=Column(String(100), nullable=False),
description="User's display name"
)
# Timestamps
created_at: datetime = Field(
default_factory=lambda: datetime.now(timezone.utc),
description="Record creation timestamp"
)
updated_at: datetime = Field(
default_factory=lambda: datetime.now(timezone.utc),
sa_column_kwargs={"onupdate": lambda: datetime.now(timezone.utc)},
description="Last update timestamp"
)
# Relationships
categories: List["Category"] = Relationship(
back_populates="user",
sa_relationship_kwargs={"cascade": "all, delete-orphan"}
)
todos: List["Todo"] = Relationship(
back_populates="user",
sa_relationship_kwargs={"cascade": "all, delete-orphan"}
)
# Table indexes
__table_args__ = (
Index("idx_users_email", "email", unique=True),
)
class UserCreate(SQLModel):
"""Schema for creating a new user."""
email: str = Field(max_length=255, description="User's email address")
display_name: str = Field(max_length=100, description="User's display name")
class UserRead(SQLModel):
"""Schema for reading user data."""
id: uuid.UUID
email: str
display_name: str
created_at: datetime
updated_at: datetime
class UserUpdate(SQLModel):
"""Schema for updating user data."""
email: Optional[str] = Field(default=None, max_length=255)
display_name: Optional[str] = Field(default=None, max_length=100)
|