shopbot/bot/services/history.py

71 lines
2.6 KiB
Python
Raw Normal View History

2024-12-24 18:08:03 +00:00
import logging
from aiogram_dialog import DialogManager
from aiogram.types import CallbackQuery
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from bot.database.db import async_session
from bot.database.models import Account
from bot.states import PurchaseHistorySG
ACCOUNT_INFO_TEMPLATE = (
"<b>Подробная информация об аккаунте:</b>\n\n"
"<b>💰 Цена:</b> {price:.2f}\n\n"
"<b>🔑 Логин:</b> {login}\n"
"<b>🔒 Пароль:</b> {password}\n\n"
"<b>🌍 Регион:</b> {region}\n"
"<b>🎮 Ранг:</b> {current_rank}\n"
"<b>🔪 Количество ножей:</b> {knife_count}\n"
"<b>🛡️ Количество агентов:</b> {agent_count}\n"
"<b>🖌️ Скины:</b> {skin_count}\n"
"<b>🗡️ Стоимость инвентаря:</b> {inventory_value}VP\n"
"<b>📈 Последний ранг:</b> {last_rank}\n"
"<b>🏅 Уровень:</b> {level}\n"
)
async def _format_account_info(account: Account) -> str:
"""Формирует строку с информацией об аккаунте."""
return ACCOUNT_INFO_TEMPLATE.format(
price=account.price,
login=account.login,
password=account.password,
region=account.region,
current_rank=account.current_rank,
knife_count=account.knife_count,
agent_count=account.agent_count,
skin_count=account.skin_count,
inventory_value=account.inventory_value,
last_rank=account.last_rank,
level=account.level,
)
async def on_history_account_selected(callback: CallbackQuery, selecte, manager: DialogManager, item_id: int):
try:
if not isinstance(item_id, int):
item_id = int(item_id)
manager.current_context().dialog_data["selected_account_id"] = item_id
async with async_session() as session:
async with session.begin():
account = await session.scalar(select(Account).where(Account.id == item_id))
if account:
manager.current_context().dialog_data["account_info"] = await _format_account_info(account)
else:
logging.warning(f"Account with id {item_id} not found")
manager.current_context().dialog_data["account_info"] = "Аккаунт не найден!"
await manager.switch_to(PurchaseHistorySG.details)
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)