This commit is contained in:
xds
2026-02-03 16:51:23 +03:00
parent e43cd575b0
commit 30daa1340a
3 changed files with 25 additions and 3 deletions

View File

@@ -1,13 +1,15 @@
from datetime import datetime, UTC
from enum import Enum
from typing import Optional
from typing import Optional, Any
from pydantic import BaseModel, computed_field
from pydantic import BaseModel
class AssetType(str, Enum):
IMAGE = 'image'
PROMPT = 'prompt'
class Asset(BaseModel):
id: Optional[str] = None
name: str
@@ -17,3 +19,13 @@ class Asset(BaseModel):
tg_doc_file_id: str
tg_photo_file_id: Optional[str] = None
created_at: datetime = datetime.now(UTC)
# --- CALCULATED FIELD ---
@computed_field
def link(self) -> str:
"""
Это поле автоматически вычислится и попадет в model_dump() / .json()
"""
if self.id:
return f"/api/assets/{self.id}"
return ""

View File

@@ -1,11 +1,13 @@
from typing import Optional
from pydantic import BaseModel
from pydantic_core.core_schema import computed_field
class Character(BaseModel):
id: str | None
name: str
avatar_image: Optional[str] = None
character_image_data: Optional[bytes] = None
character_image_doc_tg_id: str
character_image_tg_id: str | None

View File

@@ -7,6 +7,7 @@ from aiogram.fsm.state import State, StatesGroup
from aiogram.types import *
from aiogram import Router, F, Bot
from models.Asset import Asset, AssetType
from models.Character import Character
from repos.dao import DAO
@@ -53,6 +54,7 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot):
file_io = await bot.download(file_id)
# photo_bytes = file_io.getvalue() # Получаем байты
# Создаем модель
char = Character(
id=None,
@@ -65,9 +67,14 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot):
file_io.close()
# Сохраняем через DAO
await dao.chars.add_character(char)
file_info = await bot.get_file(char.character_image_doc_tg_id)
file_bytes = await bot.download_file(file_info.file_path)
avatar_asset = await dao.assets.save_asset(
Asset(name="avatar.png", type=AssetType.IMAGE, linked_char_id=char.id, data=file_bytes,
tg_doc_file_id=message.document.file_id))
char.avatar_image = avatar_asset.link
# Отправляем подтверждение
# Используем байты для отправки обратно
photo_msg = await message.answer_photo(
@@ -80,6 +87,7 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot):
)
)
char.character_image_tg_id = photo_msg.photo[0].file_id
await dao.chars.update_char(char.id, char)
await wait_msg.delete()