Files
bot-telegram/api/schemas.py
2025-09-14 22:09:39 +05:00

123 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Pydantic схемы для валидации данных API
"""
from pydantic import BaseModel, Field, ConfigDict
from typing import Optional
from datetime import datetime
# Базовые схемы для пользователей
class UserBase(BaseModel):
"""Базовая схема пользователя"""
telegram_id: int = Field(..., description="Telegram ID пользователя")
username: Optional[str] = Field(None, max_length=255, description="Username в Telegram")
first_name: Optional[str] = Field(None, max_length=255, description="Имя пользователя")
last_name: Optional[str] = Field(None, max_length=255, description="Фамилия пользователя")
language_code: str = Field(default="ru", max_length=10, description="Код языка")
class UserCreate(UserBase):
"""Схема для создания пользователя"""
pass
class UserUpdate(BaseModel):
"""Схема для обновления пользователя"""
username: Optional[str] = Field(None, max_length=255)
first_name: Optional[str] = Field(None, max_length=255)
last_name: Optional[str] = Field(None, max_length=255)
language_code: Optional[str] = Field(None, max_length=10)
is_active: Optional[bool] = None
class UserResponse(UserBase):
"""Схема ответа с данными пользователя"""
model_config = ConfigDict(from_attributes=True)
id: int
is_active: bool
is_admin: bool
created_at: datetime
updated_at: Optional[datetime] = None
# Базовые схемы для сообщений
class MessageBase(BaseModel):
"""Базовая схема сообщения"""
telegram_message_id: int = Field(..., description="ID сообщения в Telegram")
user_id: int = Field(..., description="ID пользователя в нашей БД")
text: Optional[str] = Field(None, description="Текст сообщения")
message_type: str = Field(default="text", max_length=50, description="Тип сообщения")
class MessageCreate(MessageBase):
"""Схема для создания сообщения"""
pass
class MessageResponse(MessageBase):
"""Схема ответа с данными сообщения"""
model_config = ConfigDict(from_attributes=True)
id: int
created_at: datetime
# Базовые схемы для настроек бота
class BotSettingBase(BaseModel):
"""Базовая схема настройки бота"""
key: str = Field(..., max_length=255, description="Ключ настройки")
value: Optional[str] = Field(None, description="Значение настройки")
description: Optional[str] = Field(None, description="Описание настройки")
class BotSettingCreate(BotSettingBase):
"""Схема для создания настройки"""
pass
class BotSettingUpdate(BaseModel):
"""Схема для обновления настройки"""
value: Optional[str] = None
description: Optional[str] = None
class BotSettingResponse(BotSettingBase):
"""Схема ответа с данными настройки"""
model_config = ConfigDict(from_attributes=True)
id: int
created_at: datetime
updated_at: Optional[datetime] = None
# Схемы для общих ответов API
class MessageResponseModel(BaseModel):
"""Общая схема ответа с сообщением"""
message: str
status: str = "success"
class ErrorResponse(BaseModel):
"""Схема ответа с ошибкой"""
detail: str
status: str = "error"
class HealthCheckResponse(BaseModel):
"""Схема ответа проверки здоровья"""
status: str
database: str
class RootResponse(BaseModel):
"""Схема корневого ответа"""
message: str
version: str
status: str
# Схемы для пагинации
class PaginationParams(BaseModel):
"""Параметры пагинации"""
limit: int = Field(default=100, ge=1, le=1000, description="Количество элементов на странице")
offset: int = Field(default=0, ge=0, description="Смещение")
class PaginatedResponse(BaseModel):
"""Схема ответа с пагинацией"""
items: list
total: int
limit: int
offset: int
has_next: bool
has_prev: bool