From 2f5519e8d56f7bf4d9b88b8806b0d6e2fbcb734e Mon Sep 17 00:00:00 2001 From: teryaev-anton Date: Sat, 2 Sep 2023 09:30:17 +0500 Subject: [PATCH] Refactoring - returned States.REGION to class States --- src/bot/constants/states.py | 1 + src/bot/handlers/ask_question.py | 2 +- src/bot/handlers/assistance.py | 6 +++--- src/bot/handlers/service_handlers.py | 4 ++-- src/bot/handlers/show_objects.py | 2 +- src/bot/keyboards/assistance.py | 2 +- src/bot/keyboards/assistance_types.py | 2 +- src/tests/unit/test_handlers/test_select_type_of_help.py | 7 ++----- 8 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/bot/constants/states.py b/src/bot/constants/states.py index 56e3f492..e4975777 100644 --- a/src/bot/constants/states.py +++ b/src/bot/constants/states.py @@ -5,6 +5,7 @@ class States(str, enum.Enum): """Main bot states.""" GET_ASSISTANCE = "get_assistance" + REGION = "region" ASSISTANCE_TYPE = "assistance_type" FUND_PROGRAMS = "fund_programs" GET_USER_QUESTION = "get_user_question" diff --git a/src/bot/handlers/ask_question.py b/src/bot/handlers/ask_question.py index dbd7cf40..b6f02a4a 100644 --- a/src/bot/handlers/ask_question.py +++ b/src/bot/handlers/ask_question.py @@ -105,7 +105,7 @@ async def get_username( def get_coordinator_email(context: ContextTypes.DEFAULT_TYPE): """Get coordinator email address.""" coordinator = Coordinator.objects.get( - region__region_key=context.user_data["region"], + region__region_key=context.user_data[States.REGION], ) return coordinator.email_address diff --git a/src/bot/handlers/assistance.py b/src/bot/handlers/assistance.py index bf7040d5..d4dc1040 100644 --- a/src/bot/handlers/assistance.py +++ b/src/bot/handlers/assistance.py @@ -61,7 +61,7 @@ async def select_type_of_assistance( ) -> States: """Select assistance type.""" if States.ASSISTANCE_TYPE.value not in update.callback_query.data: - context.user_data["region"] = update.callback_query.data + context.user_data[States.REGION] = update.callback_query.data await update.callback_query.answer() await update.callback_query.edit_message_text( text=ASSISTANCE_TYPE_MESSAGE, @@ -85,7 +85,7 @@ async def select_assistance( if question_type: context.user_data[States.GET_USERNAME] = question_type context.user_data[States.QUESTION_TYPE] = question_type - region = context.user_data.get("region") + region = context.user_data.get(States.REGION) await query.answer() keyboard = await build_question_keyboard( region, @@ -108,7 +108,7 @@ async def fund_programs( ) -> None: """Show fund programs.""" query = update.callback_query - region = context.user_data.get("region") + region = context.user_data.get(States.REGION) _, page_number = parse_callback_data(query.data, FUND_PROGRAMS) page_number = page_number or DEFAULT_PAGE await query.answer() diff --git a/src/bot/handlers/service_handlers.py b/src/bot/handlers/service_handlers.py index 336d2ae9..fada5633 100644 --- a/src/bot/handlers/service_handlers.py +++ b/src/bot/handlers/service_handlers.py @@ -41,11 +41,11 @@ async def answer_all_messages( FUNCTIONS: dict[str, Callable[[Any, Any], Awaitable[States]]] = { - "welcome_screen": start, + States.START: start, States.ASSISTANCE_TYPE: select_type_of_assistance, States.CONTACT_US: contact_with_us, States.FUND_PROGRAMS: fund_programs, - States.GET_ASSISTANCE.value: get_assistance, + States.REGION.value: get_assistance, States.SHOW_CONTACT: show_contact, States.GET_USERNAME: get_user_question, States.USERNAME_AFTER_RETURNING: get_username_after_returning_back, diff --git a/src/bot/handlers/show_objects.py b/src/bot/handlers/show_objects.py index 7197d47c..4993c62a 100644 --- a/src/bot/handlers/show_objects.py +++ b/src/bot/handlers/show_objects.py @@ -28,7 +28,7 @@ async def show_contact( """Show contacts of the regional curator.""" query = update.callback_query coordinator = await Coordinator.objects.filter( - region__region_key=context.user_data["region"] + region__region_key=context.user_data[States.REGION] ).afirst() chief = await Coordinator.objects.filter(is_chief=True).afirst() await query.answer() diff --git a/src/bot/keyboards/assistance.py b/src/bot/keyboards/assistance.py index 491e7874..2d31e6fd 100644 --- a/src/bot/keyboards/assistance.py +++ b/src/bot/keyboards/assistance.py @@ -72,7 +72,7 @@ async def build_region_keyboard( telegram_paginator.add_after( InlineKeyboardButton( text=BACK_BUTTON, - callback_data=f"back_to_{'welcome_screen'}", + callback_data=f"back_to_{States.START.value}", ), ) return telegram_paginator diff --git a/src/bot/keyboards/assistance_types.py b/src/bot/keyboards/assistance_types.py index f97cbbad..aeac89c7 100644 --- a/src/bot/keyboards/assistance_types.py +++ b/src/bot/keyboards/assistance_types.py @@ -45,7 +45,7 @@ [ InlineKeyboardButton( text=BACK_BUTTON, - callback_data=f"back_to_{States.GET_ASSISTANCE.value}", + callback_data=f"back_to_{States.REGION.value}", ) ], ] diff --git a/src/tests/unit/test_handlers/test_select_type_of_help.py b/src/tests/unit/test_handlers/test_select_type_of_help.py index e87fd0fb..9a5710bf 100644 --- a/src/tests/unit/test_handlers/test_select_type_of_help.py +++ b/src/tests/unit/test_handlers/test_select_type_of_help.py @@ -40,8 +40,7 @@ async def test_select_type_of_assistance_store_region( ): """Receive select type of help handler stores correct region in context unittest.""" update.callback_query.data = region - # context.user_data = {States.REGION: initial_region} - context.user_data = {"region": initial_region} + context.user_data = {States.REGION: initial_region} await select_type_of_assistance(update, context) @@ -51,9 +50,7 @@ async def test_select_type_of_assistance_store_region( reply_markup=common_settings["keyboard_markup"], ) assert ( - # context.user_data[States.REGION] == region - context.user_data["region"] - == region + context.user_data[States.REGION] == region ) == expected_region_changed, ( f"Region in context.user_data must" f"{(not expected_region_changed and ' not')} "