Skip to content

Commit

Permalink
Refactoring - returned States.REGION to class States
Browse files Browse the repository at this point in the history
  • Loading branch information
teryaev-anton committed Sep 2, 2023
1 parent 623d8f0 commit 2f5519e
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/bot/constants/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/bot/handlers/ask_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/bot/handlers/assistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/bot/handlers/service_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/bot/handlers/show_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/bot/keyboards/assistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bot/keyboards/assistance_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
)
],
]
Expand Down
7 changes: 2 additions & 5 deletions src/tests/unit/test_handlers/test_select_type_of_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')} "
Expand Down

0 comments on commit 2f5519e

Please sign in to comment.