From 9bc46a01fe2ac65e361193c8f6fa33f47621f99c Mon Sep 17 00:00:00 2001 From: user Date: Thu, 29 Feb 2024 00:24:42 +0300 Subject: [PATCH] dev@locazia: fix circular import --- app/bot/routers/home.py | 72 +++++++++++++++++++++++++++++++++++ app/bot/routers/index.py | 69 +-------------------------------- app/bot/routers/tonconnect.py | 2 +- 3 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 app/bot/routers/home.py diff --git a/app/bot/routers/home.py b/app/bot/routers/home.py new file mode 100644 index 0000000..5b95ab1 --- /dev/null +++ b/app/bot/routers/home.py @@ -0,0 +1,72 @@ +from app.core._utils.tg_process_template import tg_process_template +from app.core._keyboards import get_inline_keyboard +from app.core.logger import logger +from app.core.models.wallet_connection import WalletConnection +from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info +from app.core._config import WEB_APP_URLS +from aiogram import types, Router, F +from aiogram.filters import Command + + +main_router = Router() + + +async def send_home_menu(chat_wrap, user, wallet_connection): + return await tg_process_template( + chat_wrap, user.translated('home_menu').format( + wallet_address=wallet_connection.wallet_address + ), keyboard=get_inline_keyboard([ + [{ + 'text': user.translated('ownedContent_button'), + 'callback_data': 'ownedContent' + }], + [{ + 'text': user.translated('disconnectWallet_button'), + 'callback_data': 'disconnectWallet' + }] + ]) + ) + + +async def send_connect_wallets_list(db_session, chat_wrap, user): + ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) + await ton_connect.restore_connection() + wallets = ton_connect._sdk_client.get_wallets() + message_text = user.translated("connectWalletsList_menu") + return await tg_process_template( + chat_wrap, message_text, + keyboard=get_inline_keyboard([ + [ + { + 'text': f"{wallets[i]['name']}", + 'callback_data': f"initTonconnect_{wallets[i]['app_name']}" + } if i < len(wallets) else None, + { + 'text': f"{wallets[i + 1]['name']}", + 'callback_data': f"initTonconnect_{wallets[i + 1]['app_name']}" + } if i + 1 < len(wallets) else None, + ] + for i in range(0, len(wallets), 2) + ]) + ) + + +async def t_home_menu(__msg, **extra): + memory, user, db_session, chat_wrap = extra['memory'], extra['user'], extra['db_session'], extra['chat_wrap'] + if extra.get('state'): + await extra['state'].clear() + + wallet_connection = db_session.query(WalletConnection).filter( + WalletConnection.user_id == user.id, + WalletConnection.invalidated == False + ).first() + + if not wallet_connection: + return await send_connect_wallets_list(db_session, chat_wrap, user) + + return await send_home_menu(chat_wrap, user, wallet_connection) + + +main_router.message.register(t_home_menu, Command('start')) +main_router.callback_query.register(t_home_menu, F.data == 'home') +router = main_router diff --git a/app/bot/routers/index.py b/app/bot/routers/index.py index 3aa7c07..c3ae0e9 100644 --- a/app/bot/routers/index.py +++ b/app/bot/routers/index.py @@ -5,78 +5,13 @@ import traceback from aiogram import types, Router, F from aiogram.filters import Command +from app.bot.routers.home import router as home_router from app.bot.routers.tonconnect import router as tonconnect_router from app.bot.routers.content import router as content_router - -from app.core._utils.tg_process_template import tg_process_template -from app.core._keyboards import get_inline_keyboard from app.core.logger import logger -from app.core.models.wallet_connection import WalletConnection -from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info -from app.core._config import WEB_APP_URLS main_router = Router() - - -async def send_home_menu(chat_wrap, user, wallet_connection): - return await tg_process_template( - chat_wrap, user.translated('home_menu').format( - wallet_address=wallet_connection.wallet_address - ), keyboard=get_inline_keyboard([ - [{ - 'text': user.translated('ownedContent_button'), - 'callback_data': 'ownedContent' - }], - [{ - 'text': user.translated('disconnectWallet_button'), - 'callback_data': 'disconnectWallet' - }] - ]) - ) - - -async def send_connect_wallets_list(db_session, chat_wrap, user): - ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) - await ton_connect.restore_connection() - wallets = ton_connect._sdk_client.get_wallets() - message_text = user.translated("connectWalletsList_menu") - return await tg_process_template( - chat_wrap, message_text, - keyboard=get_inline_keyboard([ - [ - { - 'text': f"{wallets[i]['name']}", - 'callback_data': f"initTonconnect_{wallets[i]['app_name']}" - } if i < len(wallets) else None, - { - 'text': f"{wallets[i + 1]['name']}", - 'callback_data': f"initTonconnect_{wallets[i + 1]['app_name']}" - } if i + 1 < len(wallets) else None, - ] - for i in range(0, len(wallets), 2) - ]) - ) - - -async def t_home_menu(__msg, **extra): - memory, user, db_session, chat_wrap = extra['memory'], extra['user'], extra['db_session'], extra['chat_wrap'] - if extra.get('state'): - await extra['state'].clear() - - wallet_connection = db_session.query(WalletConnection).filter( - WalletConnection.user_id == user.id, - WalletConnection.invalidated == False - ).first() - - if not wallet_connection: - return await send_connect_wallets_list(db_session, chat_wrap, user) - - return await send_home_menu(chat_wrap, user, wallet_connection) - - -main_router.message.register(t_home_menu, Command('start')) -main_router.callback_query.register(t_home_menu, F.data == 'home') - +main_router.include_routers(home_router) main_router.include_routers(tonconnect_router) main_router.include_routers(content_router) diff --git a/app/bot/routers/tonconnect.py b/app/bot/routers/tonconnect.py index e0a1f4a..34afbcd 100644 --- a/app/bot/routers/tonconnect.py +++ b/app/bot/routers/tonconnect.py @@ -8,7 +8,7 @@ from app.core._utils.tg_process_template import tg_process_template from app.core.logger import make_log from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info from app.core.models.wallet_connection import WalletConnection -from app.bot.routers.index import send_connect_wallets_list +from app.bot.routers.home import send_connect_wallets_list router = Router()