From 16e9d7f12400f56ee1092ef3043345f26cd5a77a Mon Sep 17 00:00:00 2001 From: kolo Date: Wed, 15 Jul 2026 01:02:54 +0300 Subject: [PATCH] send photo/media group with detail text as caption, not separately --- .../bot/admin_dialogs/residents_management.py | 17 +++++++++++-- .../bot/admin_dialogs/rooms_management.py | 17 +++++++++++-- .../creator_dialogs/transactions_history.py | 24 +++++++++++++++++-- .../bot/user_dialogs/user_menu/history.py | 17 +++++++++++-- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/dutylog/application/bot/admin_dialogs/residents_management.py b/src/dutylog/application/bot/admin_dialogs/residents_management.py index f02c46e..1840170 100644 --- a/src/dutylog/application/bot/admin_dialogs/residents_management.py +++ b/src/dutylog/application/bot/admin_dialogs/residents_management.py @@ -627,18 +627,31 @@ async def on_transaction_selected( dialog_manager: DialogManager, item_id: str, transactions_repository: FromDishka[HoursTransactionsRepository], + users_repository: FromDishka[UsersRepository], ): dialog_manager.dialog_data["selected_transaction_id"] = int(item_id) tx = await transactions_repository.get_transaction_by_id(int(item_id)) if tx: photo_file_ids = HoursTransactionsRepository.get_photo_file_ids(tx) if photo_file_ids: + operation = "Начислено" if tx.transaction_type == "increase" else "Списано" + emoji = "+" if tx.transaction_type == "increase" else "−" + msk_time = tx.created_at.astimezone(msk_now().tzinfo).replace(tzinfo=None) + date_str = msk_time.strftime("%d.%m.%Y %H:%M") + remark_text = f"\n💬 {tx.remark}" if tx.remark else "" + admin_info = "" + if tx.admin_id: + admin = await users_repository.get_user_by_id(tx.admin_id) + if admin: + admin_name = f"@{admin.username}" if admin.username else admin.first_name or f"ID: {admin.id}" + admin_info = f"\n👨‍💼 Администратор: {admin_name}" + caption = f"📋 Детали транзакции\n\n⚙️ Операция: {operation}\n🔢 Количество: {emoji}{tx.amount} ч\n📅 Дата: {date_str}{remark_text}{admin_info}" bot: Bot = dialog_manager.middleware_data.get("bot") try: if len(photo_file_ids) == 1: - await bot.send_photo(callback.message.chat.id, photo_file_ids[0]) + await bot.send_photo(callback.message.chat.id, photo_file_ids[0], caption=caption) else: - media = [InputMediaPhoto(media=fid) for fid in photo_file_ids] + media = [InputMediaPhoto(media=fid, caption=caption if i == 0 else None) for i, fid in enumerate(photo_file_ids)] await bot.send_media_group(callback.message.chat.id, media=media) except Exception: pass diff --git a/src/dutylog/application/bot/admin_dialogs/rooms_management.py b/src/dutylog/application/bot/admin_dialogs/rooms_management.py index e203f62..b89914f 100644 --- a/src/dutylog/application/bot/admin_dialogs/rooms_management.py +++ b/src/dutylog/application/bot/admin_dialogs/rooms_management.py @@ -508,18 +508,31 @@ async def on_room_transaction_selected( dialog_manager: DialogManager, item_id: str, transactions_repository: FromDishka[HoursTransactionsRepository], + users_repository: FromDishka[UsersRepository], ): dialog_manager.dialog_data["selected_transaction_id"] = int(item_id) tx = await transactions_repository.get_transaction_by_id(int(item_id)) if tx: photo_file_ids = HoursTransactionsRepository.get_photo_file_ids(tx) if photo_file_ids: + operation = "Начислено" if tx.transaction_type == "increase" else "Списано" + emoji = "+" if tx.transaction_type == "increase" else "−" + msk_time = tx.created_at.astimezone(msk_now().tzinfo).replace(tzinfo=None) + date_str = msk_time.strftime("%d.%m.%Y %H:%M") + remark_text = f"\n💬 {tx.remark}" if tx.remark else "" + admin_info = "" + if tx.admin_id: + admin = await users_repository.get_user_by_id(tx.admin_id) + if admin: + admin_name = f"@{admin.username}" if admin.username else admin.first_name or f"ID: {admin.id}" + admin_info = f"\n👨‍💼 Администратор: {admin_name}" + caption = f"📋 Детали транзакции\n\n⚙️ Операция: {operation}\n🔢 Количество: {emoji}{tx.amount} ч\n📅 Дата: {date_str}{remark_text}{admin_info}" bot: Bot = dialog_manager.middleware_data.get("bot") try: if len(photo_file_ids) == 1: - await bot.send_photo(callback.message.chat.id, photo_file_ids[0]) + await bot.send_photo(callback.message.chat.id, photo_file_ids[0], caption=caption) else: - media = [InputMediaPhoto(media=fid) for fid in photo_file_ids] + media = [InputMediaPhoto(media=fid, caption=caption if i == 0 else None) for i, fid in enumerate(photo_file_ids)] await bot.send_media_group(callback.message.chat.id, media=media) except Exception: pass diff --git a/src/dutylog/application/bot/creator_dialogs/transactions_history.py b/src/dutylog/application/bot/creator_dialogs/transactions_history.py index f86acb0..ed49f42 100644 --- a/src/dutylog/application/bot/creator_dialogs/transactions_history.py +++ b/src/dutylog/application/bot/creator_dialogs/transactions_history.py @@ -75,18 +75,38 @@ async def on_creator_transaction_selected( dialog_manager, item_id: str, transactions_repository: FromDishka[HoursTransactionsRepository], + residents_repository: FromDishka[ResidentsRepository], + rooms_repository: FromDishka[RoomsRepository], + users_repository: FromDishka[UsersRepository], ): dialog_manager.dialog_data["selected_transaction_id"] = int(item_id) tx = await transactions_repository.get_transaction_by_id(int(item_id)) if tx: photo_file_ids = HoursTransactionsRepository.get_photo_file_ids(tx) if photo_file_ids: + operation = "Начислено" if tx.transaction_type == "increase" else "Списано" + emoji = "+" if tx.transaction_type == "increase" else "−" + room_mark = " 🚪" if tx.per_room else "" + msk_time = tx.created_at.astimezone(msk_now().tzinfo).replace(tzinfo=None) + date_str = msk_time.strftime("%d.%m.%Y %H:%M") + resident = await residents_repository.get_by_id(tx.resident_id) + resident_name = resident.real_name if resident and resident.real_name else "Без имени" + room = await rooms_repository.get_by_id(resident.room) if resident else None + room_number = room.number if room else "???" + admin_info = "" + if tx.admin_id: + admin = await users_repository.get_user_by_id(tx.admin_id) + if admin: + admin_name = f"@{admin.username}" if admin.username else admin.first_name or f"ID: {admin.id}" + admin_info = f"\n👨‍💼 Администратор: {admin_name}" + remark_text = f"\n💬 {tx.remark}" if tx.remark else "" + caption = f"📋 Детали транзакции\n\n⚙️ Операция: {operation}\n🔢 Количество: {emoji}{tx.amount} ч{room_mark}\n👤 Резидент: {resident_name} (к. {room_number}){admin_info}\n📅 Дата: {date_str}{remark_text}" bot: Bot = dialog_manager.middleware_data.get("bot") try: if len(photo_file_ids) == 1: - await bot.send_photo(callback.message.chat.id, photo_file_ids[0]) + await bot.send_photo(callback.message.chat.id, photo_file_ids[0], caption=caption) else: - media = [InputMediaPhoto(media=fid) for fid in photo_file_ids] + media = [InputMediaPhoto(media=fid, caption=caption if i == 0 else None) for i, fid in enumerate(photo_file_ids)] await bot.send_media_group(callback.message.chat.id, media=media) except Exception: pass diff --git a/src/dutylog/application/bot/user_dialogs/user_menu/history.py b/src/dutylog/application/bot/user_dialogs/user_menu/history.py index 278ed39..df5522b 100644 --- a/src/dutylog/application/bot/user_dialogs/user_menu/history.py +++ b/src/dutylog/application/bot/user_dialogs/user_menu/history.py @@ -69,18 +69,31 @@ async def on_user_transaction_selected( dialog_manager, item_id: str, transactions_repository: FromDishka[HoursTransactionsRepository], + users_repository: FromDishka[UsersRepository], ): dialog_manager.dialog_data["selected_transaction_id"] = int(item_id) tx = await transactions_repository.get_transaction_by_id(int(item_id)) if tx: photo_file_ids = HoursTransactionsRepository.get_photo_file_ids(tx) if photo_file_ids: + operation = "Начислено" if tx.transaction_type == "increase" else "Списано" + emoji = "+" if tx.transaction_type == "increase" else "−" + msk_time = tx.created_at.astimezone(msk_now().tzinfo).replace(tzinfo=None) + date_str = msk_time.strftime("%d.%m.%Y %H:%M") + remark_text = f"\n💬 {tx.remark}" if tx.remark else "" + admin_info = "" + if tx.admin_id: + admin = await users_repository.get_user_by_id(tx.admin_id) + if admin: + admin_name = f"@{admin.username}" if admin.username else admin.first_name or f"ID: {admin.id}" + admin_info = f"\n👨‍💼 Администратор: {admin_name}" + caption = f"📋 Детали транзакции\n\n⚙️ Операция: {operation}\n🔢 Количество: {emoji}{tx.amount} ч\n📅 Дата: {date_str}{remark_text}{admin_info}" bot: Bot = dialog_manager.middleware_data.get("bot") try: if len(photo_file_ids) == 1: - await bot.send_photo(callback.message.chat.id, photo_file_ids[0]) + await bot.send_photo(callback.message.chat.id, photo_file_ids[0], caption=caption) else: - media = [InputMediaPhoto(media=fid) for fid in photo_file_ids] + media = [InputMediaPhoto(media=fid, caption=caption if i == 0 else None) for i, fid in enumerate(photo_file_ids)] await bot.send_media_group(callback.message.chat.id, media=media) except Exception: pass