45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
from pydantic import BaseModel, Field
|
||
from typing import Optional, Generic, TypeVar, List
|
||
from datetime import datetime
|
||
|
||
T = TypeVar('T')
|
||
|
||
|
||
class BaseSchema(BaseModel):
|
||
"""Базовый класс для всех схем"""
|
||
|
||
class Config:
|
||
from_attributes = True
|
||
json_encoders = {
|
||
datetime: lambda v: v.isoformat()
|
||
}
|
||
|
||
|
||
class PaginationParams(BaseModel):
|
||
"""Параметры пагинации"""
|
||
page: int = Field(1, ge=1, description="Номер страницы")
|
||
per_page: int = Field(20, ge=1, le=100, description="Записей на странице")
|
||
order_by: Optional[str] = Field(None, description="Поле для сортировки")
|
||
descending: bool = Field(True, description="По убыванию")
|
||
|
||
|
||
class PaginatedResponse(BaseSchema, Generic[T]):
|
||
"""Ответ с пагинацией"""
|
||
items: List[T]
|
||
total: int
|
||
page: int
|
||
per_page: int
|
||
pages: int
|
||
|
||
|
||
class MessageResponse(BaseSchema):
|
||
"""Простой ответ с сообщением"""
|
||
message: str
|
||
success: bool = True
|
||
|
||
|
||
class ErrorResponse(BaseSchema):
|
||
"""Ответ с ошибкой"""
|
||
error: str
|
||
detail: Optional[str] = None
|
||
status_code: int |