+ api
This commit is contained in:
@@ -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 ""
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,14 +67,19 @@ 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(
|
||||
photo=BufferedInputFile(file_bytes.read(),
|
||||
filename="char.jpg") if not char.character_image_tg_id else char.character_image_tg_id,
|
||||
filename="char.jpg") if not char.character_image_tg_id else char.character_image_tg_id,
|
||||
caption=(
|
||||
"🎉 <b>Персонаж создан!</b>\n\n"
|
||||
f"👤 <b>Имя:</b> {char.name}\n"
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user