Skip to content

Commit

Permalink
fix:simplify get_tasks_report command & move extra features (#305)
Browse files Browse the repository at this point in the history
fix:simplify get_tasks_report command & move extra features to new command
  • Loading branch information
riveriswild authored Jan 10, 2024
1 parent f648ceb commit 3c4ae47
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def init_handlers(self):
direct_message_only(handlers.get_tasks_report),
"получить список задач из Trello",
)
self.add_manager_handler(
"get_tasks_report_advanced",
CommandCategories.SUMMARY,
direct_message_only(handlers.get_tasks_report_advanced),
"получить список задач из Trello (расширенный)",
)
self.add_manager_handler(
"get_articles_arts",
CommandCategories.SUMMARY,
Expand Down
2 changes: 1 addition & 1 deletion src/tg/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Admin (developer) handlers
from .get_roles_for_member_handler import get_roles_for_member
from .get_tasks_report_handler import get_tasks_report
from .get_tasks_report_handler import get_tasks_report, get_tasks_report_advanced
from .help_handler import help
from .list_chats_handler import list_chats
from .list_job_handler import list_jobs
Expand Down
21 changes: 18 additions & 3 deletions src/tg/handlers/get_tasks_report_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@

@manager_only
def get_tasks_report(update: telegram.Update, tg_context: telegram.ext.CallbackContext):
# set initial dialogue data
_get_task_report_base(update, tg_context, advanced=False)

return


@manager_only
def get_tasks_report_advanced(
update: telegram.Update, tg_context: telegram.ext.CallbackContext
):
_get_task_report_base(update, tg_context, advanced=True)

return


def _get_task_report_base(
update: telegram.Update, tg_context: telegram.ext.CallbackContext, advanced: bool
):
app_context = AppContext()

boards_list = app_context.trello_client.get_boards_for_user()
Expand All @@ -34,15 +50,14 @@ def get_tasks_report(update: telegram.Update, tg_context: telegram.ext.CallbackC
tg_context.chat_data[TASK_NAME] = {
consts.NEXT_ACTION: consts.PlainTextUserAction.GET_TASKS_REPORT__ENTER_BOARD_NUMBER.value
}
tg_context.chat_data["advanced"] = advanced
reply(
load(
"get_tasks_report_handler__choose_trello_board", lists=boards_list_formatted
),
update,
)

return


def generate_report_messages(
board_id: str, list_id: str, introduction: str, add_labels: bool
Expand Down
42 changes: 28 additions & 14 deletions src/tg/handlers/user_message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def handle_user_message(
assert 0 <= list_idx < len(board_list)
board_id = board_list[list_idx]["id"]
trello_lists = trello_client.get_lists(board_id)
trello_lists = trello_lists[::-1]
except Exception as e:
logger.warning(e)
reply(
Expand All @@ -107,7 +108,10 @@ def handle_user_message(
]

trello_lists_formatted = "\n".join(
[f"{i + 1}) {lst.name}" for i, lst in enumerate(trello_lists)]
[
f"{len(trello_lists) - i}) {lst.name}"
for i, lst in enumerate(trello_lists)
]
)
reply(
load(
Expand All @@ -123,8 +127,8 @@ def handle_user_message(
elif next_action == PlainTextUserAction.GET_TASKS_REPORT__ENTER_LIST_NUMBER:
try:
trello_lists = command_data.get(consts.GetTasksReportData.LISTS, [])
list_idx = int(user_input) - 1
assert 0 <= list_idx < len(trello_lists)
list_idx = -int(user_input)
assert 0 > list_idx >= -len(trello_lists)
list_id = trello_lists[list_idx]["id"]
except Exception as e:
logger.warning(e)
Expand All @@ -148,6 +152,12 @@ def handle_user_message(
]
]
)
if not tg_context.chat_data.get("advanced"):
add_labels = button == ButtonValues.GET_TASKS_REPORT__LABELS__NO
command_data[consts.GetTasksReportData.INTRO_TEXT] = None
handle_task_report(command_data, add_labels, update)
return

reply(
load("get_tasks_report_handler__enter_intro"),
update,
Expand Down Expand Up @@ -188,17 +198,7 @@ def handle_user_message(
reply(load("user_message_handler__press_button_please"), update)
return
add_labels = button == ButtonValues.GET_TASKS_REPORT__LABELS__YES
board_id = command_data[consts.GetTasksReportData.BOARD_ID]
list_id = command_data[consts.GetTasksReportData.LIST_ID]
introduction = command_data[consts.GetTasksReportData.INTRO_TEXT]
messages = get_tasks_report_handler.generate_report_messages(
board_id, list_id, introduction, add_labels
)
for message in messages:
reply(message, update)
# finished with last action for /trello_client_get_lists
set_next_action(command_data, None)
return
handle_task_report(command_data, add_labels, update)
elif next_action == PlainTextUserAction.MANAGE_REMINDERS__CHOOSE_ACTION:
if button is None:
reply(load("user_message_handler__press_button_please"), update)
Expand Down Expand Up @@ -546,3 +546,17 @@ def handle_new_members(
# writes chat_id and chat name to db when anybody (including the bot) is added to a new chat
# very heuristic solution
DBClient().set_chat_name(get_chat_id(update), get_chat_name(update))


def handle_task_report(command_data, add_labels, update):
board_id = command_data[consts.GetTasksReportData.BOARD_ID]
list_id = command_data[consts.GetTasksReportData.LIST_ID]
introduction = command_data[consts.GetTasksReportData.INTRO_TEXT]
messages = get_tasks_report_handler.generate_report_messages(
board_id, list_id, introduction, add_labels
)
for message in messages:
reply(message, update)
# finished with last action for /trello_client_get_lists
set_next_action(command_data, None)
return

0 comments on commit 3c4ae47

Please sign in to comment.