shopbot/bot/services/history.py
2024-12-25 04:30:33 +03:00

77 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
from aiogram.types import CallbackQuery
from aiogram_dialog import DialogManager
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from shopbot.bot.database.db import async_session
from shopbot.bot.database.models import Account
from shopbot.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)