From e43cd575b00620da7dd57e61976a65c886748146 Mon Sep 17 00:00:00 2001 From: xds Date: Tue, 3 Feb 2026 16:21:15 +0300 Subject: [PATCH] + api --- models/Character.py | 3 +++ repos/char_repo.py | 10 ++++++---- routers/char_router.py | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/models/Character.py b/models/Character.py index 39a8c2c..a153043 100644 --- a/models/Character.py +++ b/models/Character.py @@ -1,9 +1,12 @@ +from typing import Optional + from pydantic import BaseModel class Character(BaseModel): id: str | None name: str + character_image_data: Optional[bytes] = None character_image_doc_tg_id: str character_image_tg_id: str | None character_bio: str diff --git a/repos/char_repo.py b/repos/char_repo.py index 01d7cf5..8c17531 100644 --- a/repos/char_repo.py +++ b/repos/char_repo.py @@ -15,8 +15,11 @@ class CharacterRepo: character.id = op.inserted_id return character - async def get_character(self, character_id: str) -> Character | None: - res = await self.collection.find_one({"_id": ObjectId(character_id)}) + async def get_character(self, character_id: str, with_image_data: bool = False) -> Character | None: + args = {} + if not with_image_data: + args["character_image_data"] = 0 + res = await self.collection.find_one({"_id": ObjectId(character_id)}, args) if res is None: return None else: @@ -24,7 +27,7 @@ class CharacterRepo: return Character(**res) async def get_all_characters(self) -> List[Character]: - docs = await self.collection.find().to_list(None) + docs = await self.collection.find({}, {"character_image_data": 0}).to_list(None) characters = [] for doc in docs: @@ -38,4 +41,3 @@ class CharacterRepo: async def update_char(self, char_id: str, character: Character) -> None: await self.collection.update_one({"_id": ObjectId(char_id)}, {"$set": character.model_dump()}) - diff --git a/routers/char_router.py b/routers/char_router.py index fe39004..3b52997 100644 --- a/routers/char_router.py +++ b/routers/char_router.py @@ -50,18 +50,19 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot): try: # ВОТ ТУТ скачиваем файл (прямо перед сохранением) - # file_io = await bot.download(file_id) + file_io = await bot.download(file_id) # photo_bytes = file_io.getvalue() # Получаем байты # Создаем модель char = Character( id=None, name=name, - # character_image=photo_bytes, + character_image_data=file_io.read(), character_image_tg_id=None, character_image_doc_tg_id=file_id, character_bio=bio ) + file_io.close() # Сохраняем через DAO await dao.chars.add_character(char)