From 1eb755c2958d59fc0ad9658fba18a03848f33a41 Mon Sep 17 00:00:00 2001 From: Alexei Date: Sun, 30 Apr 2023 15:25:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D0=B2?= =?UTF-8?q?=D0=BD=D1=83=D1=82=D1=80=D0=B5=D0=BD=D0=BD=D0=B8=D1=85=20=D1=8D?= =?UTF-8?q?=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=BE=D1=81=D0=BC=D0=BE=D1=82=D1=80=D0=B5?= =?UTF-8?q?.=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D0=B8=D0=BD=D0=BB=D0=B0?= =?UTF-8?q?=D0=B9=D0=BD=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/comments.py | 4 ++++ bot_modules/needs.py | 20 +++++++++++++++++--- bot_modules/projects.py | 17 ++++++++++++++--- bot_modules/tasks.py | 21 ++++++++++++++++++--- bot_sys/keyboard.py | 18 +++++++++++------- template/bd_item_select.py | 6 +++--- 6 files changed, 67 insertions(+), 19 deletions(-) diff --git a/bot_modules/comments.py b/bot_modules/comments.py index e7b28a0..3aafecb 100644 --- a/bot_modules/comments.py +++ b/bot_modules/comments.py @@ -45,6 +45,8 @@ init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}( f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_new}=va', '{user_access.user_access_group_new}=va');" ] +select_comments_prefix = '' + # --------------------------------------------------------- # Сообщения @@ -264,6 +266,8 @@ def RegisterHandlers(dp : Dispatcher): defaul_keyboard_func, \ access_mode = user_access.AccessMode.VIEW\ ) + global select_comments_prefix + select_comments_prefix = a_Prefix # Удаление комментариев a_Prefix = RegisterSelectParent(del_comment_button_name, user_access.AccessMode.DELETE) diff --git a/bot_modules/needs.py b/bot_modules/needs.py index 9e04132..b24dac8 100644 --- a/bot_modules/needs.py +++ b/bot_modules/needs.py @@ -59,6 +59,8 @@ init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}( f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_new}=va', '{user_access.user_access_group_new}=va');" ] +select_needs_prefix = '' + # --------------------------------------------------------- # Сообщения @@ -187,6 +189,13 @@ def GetStartNeedKeyboardButtons(a_Message, a_UserGroups): mods = [start, projects, tasks, comments] return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) +def GetViewItemInlineKeyboardTemplate(a_ItemID): + def GetViewItemInlineKeyboard(a_Message, a_UserGroups): + cur_buttons = [ + keyboard.InlineButton(comments.list_comment_button_name, comments.select_comments_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + ] + return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) + return GetViewItemInlineKeyboard # --------------------------------------------------------- # Обработка сообщений @@ -198,7 +207,7 @@ def GetButtonNameAndKeyValueAndAccess(a_Item): # needName needID needAccess return a_Item[1], a_Item[0], a_Item[4] -def ShowMessageTemplate(a_StringMessage): +def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): if (len(a_Item) < 6): return simple_message.WorkFuncResult(error_find_proj_message) @@ -208,7 +217,10 @@ def ShowMessageTemplate(a_StringMessage): replace(f'#{desc_field}', a_Item[2]).\ replace(f'#{create_datetime_field}', a_Item[5]).\ replace(f'#{access_field}', a_Item[4]) - return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4]) + keyboard_func = None + if keyboard_template_func: + keyboard_func = keyboard_template_func(a_Item[0]) + return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage def SimpleMessageTemplate(a_StringMessage): @@ -294,13 +306,15 @@ def RegisterHandlers(dp : Dispatcher): bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, \ a_Prefix, parent_id_field, \ table_name, key_name, \ - ShowMessageTemplate(need_open_message), \ + ShowMessageTemplate(need_open_message, GetViewItemInlineKeyboardTemplate), \ GetButtonNameAndKeyValueAndAccess, \ select_need_message, \ GetAccess, \ defaul_keyboard_func, \ access_mode = user_access.AccessMode.VIEW\ ) + global select_needs_prefix + select_needs_prefix = a_Prefix # Удаление потребностей a_Prefix = RegisterSelectParent(del_need_button_name, user_access.AccessMode.DELETE) diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 292f7b1..331d0ee 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -181,6 +181,14 @@ def GetStartProjectKeyboardButtons(a_Message, a_UserGroups): mods = [start, tasks, needs, comments] return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) +def GetViewItemInlineKeyboardTemplate(a_ItemID): + def GetViewItemInlineKeyboard(a_Message, a_UserGroups): + cur_buttons = [ + keyboard.InlineButton(tasks.list_task_button_name, tasks.select_tasks_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + ] + return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) + return GetViewItemInlineKeyboard + # --------------------------------------------------------- # Обработка сообщений @@ -192,7 +200,7 @@ def GetButtonNameAndKeyValueAndAccess(a_Item): # projectName projectID projectAccess return a_Item[1], a_Item[0], a_Item[4] -def ShowMessageTemplate(a_StringMessage): +def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): if (len(a_Item) < 6): return simple_message.WorkFuncResult(error_find_proj_message) @@ -202,7 +210,10 @@ def ShowMessageTemplate(a_StringMessage): replace(f'#{desc_field}', a_Item[2]).\ replace(f'#{create_datetime_field}', a_Item[5]).\ replace(f'#{access_field}', a_Item[4]) - return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4]) + keyboard_func = None + if keyboard_template_func: + keyboard_func = keyboard_template_func(a_Item[0]) + return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage def SimpleMessageTemplate(a_StringMessage): @@ -257,7 +268,7 @@ def RegisterHandlers(dp : Dispatcher): # Список проектов dp.register_message_handler(simple_message.SimpleMessageTemplate(ProjectsOpen, defaul_keyboard_func, GetAccess), text = projects_button_name) - bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowMessageTemplate(project_open_message), GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func) + bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowMessageTemplate(project_open_message, GetViewItemInlineKeyboardTemplate), GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func) # Удаление проекта bd_item_delete.DeleteBDItemRegisterHandlers(dp, None, bd_item.GetCheckForTextFunc(del_project_button_name), table_name, key_name, None, ProjectPreDelete, ProjectPostDelete, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func) diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py index 40610b9..de75d02 100644 --- a/bot_modules/tasks.py +++ b/bot_modules/tasks.py @@ -59,6 +59,8 @@ init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}( f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_new}=va', '{user_access.user_access_group_new}=va');" ] +select_tasks_prefix = '' + # --------------------------------------------------------- # Сообщения @@ -187,6 +189,14 @@ def GetStartTaskKeyboardButtons(a_Message, a_UserGroups): mods = [start, projects, needs, comments] return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) +def GetViewItemInlineKeyboardTemplate(a_ItemID): + def GetViewItemInlineKeyboard(a_Message, a_UserGroups): + cur_buttons = [ + keyboard.InlineButton(needs.list_need_button_name, needs.select_needs_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + ] + return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) + return GetViewItemInlineKeyboard + # --------------------------------------------------------- # Обработка сообщений @@ -198,7 +208,7 @@ def GetButtonNameAndKeyValueAndAccess(a_Item): # taskName taskID taskAccess return a_Item[1], a_Item[0], a_Item[4] -def ShowMessageTemplate(a_StringMessage): +def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): if (len(a_Item) < 6): return simple_message.WorkFuncResult(error_find_proj_message) @@ -208,7 +218,10 @@ def ShowMessageTemplate(a_StringMessage): replace(f'#{desc_field}', a_Item[2]).\ replace(f'#{create_datetime_field}', a_Item[5]).\ replace(f'#{access_field}', a_Item[4]) - return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4]) + keyboard_func = None + if keyboard_template_func: + keyboard_func = keyboard_template_func(a_Item[0]) + return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage def SimpleMessageTemplate(a_StringMessage): @@ -280,13 +293,15 @@ def RegisterHandlers(dp : Dispatcher): bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, \ a_Prefix, parent_id_field, \ table_name, key_name, \ - ShowMessageTemplate(task_open_message), \ + ShowMessageTemplate(task_open_message, GetViewItemInlineKeyboardTemplate), \ GetButtonNameAndKeyValueAndAccess, \ select_task_message, \ GetAccess, \ defaul_keyboard_func, \ access_mode = user_access.AccessMode.VIEW\ ) + global select_tasks_prefix + select_tasks_prefix = a_Prefix # Удаление задачи a_Prefix = RegisterSelectParent(del_task_button_name, user_access.AccessMode.DELETE) diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index 0a4ac42..62e98df 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -27,7 +27,7 @@ def Chunks(a_List, a_ChunkSize): for i in range(0, len(a_List), a_ChunkSize): yield a_List[i: i + a_ChunkSize] -def GetKeyInRowCount(a_AllKeyCount): +def GetButtonInRowCount(a_AllKeyCount): return min(max(int(math.sqrt(a_AllKeyCount) // 1), 1), 4) def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): @@ -35,7 +35,7 @@ def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): for b in a_ButtonList: if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): buttons += [types.KeyboardButton(b.label)] - step = GetKeyInRowCount(len(buttons)) + step = GetButtonInRowCount(len(buttons)) key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True) return key @@ -46,16 +46,20 @@ def MakeKeyboardForMods(a_ModList, a_UserGroups): buttons = GetButtons(a_ModList) return MakeKeyboard(buttons, a_UserGroups) -class Button: - def __init__(self, a_Label, a_CallBackData): +class InlineButton: + def __init__(self, a_Label, a_CallBackPrefix, a_CallBackData, a_AccessString, a_AccessMode): self.label = a_Label + self.callback_prefix = a_CallBackPrefix self.callback_data = str(a_CallBackData) + self.access_string = a_AccessString + self.access_mode = a_AccessMode -def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str): +def MakeInlineKeyboard(a_ButtonList : [InlineButton], a_UserGroups): buttons = [] for b in a_ButtonList: - buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')] - step = GetKeyInRowCount(len(buttons)) + if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): + buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{b.callback_prefix}{b.callback_data}')] + step = GetButtonInRowCount(len(buttons)) inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step)) return inline_keyboard diff --git a/template/bd_item_select.py b/template/bd_item_select.py index 97b8e79..e5344e4 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -23,10 +23,10 @@ def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_ParentIDFieldName bname, key_value, access = a_GetButtonNameAndKeyValueAndAccessFunc(t) if access is None: access = '' - if not bname is None and user_access.CheckAccessString(access, a_UserGroups, access_mode): - b = keyboard.Button(bname, key_value) + if bname: + b = keyboard.InlineButton(bname, a_NextPrefix, key_value, access, access_mode) items_button_list += [b] - return keyboard.MakeInlineKeyboard(items_button_list, a_NextPrefix) + return keyboard.MakeInlineKeyboard(items_button_list, a_UserGroups) return GetBDItemsListKeyboardButtons def SelectDBItemTemplate(a_TableName : str, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_NextPrefix, access_mode = user_access.AccessMode.VIEW):