62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
"""Pydantic schemas for User API requests/responses."""
|
|
|
|
from datetime import datetime
|
|
from typing import Optional, Dict
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
|
|
|
|
# Shared properties
|
|
class UserBase(BaseModel):
|
|
"""Base user schema with common fields."""
|
|
|
|
username: Optional[str] = Field(None, min_length=3, max_length=100)
|
|
email: Optional[EmailStr] = None
|
|
is_active: Optional[bool] = True
|
|
is_superuser: Optional[bool] = False
|
|
|
|
|
|
# Properties to receive via API on creation
|
|
class UserCreate(UserBase):
|
|
"""Schema for user creation."""
|
|
|
|
username: str = Field(..., min_length=3, max_length=100)
|
|
email: EmailStr
|
|
password: str = Field(..., min_length=8)
|
|
permissions: Optional[Dict[str, bool]] = None
|
|
|
|
|
|
# Properties to receive via API on update
|
|
class UserUpdate(UserBase):
|
|
"""Schema for user update."""
|
|
|
|
password: Optional[str] = Field(None, min_length=8)
|
|
permissions: Optional[Dict[str, bool]] = None
|
|
|
|
|
|
# Properties shared by models stored in DB
|
|
class UserInDBBase(UserBase):
|
|
"""Schema for user in database (base)."""
|
|
|
|
id: str # UUID stored as string for SQLite compatibility
|
|
permissions: Dict[str, bool] = Field(default_factory=dict)
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
last_login: Optional[datetime] = None
|
|
|
|
class Config:
|
|
from_attributes = True # Pydantic v2 (was orm_mode in v1)
|
|
|
|
|
|
# Additional properties to return via API
|
|
class User(UserInDBBase):
|
|
"""Schema for user response."""
|
|
|
|
pass
|
|
|
|
|
|
# Additional properties stored in DB
|
|
class UserInDB(UserInDBBase):
|
|
"""Schema for user with hashed password."""
|
|
|
|
hashed_password: str
|