+ api
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
from datetime import datetime, UTC
|
from datetime import datetime, UTC
|
||||||
from enum import Enum
|
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):
|
class AssetType(str, Enum):
|
||||||
IMAGE = 'image'
|
IMAGE = 'image'
|
||||||
PROMPT = 'prompt'
|
PROMPT = 'prompt'
|
||||||
|
|
||||||
|
|
||||||
class Asset(BaseModel):
|
class Asset(BaseModel):
|
||||||
id: Optional[str] = None
|
id: Optional[str] = None
|
||||||
name: str
|
name: str
|
||||||
@@ -17,3 +19,13 @@ class Asset(BaseModel):
|
|||||||
tg_doc_file_id: str
|
tg_doc_file_id: str
|
||||||
tg_photo_file_id: Optional[str] = None
|
tg_photo_file_id: Optional[str] = None
|
||||||
created_at: datetime = datetime.now(UTC)
|
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 typing import Optional
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from pydantic_core.core_schema import computed_field
|
||||||
|
|
||||||
|
|
||||||
class Character(BaseModel):
|
class Character(BaseModel):
|
||||||
id: str | None
|
id: str | None
|
||||||
name: str
|
name: str
|
||||||
|
avatar_image: Optional[str] = None
|
||||||
character_image_data: Optional[bytes] = None
|
character_image_data: Optional[bytes] = None
|
||||||
character_image_doc_tg_id: str
|
character_image_doc_tg_id: str
|
||||||
character_image_tg_id: str | None
|
character_image_tg_id: str | None
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from aiogram.fsm.state import State, StatesGroup
|
|||||||
from aiogram.types import *
|
from aiogram.types import *
|
||||||
from aiogram import Router, F, Bot
|
from aiogram import Router, F, Bot
|
||||||
|
|
||||||
|
from models.Asset import Asset, AssetType
|
||||||
from models.Character import Character
|
from models.Character import Character
|
||||||
from repos.dao import DAO
|
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)
|
file_io = await bot.download(file_id)
|
||||||
# photo_bytes = file_io.getvalue() # Получаем байты
|
# photo_bytes = file_io.getvalue() # Получаем байты
|
||||||
|
|
||||||
|
|
||||||
# Создаем модель
|
# Создаем модель
|
||||||
char = Character(
|
char = Character(
|
||||||
id=None,
|
id=None,
|
||||||
@@ -65,9 +67,14 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot):
|
|||||||
file_io.close()
|
file_io.close()
|
||||||
|
|
||||||
# Сохраняем через DAO
|
# Сохраняем через DAO
|
||||||
|
|
||||||
await dao.chars.add_character(char)
|
await dao.chars.add_character(char)
|
||||||
file_info = await bot.get_file(char.character_image_doc_tg_id)
|
file_info = await bot.get_file(char.character_image_doc_tg_id)
|
||||||
file_bytes = await bot.download_file(file_info.file_path)
|
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_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
|
char.character_image_tg_id = photo_msg.photo[0].file_id
|
||||||
|
|
||||||
await dao.chars.update_char(char.id, char)
|
await dao.chars.update_char(char.id, char)
|
||||||
await wait_msg.delete()
|
await wait_msg.delete()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user