Initial commit
This commit is contained in:
18
backend/app/schemas/__init__.py
Normal file
18
backend/app/schemas/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Schemas package - exports all Pydantic schemas."""
|
||||
|
||||
from app.schemas.user import User, UserCreate, UserUpdate, UserInDB
|
||||
from app.schemas.auth import Token, TokenData, LoginRequest, RegisterRequest
|
||||
from app.schemas.settings import Setting, SettingUpdate
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
"UserCreate",
|
||||
"UserUpdate",
|
||||
"UserInDB",
|
||||
"Token",
|
||||
"TokenData",
|
||||
"LoginRequest",
|
||||
"RegisterRequest",
|
||||
"Setting",
|
||||
"SettingUpdate",
|
||||
]
|
||||
41
backend/app/schemas/auth.py
Normal file
41
backend/app/schemas/auth.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""Pydantic schemas for authentication requests/responses."""
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field, EmailStr
|
||||
|
||||
|
||||
class Token(BaseModel):
|
||||
"""JWT token response schema."""
|
||||
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
|
||||
|
||||
class TokenData(BaseModel):
|
||||
"""Token payload data schema."""
|
||||
|
||||
user_id: Optional[str] = None
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
"""Login request schema."""
|
||||
|
||||
username: str = Field(..., min_length=3, max_length=100)
|
||||
password: str = Field(..., min_length=1)
|
||||
|
||||
|
||||
class RegisterRequest(BaseModel):
|
||||
"""Registration request schema."""
|
||||
|
||||
username: str = Field(..., min_length=3, max_length=100)
|
||||
email: EmailStr = Field(..., description="Valid email address")
|
||||
password: str = Field(..., min_length=8, description="Password must be at least 8 characters")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"username": "johndoe",
|
||||
"email": "john@example.com",
|
||||
"password": "securepassword123"
|
||||
}
|
||||
}
|
||||
40
backend/app/schemas/common.py
Normal file
40
backend/app/schemas/common.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Common Pydantic schemas used across the application."""
|
||||
|
||||
from typing import Optional, Generic, TypeVar, List
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
DataT = TypeVar('DataT')
|
||||
|
||||
|
||||
class PaginatedResponse(BaseModel, Generic[DataT]):
|
||||
"""Generic paginated response schema."""
|
||||
|
||||
items: List[DataT]
|
||||
total: int
|
||||
page: int
|
||||
limit: int
|
||||
pages: int
|
||||
|
||||
|
||||
class MessageResponse(BaseModel):
|
||||
"""Simple message response schema."""
|
||||
|
||||
message: str
|
||||
detail: Optional[str] = None
|
||||
|
||||
|
||||
class ErrorResponse(BaseModel):
|
||||
"""Error response schema."""
|
||||
|
||||
error: str
|
||||
detail: Optional[str] = None
|
||||
status_code: int
|
||||
|
||||
|
||||
class TokenResponse(BaseModel):
|
||||
"""JWT token response schema."""
|
||||
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
expires_in: int # seconds
|
||||
35
backend/app/schemas/settings.py
Normal file
35
backend/app/schemas/settings.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""Settings schemas."""
|
||||
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Any
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class SettingBase(BaseModel):
|
||||
"""Base setting schema."""
|
||||
key: str
|
||||
value: Any
|
||||
|
||||
|
||||
class SettingUpdate(BaseModel):
|
||||
"""Schema for updating a setting."""
|
||||
value: Any
|
||||
|
||||
|
||||
class Setting(BaseModel):
|
||||
"""Setting schema for responses."""
|
||||
key: str
|
||||
value: Any
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@classmethod
|
||||
def from_orm(cls, obj):
|
||||
"""Custom from_orm to handle value extraction."""
|
||||
return cls(
|
||||
key=obj.key,
|
||||
value=obj.get_value(),
|
||||
updated_at=obj.updated_at
|
||||
)
|
||||
61
backend/app/schemas/user.py
Normal file
61
backend/app/schemas/user.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user