From 92030bf5cb3eddb39085361e9f0548286d29379a Mon Sep 17 00:00:00 2001 From: torrua Date: Mon, 9 Dec 2024 21:38:58 +0700 Subject: [PATCH 1/8] Working DRAFT --- app/bot/telegram/keyboards.py | 205 ++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 97 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 7f8eb1a..8e4e822 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -16,7 +16,109 @@ ) +def get_delimiter(number_of_items: int) -> int: + + allowed_range = list(range(MIN_NUMBER_OF_BUTTONS, MIN_NUMBER_OF_BUTTONS + 11)) + lst = [(number_of_items % i, i) for i in allowed_range] + delimiter = min(lst, key=lambda x: abs(x[0] - MIN_NUMBER_OF_BUTTONS))[1] + for i in lst: + if i[0] == 0: + delimiter = i[1] + break + return delimiter + + +def get_slice_end(slice_start: int, number_of_items: int) -> int: + last_allowed_item = slice_start + get_delimiter(number_of_items) + slice_end = min(last_allowed_item, number_of_items) + return slice_end + + +def keyboard_data(slice_start: int, items: list): + """ + :param items: + :param slice_start: + :return: + """ + slice_end = get_slice_end(slice_start, len(items)) + current_item_set = items[slice_start:slice_end] + + kb_items = [ + { + t: item.name, + cbd: callback_from_info( + { + mark_entity: entity_predy, + mark_action: action_predy_send_card, + mark_record_id: item.id, + } + ), + } + for item in current_item_set + ] + return Keyboa(items=kb_items, items_in_row=3)() + + +def keyboard_navi( + index_start: int, number_of_items: int, word_id: int, action_mark: str +): + """ + :param action_mark: + :param word_id: + :param number_of_items: + :param index_start: + :return: + """ + + delimiter = get_delimiter(number_of_items) + if number_of_items <= delimiter: + return None + + index_end = get_slice_end(index_start, number_of_items) + + text_arrow_back = "❮❮" + text_arrow_forward = "❯❯" + button_back, button_forward = None, None + + common_data = { + mark_entity: entity_predy, + mark_action: action_mark, + mark_record_id: word_id, + } + + if index_start != 0: + data_cbd_predy_kb_back = { + **common_data, + mark_slice_start: index_start - delimiter, + } + button_back = { + t: text_arrow_back, + cbd: callback_from_info(data_cbd_predy_kb_back), + } + + if index_end != number_of_items: + data_cbd_predy_kb_forward = { + **common_data, + mark_slice_start: index_end, + } + button_forward = { + t: text_arrow_forward, + cbd: callback_from_info(data_cbd_predy_kb_forward), + } + + nav_row = [b for b in [button_back, button_forward] if b] + return Keyboa(nav_row, items_in_row=2)() + + +def kb_close(): + """ + :return: + """ + return Keyboa({t: "Close", cbd: "close"})() + + class WordKeyboard: + def __init__(self, word): self.word = word self.items = self._get_items() @@ -31,51 +133,6 @@ def get_title(self): return "Parent" + f"{'s' if len(self.word.parents) > 1 else ''}" return "Complex" + f"{'es' if len(self.word.complexes) > 1 else ''}" - def _keyboard_navi(self, index_start: int): - """ - :param index_start: - :return: - """ - - delimiter = self._get_delimiter() - if len(self.items) <= delimiter: - return None - - index_end = self.get_slice_end(index_start) - - text_arrow_back = "❮❮" - text_arrow_forward = "❯❯" - button_back, button_forward = None, None - - common_data = { - mark_entity: entity_predy, - mark_action: action_predy_kb_cpx_show, - mark_record_id: self.word.id, - } - - if index_start != 0: - cbd_predy_kb_cpx_back = { - **common_data, - mark_slice_start: index_start - delimiter, - } - button_back = { - t: text_arrow_back, - cbd: callback_from_info(cbd_predy_kb_cpx_back), - } - - if index_end != len(self.items): - cbd_predy_kb_cpx_forward = { - **common_data, - mark_slice_start: index_end, - } - button_forward = { - t: text_arrow_forward, - cbd: callback_from_info(cbd_predy_kb_cpx_forward), - } - - nav_row = [b for b in [button_back, button_forward] if b] - return Keyboa(nav_row, items_in_row=2)() - def _keyboard_hide(self): """ :return: @@ -109,54 +166,15 @@ def _keyboard_show(self): ] return Keyboa.combine((Keyboa(button_show)(), kb_close())) - def _get_delimiter(self): - """ - :return: - """ - allowed_range = list(range(MIN_NUMBER_OF_BUTTONS, MIN_NUMBER_OF_BUTTONS + 11)) - lst = [(len(self.items) % i, i) for i in allowed_range] - delimiter = min(lst, key=lambda x: abs(x[0] - MIN_NUMBER_OF_BUTTONS))[1] - for i in lst: - if i[0] == 0: - delimiter = i[1] - break - return delimiter - - def _keyboard_data(self, slice_start: int): - """ - :param slice_start: - :return: - """ - slice_end = self.get_slice_end(slice_start) - current_item_set = self.items[slice_start:slice_end] - - kb_items = [ - { - t: item.name, - cbd: callback_from_info( - { - mark_entity: entity_predy, - mark_action: action_predy_send_card, - mark_record_id: item.id, - } - ), - } - for item in current_item_set - ] - return Keyboa(items=kb_items, items_in_row=3)() - - def _keyboard_complete(self, slice_start: int): - kb_data = self._keyboard_data(slice_start) - kb_navi = self._keyboard_navi(slice_start) + def _keyboard_complete(self, slice_start: int, items: list, word_id: int): + kb_data = keyboard_data(slice_start, items) + kb_navi = keyboard_navi( + slice_start, len(items), word_id, action_predy_kb_cpx_show + ) kb_hide = self._keyboard_hide() kb_combo = (kb_hide, kb_data, kb_navi, kb_close()) return Keyboa.combine(kb_combo) - def get_slice_end(self, slice_start: int) -> int: - last_allowed_item = slice_start + self._get_delimiter() - slice_end = min(last_allowed_item, len(self.items)) - return slice_end - def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): """ :param show_list: @@ -170,11 +188,4 @@ def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): if not show_list: return self._keyboard_show() - return self._keyboard_complete(slice_start) - - -def kb_close(): - """ - :return: - """ - return Keyboa({t: "Close", cbd: "close"})() + return self._keyboard_complete(slice_start, self.items, self.word.id) From 42f8b0243ac37df8d55a5eb3841af8820321a270 Mon Sep 17 00:00:00 2001 From: torrua Date: Mon, 9 Dec 2024 21:45:38 +0700 Subject: [PATCH 2/8] Working DRAFT --- app/bot/telegram/keyboards.py | 55 ++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 8e4e822..2683d31 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -117,6 +117,31 @@ def kb_close(): return Keyboa({t: "Close", cbd: "close"})() +def keyboard_hide(title: str, word_id: int): + """ + :return: + """ + + text_hide = f"Hide {title}" + cbd_predy_kb_cpx_hide = { + mark_entity: entity_predy, + mark_action: action_predy_kb_cpx_hide, + mark_record_id: word_id, + } + button_predy_kb_cpx_hide = [ + {t: text_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, + ] + return Keyboa(button_predy_kb_cpx_hide)() + + +def keyboard_complete(slice_start: int, items: list, word_id: int, title: str): + kb_data = keyboard_data(slice_start, items) + kb_navi = keyboard_navi(slice_start, len(items), word_id, action_predy_kb_cpx_show) + kb_hide = keyboard_hide(title, word_id) + kb_combo = (kb_hide, kb_data, kb_navi, kb_close()) + return Keyboa.combine(kb_combo) + + class WordKeyboard: def __init__(self, word): @@ -133,22 +158,6 @@ def get_title(self): return "Parent" + f"{'s' if len(self.word.parents) > 1 else ''}" return "Complex" + f"{'es' if len(self.word.complexes) > 1 else ''}" - def _keyboard_hide(self): - """ - :return: - """ - - text_hide = f"Hide {self.get_title()}" - cbd_predy_kb_cpx_hide = { - mark_entity: entity_predy, - mark_action: action_predy_kb_cpx_hide, - mark_record_id: self.word.id, - } - button_predy_kb_cpx_hide = [ - {t: text_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, - ] - return Keyboa(button_predy_kb_cpx_hide)() - def _keyboard_show(self): """ :return: @@ -156,6 +165,7 @@ def _keyboard_show(self): total_num = len(self.items) number = f" ({total_num})" if total_num > 1 else "" text_cpx_show = f"Show {self.get_title()}{number}" + cbd_predy_kb_cpx_show = { mark_entity: entity_predy, mark_action: action_predy_kb_cpx_show, @@ -166,15 +176,6 @@ def _keyboard_show(self): ] return Keyboa.combine((Keyboa(button_show)(), kb_close())) - def _keyboard_complete(self, slice_start: int, items: list, word_id: int): - kb_data = keyboard_data(slice_start, items) - kb_navi = keyboard_navi( - slice_start, len(items), word_id, action_predy_kb_cpx_show - ) - kb_hide = self._keyboard_hide() - kb_combo = (kb_hide, kb_data, kb_navi, kb_close()) - return Keyboa.combine(kb_combo) - def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): """ :param show_list: @@ -188,4 +189,6 @@ def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): if not show_list: return self._keyboard_show() - return self._keyboard_complete(slice_start, self.items, self.word.id) + return keyboard_complete( + slice_start, self.items, self.word.id, self.get_title() + ) From bed0269c5414d78e72bd5a39a7cc2d8ff656c589 Mon Sep 17 00:00:00 2001 From: torrua Date: Mon, 9 Dec 2024 21:56:32 +0700 Subject: [PATCH 3/8] Working DRAFT --- app/bot/telegram/keyboards.py | 43 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 2683d31..b3e4598 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -142,6 +142,24 @@ def keyboard_complete(slice_start: int, items: list, word_id: int, title: str): return Keyboa.combine(kb_combo) +def keyboard_show(number_of_items: int, title: str, word_id: int, action_mark: str): + """ + :return: + """ + number = f" ({number_of_items})" if number_of_items > 1 else "" + text_cpx_show = f"Show {title}{number}" + + cbd_predy_kb_cpx_show = { + mark_entity: entity_predy, + mark_action: action_mark, + mark_record_id: word_id, + } + button_show = [ + {t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, + ] + return Keyboa.combine((Keyboa(button_show)(), kb_close())) + + class WordKeyboard: def __init__(self, word): @@ -158,24 +176,6 @@ def get_title(self): return "Parent" + f"{'s' if len(self.word.parents) > 1 else ''}" return "Complex" + f"{'es' if len(self.word.complexes) > 1 else ''}" - def _keyboard_show(self): - """ - :return: - """ - total_num = len(self.items) - number = f" ({total_num})" if total_num > 1 else "" - text_cpx_show = f"Show {self.get_title()}{number}" - - cbd_predy_kb_cpx_show = { - mark_entity: entity_predy, - mark_action: action_predy_kb_cpx_show, - mark_record_id: self.word.id, - } - button_show = [ - {t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, - ] - return Keyboa.combine((Keyboa(button_show)(), kb_close())) - def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): """ :param show_list: @@ -187,7 +187,12 @@ def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): return kb_close() if not show_list: - return self._keyboard_show() + return keyboard_show( + len(self.items), + self.get_title(), + self.word.id, + action_predy_kb_cpx_show, + ) return keyboard_complete( slice_start, self.items, self.word.id, self.get_title() From b7cfbce0f0a8fc518d43e87b587a0888cc192e69 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 11 Dec 2024 20:41:33 +0700 Subject: [PATCH 4/8] Working DRAFT --- app/bot/telegram/handlers/bib_functions.py | 30 +---- app/bot/telegram/handlers/inline.py | 20 ++- app/bot/telegram/keyboards.py | 143 ++++++++++++--------- app/bot/telegram/variables.py | 10 +- 4 files changed, 106 insertions(+), 97 deletions(-) diff --git a/app/bot/telegram/handlers/bib_functions.py b/app/bot/telegram/handlers/bib_functions.py index 917afb2..b975fb5 100644 --- a/app/bot/telegram/handlers/bib_functions.py +++ b/app/bot/telegram/handlers/bib_functions.py @@ -6,7 +6,7 @@ from app.bot.telegram import bot, cbq from app.bot.telegram.keyboards import WordKeyboard from app.bot.telegram.models import export_as_str -from app.bot.telegram.variables import mark_record_id, mark_slice_start +from app.bot.telegram.variables import mark_record_id, mark_slice_start, mark_action from app.decorators import logging_time from app.engine import async_session_maker @@ -24,7 +24,7 @@ async def bib_cancel(call: cbq): @logging_time async def bib_predy_send_card(call: cbq): """ - Обработка нажатия кнопки со словом на логлане + Обработка нажатия кнопки со logpua :param call: :return: """ @@ -46,15 +46,15 @@ async def bib_predy_send_card(call: cbq): @logging_time -async def bib_predy_kb_cpx_switcher(call: cbq, state: bool): +async def bib_predy_kb_cpx_switcher(call: cbq): """ Обработка нажатия кнопки отображения/скрытия комплексных слов :param call: - :param state: :return: """ info = info_from_callback(call.data) slice_start = info.pop(mark_slice_start, 0) + action = info.pop(mark_action, "") async with async_session_maker() as session: word = await ( @@ -65,7 +65,7 @@ async def bib_predy_kb_cpx_switcher(call: cbq, state: bool): ) keyboard = WordKeyboard(word).keyboard_cpx( - show_list=state, slice_start=slice_start + action=action, slice_start=slice_start ) await bot.edit_message_reply_markup( @@ -73,23 +73,3 @@ async def bib_predy_kb_cpx_switcher(call: cbq, state: bool): message_id=call.message.message_id, reply_markup=keyboard, ) - - -@logging_time -async def bib_predy_kb_cpx_show(call: cbq): - """ - Обработка нажатия кнопки отображения комплексных слов - :param call: - :return: - """ - await bib_predy_kb_cpx_switcher(call, True) - - -@logging_time -async def bib_predy_kb_cpx_hide(call: cbq): - """ - Обработка нажатия кнопки скрытия комплексных слов - :param call: - :return: - """ - await bib_predy_kb_cpx_switcher(call, False) diff --git a/app/bot/telegram/handlers/inline.py b/app/bot/telegram/handlers/inline.py index f080ba7..6ee7d5c 100644 --- a/app/bot/telegram/handlers/inline.py +++ b/app/bot/telegram/handlers/inline.py @@ -8,15 +8,11 @@ from app.bot.telegram.handlers.bib_functions import bib_cancel from app.bot.telegram.handlers.bib_functions import ( bib_predy_send_card, - bib_predy_kb_cpx_hide, - bib_predy_kb_cpx_show, -) -from app.bot.telegram.variables import ( - action_predy_send_card, - action_predy_kb_cpx_hide, - action_predy_kb_cpx_show, + bib_predy_kb_cpx_switcher, ) + from app.bot.telegram.variables import ( + Action, cancel, close, mark_entity, @@ -72,10 +68,12 @@ async def action_selector_predy(call: cbq): current_action = info.get(mark_action, None) actions = { - action_predy_send_card: bib_predy_send_card, - action_predy_kb_cpx_hide: bib_predy_kb_cpx_hide, - action_predy_kb_cpx_show: bib_predy_kb_cpx_show, + Action.send_card: bib_predy_send_card, + Action.kb_cpx_hide: bib_predy_kb_cpx_switcher, + Action.kb_cpx_show: bib_predy_kb_cpx_switcher, + Action.kb_afx_hide: bib_predy_kb_cpx_switcher, + Action.kb_afx_show: bib_predy_kb_cpx_switcher, } - if action_to_run := actions.get(current_action): + if action_to_run := actions.get(current_action): # TODO Add default keyboard await action_to_run(call) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index b3e4598..330ee7e 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -6,13 +6,11 @@ mark_entity, entity_predy, mark_action, - action_predy_kb_cpx_show, mark_record_id, mark_slice_start, t, cbd, - action_predy_kb_cpx_hide, - action_predy_send_card, + Action, ) @@ -49,7 +47,7 @@ def keyboard_data(slice_start: int, items: list): cbd: callback_from_info( { mark_entity: entity_predy, - mark_action: action_predy_send_card, + mark_action: Action.send_card, mark_record_id: item.id, } ), @@ -117,47 +115,20 @@ def kb_close(): return Keyboa({t: "Close", cbd: "close"})() -def keyboard_hide(title: str, word_id: int): +def keyboard_show_hide(title: str, word_id: int, action_mark: str): """ :return: """ - text_hide = f"Hide {title}" - cbd_predy_kb_cpx_hide = { - mark_entity: entity_predy, - mark_action: action_predy_kb_cpx_hide, - mark_record_id: word_id, - } - button_predy_kb_cpx_hide = [ - {t: text_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, - ] - return Keyboa(button_predy_kb_cpx_hide)() - - -def keyboard_complete(slice_start: int, items: list, word_id: int, title: str): - kb_data = keyboard_data(slice_start, items) - kb_navi = keyboard_navi(slice_start, len(items), word_id, action_predy_kb_cpx_show) - kb_hide = keyboard_hide(title, word_id) - kb_combo = (kb_hide, kb_data, kb_navi, kb_close()) - return Keyboa.combine(kb_combo) - - -def keyboard_show(number_of_items: int, title: str, word_id: int, action_mark: str): - """ - :return: - """ - number = f" ({number_of_items})" if number_of_items > 1 else "" - text_cpx_show = f"Show {title}{number}" - - cbd_predy_kb_cpx_show = { + cbd_predy = { mark_entity: entity_predy, mark_action: action_mark, mark_record_id: word_id, } - button_show = [ - {t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, + button = [ + {t: title, cbd: callback_from_info(cbd_predy)}, ] - return Keyboa.combine((Keyboa(button_show)(), kb_close())) + return Keyboa(button)() class WordKeyboard: @@ -171,29 +142,85 @@ def _get_items(self): return self.word.parents return self.word.complexes - def get_title(self): - if self.word.type.parentable: - return "Parent" + f"{'s' if len(self.word.parents) > 1 else ''}" - return "Complex" + f"{'es' if len(self.word.complexes) > 1 else ''}" - - def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): + def get_title(self, show: bool, items_type: str): + show_text = "Show" if show else "Hide" + + match items_type: + case "parent": + return ( + f"{show_text} Parent" + + f"{f's ({len(self.word.parents)})' if len(self.word.parents) > 1 else ''}" + ) + case "djifoa": + return ( + f"{show_text} Djifoa" + + f"{f' ({len(self.word.affixes)})' if len(self.word.affixes) > 1 else ''}" + ) + case "complex": + return ( + f"{show_text} Complex" + + f"{f'es ({len(self.word.complexes)})' if len(self.word.complexes) > 1 else ''}" + ) + case _: + return show_text + + def keyboard_cpx(self, action: str = "", slice_start: int = 0): """ - :param show_list: + :param action: :param slice_start: :return: """ - - if not self.items: - return kb_close() - - if not show_list: - return keyboard_show( - len(self.items), - self.get_title(), - self.word.id, - action_predy_kb_cpx_show, - ) - - return keyboard_complete( - slice_start, self.items, self.word.id, self.get_title() - ) + print(action) + + match action: + case Action.kb_cpx_show: + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_cpx_hide + ) + + title_cpx = self.get_title(show=False, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_hide + ) + kb_data = keyboard_data(slice_start, self.word.complexes) + + kb_navi = keyboard_navi( + slice_start, + len(self.word.complexes), + self.word.id, + Action.kb_cpx_show, + ) + kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_data, kb_navi, kb_close()) + return Keyboa.combine(kb_combo) + + case Action.kb_cpx_hide: + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_cpx_show + ) + + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_show + ) + kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_close()) + return Keyboa.combine(kb_combo) + + case Action.kb_afx_show: + print("apkas") + case Action.kb_afx_hide: + print("apkah") + case _: + + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_cpx_show + ) + + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_show + ) + + return Keyboa.combine((kb_hide_djifoa, kb_hide_cpx, kb_close())) diff --git a/app/bot/telegram/variables.py b/app/bot/telegram/variables.py index cb2c7ff..0d14cc6 100644 --- a/app/bot/telegram/variables.py +++ b/app/bot/telegram/variables.py @@ -19,7 +19,11 @@ # entities entity_predy = "ep" + # actions -action_predy_send_card = "apsc" -action_predy_kb_cpx_show = "apkcs" -action_predy_kb_cpx_hide = "apkch" +class Action: + send_card = "apsc" + kb_cpx_show = "apkcs" + kb_cpx_hide = "apkch" + kb_afx_show = "apkas" + kb_afx_hide = "apkah" From ce86fce74ad242971156ffbe222a2e4c4d0e65c9 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 11 Dec 2024 20:50:19 +0700 Subject: [PATCH 5/8] Working DRAFT --- app/bot/telegram/keyboards.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 330ee7e..13a6f45 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -176,7 +176,7 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): case Action.kb_cpx_show: title_djifoa = self.get_title(show=True, items_type="djifoa") kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_cpx_hide + title_djifoa, self.word.id, Action.kb_afx_hide ) title_cpx = self.get_title(show=False, items_type="complex") @@ -197,7 +197,7 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): case Action.kb_cpx_hide: title_djifoa = self.get_title(show=True, items_type="djifoa") kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_cpx_show + title_djifoa, self.word.id, Action.kb_afx_show ) title_cpx = self.get_title(show=True, items_type="complex") @@ -208,14 +208,27 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): return Keyboa.combine(kb_combo) case Action.kb_afx_show: - print("apkas") + title_djifoa = self.get_title(show=False, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_afx_hide + ) + + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_show + ) + kb_data = keyboard_data(0, self.word.affixes) + + kb_combo = (kb_hide_djifoa, kb_data, kb_hide_cpx, kb_close()) + return Keyboa.combine(kb_combo) + case Action.kb_afx_hide: print("apkah") case _: title_djifoa = self.get_title(show=True, items_type="djifoa") kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_cpx_show + title_djifoa, self.word.id, Action.kb_afx_show ) title_cpx = self.get_title(show=True, items_type="complex") From d1d9d1bd54b60afc7a42f4633b0845116e3a8fd2 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 11 Dec 2024 21:21:06 +0700 Subject: [PATCH 6/8] Working DRAFT --- app/bot/telegram/handlers/bib_functions.py | 10 +-- app/bot/telegram/handlers/inline.py | 17 ++--- app/bot/telegram/keyboards.py | 89 ++++++++++++---------- app/bot/telegram/variables.py | 28 ++++--- 4 files changed, 74 insertions(+), 70 deletions(-) diff --git a/app/bot/telegram/handlers/bib_functions.py b/app/bot/telegram/handlers/bib_functions.py index b975fb5..969f9fe 100644 --- a/app/bot/telegram/handlers/bib_functions.py +++ b/app/bot/telegram/handlers/bib_functions.py @@ -6,7 +6,7 @@ from app.bot.telegram import bot, cbq from app.bot.telegram.keyboards import WordKeyboard from app.bot.telegram.models import export_as_str -from app.bot.telegram.variables import mark_record_id, mark_slice_start, mark_action +from app.bot.telegram.variables import Mark from app.decorators import logging_time from app.engine import async_session_maker @@ -34,7 +34,7 @@ async def bib_predy_send_card(call: cbq): async with async_session_maker() as session: word = await ( WordSelector() - .filter_by(id=info[mark_record_id]) + .filter_by(id=info[Mark.record_id]) .with_relationships() .scalar_async(session) ) @@ -53,13 +53,13 @@ async def bib_predy_kb_cpx_switcher(call: cbq): :return: """ info = info_from_callback(call.data) - slice_start = info.pop(mark_slice_start, 0) - action = info.pop(mark_action, "") + slice_start = info.pop(Mark.slice_start, 0) + action = info.pop(Mark.action, "") async with async_session_maker() as session: word = await ( WordSelector() - .filter_by(id=info[mark_record_id]) + .filter_by(id=info[Mark.record_id]) .with_relationships() .scalar_async(session) ) diff --git a/app/bot/telegram/handlers/inline.py b/app/bot/telegram/handlers/inline.py index 6ee7d5c..28c8362 100644 --- a/app/bot/telegram/handlers/inline.py +++ b/app/bot/telegram/handlers/inline.py @@ -15,8 +15,7 @@ Action, cancel, close, - mark_entity, - mark_action, + Mark, entity_predy, ) from app.decorators import logging_time @@ -36,8 +35,8 @@ async def bot_callback_inline(call: cbq): return info = info_from_callback(call.data) - current_entity = info.get(mark_entity, None) - current_action = info.get(mark_action, None) + current_entity = info.get(Mark.entity, None) + current_action = info.get(Mark.action, None) if not (current_entity and current_action): return @@ -52,7 +51,7 @@ async def entity_selector_general(call: cbq): :return: """ info = info_from_callback(call.data) - current_entity = info.get(mark_entity, None) + current_entity = info.get(Mark.entity, None) if current_entity == entity_predy: await action_selector_predy(call) @@ -65,15 +64,11 @@ async def action_selector_predy(call: cbq): :return: """ info = info_from_callback(call.data) - current_action = info.get(mark_action, None) + current_action = info.get(Mark.action, None) actions = { Action.send_card: bib_predy_send_card, - Action.kb_cpx_hide: bib_predy_kb_cpx_switcher, - Action.kb_cpx_show: bib_predy_kb_cpx_switcher, - Action.kb_afx_hide: bib_predy_kb_cpx_switcher, - Action.kb_afx_show: bib_predy_kb_cpx_switcher, } - if action_to_run := actions.get(current_action): # TODO Add default keyboard + if action_to_run := actions.get(current_action, bib_predy_kb_cpx_switcher): await action_to_run(call) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 13a6f45..0c3ab3f 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -3,14 +3,11 @@ from app.bot.telegram import MIN_NUMBER_OF_BUTTONS from app.bot.telegram.variables import ( - mark_entity, entity_predy, - mark_action, - mark_record_id, - mark_slice_start, t, cbd, Action, + Mark, ) @@ -46,9 +43,9 @@ def keyboard_data(slice_start: int, items: list): t: item.name, cbd: callback_from_info( { - mark_entity: entity_predy, - mark_action: Action.send_card, - mark_record_id: item.id, + Mark.entity: entity_predy, + Mark.action: Action.send_card, + Mark.record_id: item.id, } ), } @@ -79,15 +76,15 @@ def keyboard_navi( button_back, button_forward = None, None common_data = { - mark_entity: entity_predy, - mark_action: action_mark, - mark_record_id: word_id, + Mark.entity: entity_predy, + Mark.action: action_mark, + Mark.record_id: word_id, } if index_start != 0: data_cbd_predy_kb_back = { **common_data, - mark_slice_start: index_start - delimiter, + Mark.slice_start: index_start - delimiter, } button_back = { t: text_arrow_back, @@ -97,7 +94,7 @@ def keyboard_navi( if index_end != number_of_items: data_cbd_predy_kb_forward = { **common_data, - mark_slice_start: index_end, + Mark.slice_start: index_end, } button_forward = { t: text_arrow_forward, @@ -121,9 +118,9 @@ def keyboard_show_hide(title: str, word_id: int, action_mark: str): """ cbd_predy = { - mark_entity: entity_predy, - mark_action: action_mark, - mark_record_id: word_id, + Mark.entity: entity_predy, + Mark.action: action_mark, + Mark.record_id: word_id, } button = [ {t: title, cbd: callback_from_info(cbd_predy)}, @@ -154,7 +151,7 @@ def get_title(self, show: bool, items_type: str): case "djifoa": return ( f"{show_text} Djifoa" - + f"{f' ({len(self.word.affixes)})' if len(self.word.affixes) > 1 else ''}" + + f"{f' ({len(self.word.affixes)})' if len(self.word.affixes) else ''}" ) case "complex": return ( @@ -176,7 +173,7 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): case Action.kb_cpx_show: title_djifoa = self.get_title(show=True, items_type="djifoa") kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_hide + title_djifoa, self.word.id, Action.kb_afx_show ) title_cpx = self.get_title(show=False, items_type="complex") @@ -194,19 +191,6 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_data, kb_navi, kb_close()) return Keyboa.combine(kb_combo) - case Action.kb_cpx_hide: - title_djifoa = self.get_title(show=True, items_type="djifoa") - kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_show - ) - - title_cpx = self.get_title(show=True, items_type="complex") - kb_hide_cpx = keyboard_show_hide( - title_cpx, self.word.id, Action.kb_cpx_show - ) - kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_close()) - return Keyboa.combine(kb_combo) - case Action.kb_afx_show: title_djifoa = self.get_title(show=False, items_type="djifoa") kb_hide_djifoa = keyboard_show_hide( @@ -222,18 +206,39 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): kb_combo = (kb_hide_djifoa, kb_data, kb_hide_cpx, kb_close()) return Keyboa.combine(kb_combo) - case Action.kb_afx_hide: - print("apkah") - case _: - - title_djifoa = self.get_title(show=True, items_type="djifoa") - kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_show + case Action.kb_pnt_show: + title_parents = self.get_title(show=False, items_type="parent") + kb_hide_parents = keyboard_show_hide( + title_parents, self.word.id, Action.kb_pnt_hide ) + kb_data = keyboard_data(0, self.word.parents) - title_cpx = self.get_title(show=True, items_type="complex") - kb_hide_cpx = keyboard_show_hide( - title_cpx, self.word.id, Action.kb_cpx_show - ) + kb_combo = (kb_hide_parents, kb_data, kb_close()) + return Keyboa.combine(kb_combo) - return Keyboa.combine((kb_hide_djifoa, kb_hide_cpx, kb_close())) + case _: + return self.get_default_keyboard() + + def get_default_keyboard(self): + if self.word.type.parentable: + return self.get_default_kb_for_parentable() + else: + return self.get_default_kb_for_predy() + + def get_default_kb_for_predy(self): + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_afx_show + ) + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide(title_cpx, self.word.id, Action.kb_cpx_show) + kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_close()) + return Keyboa.combine(kb_combo) + + def get_default_kb_for_parentable(self): + title_parent = self.get_title(show=True, items_type="parent") + kb_hide_parent = keyboard_show_hide( + title_parent, self.word.id, Action.kb_pnt_show + ) + kb_combo = (kb_hide_parent, kb_close()) + return Keyboa.combine(kb_combo) diff --git a/app/bot/telegram/variables.py b/app/bot/telegram/variables.py index 0d14cc6..5f22702 100644 --- a/app/bot/telegram/variables.py +++ b/app/bot/telegram/variables.py @@ -8,13 +8,15 @@ cancel = "cancel" close = "close" -# marks -mark_language = "lang" -mark_page = "page" -mark_action = "act" -mark_entity = "ent" -mark_record_id = "rid" -mark_slice_start = "ss" + +class Mark: + language = "l" + page = "p" + action = "a" + entity = "e" + record_id = "r" + slice_start = "s" + # entities entity_predy = "ep" @@ -22,8 +24,10 @@ # actions class Action: - send_card = "apsc" - kb_cpx_show = "apkcs" - kb_cpx_hide = "apkch" - kb_afx_show = "apkas" - kb_afx_hide = "apkah" + send_card = "sc" + kb_cpx_show = "cs" + kb_cpx_hide = "ch" + kb_afx_show = "as" + kb_afx_hide = "ah" + kb_pnt_show = "ps" + kb_pnt_hide = "ph" From 851d40dfbf211917d756cff65c84d0184dbd41c7 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 11 Dec 2024 21:43:04 +0700 Subject: [PATCH 7/8] Working PROTOTYPE --- app/bot/telegram/keyboards.py | 153 +++++++++++++++++++++------------- 1 file changed, 96 insertions(+), 57 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 0c3ab3f..36338e1 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -128,16 +128,24 @@ def keyboard_show_hide(title: str, word_id: int, action_mark: str): return Keyboa(button)() +def combine_and_close(func): + def wrapper(self, *args, **kwargs): + # Call the original function to get the kb_combo list + kb_combo = func(self, *args, **kwargs) + + # Append kb_close() to the list + kb_combo.append(kb_close()) + + # Combine the keyboard buttons + return Keyboa.combine(tuple(kb_combo)) + + return wrapper + + class WordKeyboard: def __init__(self, word): self.word = word - self.items = self._get_items() - - def _get_items(self): - if self.word.type.parentable: - return self.word.parents - return self.word.complexes def get_title(self, show: bool, items_type: str): show_text = "Show" if show else "Hide" @@ -167,78 +175,109 @@ def keyboard_cpx(self, action: str = "", slice_start: int = 0): :param slice_start: :return: """ - print(action) match action: case Action.kb_cpx_show: - title_djifoa = self.get_title(show=True, items_type="djifoa") - kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_show - ) - - title_cpx = self.get_title(show=False, items_type="complex") - kb_hide_cpx = keyboard_show_hide( - title_cpx, self.word.id, Action.kb_cpx_hide - ) - kb_data = keyboard_data(slice_start, self.word.complexes) - - kb_navi = keyboard_navi( - slice_start, - len(self.word.complexes), - self.word.id, - Action.kb_cpx_show, - ) - kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_data, kb_navi, kb_close()) - return Keyboa.combine(kb_combo) + return self.get_kb_cpx_show(slice_start) case Action.kb_afx_show: - title_djifoa = self.get_title(show=False, items_type="djifoa") - kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_hide - ) - - title_cpx = self.get_title(show=True, items_type="complex") - kb_hide_cpx = keyboard_show_hide( - title_cpx, self.word.id, Action.kb_cpx_show - ) - kb_data = keyboard_data(0, self.word.affixes) - - kb_combo = (kb_hide_djifoa, kb_data, kb_hide_cpx, kb_close()) - return Keyboa.combine(kb_combo) + return self.get_kb_afx_show() case Action.kb_pnt_show: - title_parents = self.get_title(show=False, items_type="parent") - kb_hide_parents = keyboard_show_hide( - title_parents, self.word.id, Action.kb_pnt_hide - ) - kb_data = keyboard_data(0, self.word.parents) - - kb_combo = (kb_hide_parents, kb_data, kb_close()) - return Keyboa.combine(kb_combo) + return self.get_kb_pnt_show() case _: return self.get_default_keyboard() + @combine_and_close + def get_kb_pnt_show(self): + kb_combo = [] + + title_parents = self.get_title(show=False, items_type="parent") + kb_hide_parents = keyboard_show_hide( + title_parents, self.word.id, Action.kb_pnt_hide + ) + kb_data = keyboard_data(0, self.word.parents) + kb_combo.extend([kb_hide_parents, kb_data]) + + return kb_combo + + @combine_and_close + def get_kb_cpx_show(self, slice_start): + kb_combo = [] + if self.word.affixes: + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_afx_show + ) + kb_combo.append(kb_hide_djifoa) + + if self.word.complexes: + title_cpx = self.get_title(show=False, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_hide + ) + kb_data = keyboard_data(slice_start, self.word.complexes) + + kb_navi = keyboard_navi( + slice_start, + len(self.word.complexes), + self.word.id, + Action.kb_cpx_show, + ) + kb_combo.extend([kb_hide_cpx, kb_data, kb_navi]) + + return kb_combo + + @combine_and_close + def get_kb_afx_show(self): + kb_combo = [] + if self.word.affixes: + title_djifoa = self.get_title(show=False, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_afx_hide + ) + kb_data = keyboard_data(0, self.word.affixes) + kb_combo.extend([kb_hide_djifoa, kb_data]) + if self.word.complexes: + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_show + ) + kb_combo.append(kb_hide_cpx) + + return kb_combo + def get_default_keyboard(self): if self.word.type.parentable: return self.get_default_kb_for_parentable() else: return self.get_default_kb_for_predy() + @combine_and_close def get_default_kb_for_predy(self): - title_djifoa = self.get_title(show=True, items_type="djifoa") - kb_hide_djifoa = keyboard_show_hide( - title_djifoa, self.word.id, Action.kb_afx_show - ) - title_cpx = self.get_title(show=True, items_type="complex") - kb_hide_cpx = keyboard_show_hide(title_cpx, self.word.id, Action.kb_cpx_show) - kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_close()) - return Keyboa.combine(kb_combo) + kb_combo = [] + + if self.word.affixes: + title_djifoa = self.get_title(show=True, items_type="djifoa") + kb_hide_djifoa = keyboard_show_hide( + title_djifoa, self.word.id, Action.kb_afx_show + ) + kb_combo.append(kb_hide_djifoa) + + if self.word.complexes: + title_cpx = self.get_title(show=True, items_type="complex") + kb_hide_cpx = keyboard_show_hide( + title_cpx, self.word.id, Action.kb_cpx_show + ) + kb_combo.append(kb_hide_cpx) + + return kb_combo + @combine_and_close def get_default_kb_for_parentable(self): title_parent = self.get_title(show=True, items_type="parent") kb_hide_parent = keyboard_show_hide( title_parent, self.word.id, Action.kb_pnt_show ) - kb_combo = (kb_hide_parent, kb_close()) - return Keyboa.combine(kb_combo) + return [kb_hide_parent] From 3c81b79f74e58059a51a3285b2786d9630096990 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 11 Dec 2024 21:44:35 +0700 Subject: [PATCH 8/8] Working PROTOTYPE --- app/bot/telegram/keyboards.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/bot/telegram/keyboards.py b/app/bot/telegram/keyboards.py index 36338e1..4842323 100644 --- a/app/bot/telegram/keyboards.py +++ b/app/bot/telegram/keyboards.py @@ -130,13 +130,8 @@ def keyboard_show_hide(title: str, word_id: int, action_mark: str): def combine_and_close(func): def wrapper(self, *args, **kwargs): - # Call the original function to get the kb_combo list kb_combo = func(self, *args, **kwargs) - - # Append kb_close() to the list kb_combo.append(kb_close()) - - # Combine the keyboard buttons return Keyboa.combine(tuple(kb_combo)) return wrapper @@ -248,12 +243,6 @@ def get_kb_afx_show(self): return kb_combo - def get_default_keyboard(self): - if self.word.type.parentable: - return self.get_default_kb_for_parentable() - else: - return self.get_default_kb_for_predy() - @combine_and_close def get_default_kb_for_predy(self): kb_combo = [] @@ -281,3 +270,9 @@ def get_default_kb_for_parentable(self): title_parent, self.word.id, Action.kb_pnt_show ) return [kb_hide_parent] + + def get_default_keyboard(self): + if self.word.type.parentable: + return self.get_default_kb_for_parentable() + else: + return self.get_default_kb_for_predy()