From b2fc78a3b5156cd7ac48a9c0c999da6edbd52d14 Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 29 Apr 2023 11:38:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=BE=20=D0=B8=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82.=20=D0=A3=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=88=D0=B8=D1=80=D0=B8=D0=BD=D0=B0=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=B0=D1=82=D1=83=D1=80=D1=8B=20=D0=B2=204?= =?UTF-8?q?=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/projects.py | 2 +- bot_modules/tasks.py | 6 ++---- bot_sys/keyboard.py | 7 +++++-- template/bd_item.py | 7 +++++++ template/bd_item_delete.py | 12 ++++++++---- template/bd_item_edit.py | 2 +- template/bd_item_select.py | 14 ++------------ template/simple_message.py | 1 - 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 1529ebe..86ec948 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -266,7 +266,7 @@ def RegisterHandlers(dp : Dispatcher): select_handler = 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_delete.DeleteBDItemRegisterHandlers(dp, del_project_button_name, table_name, key_name, ProjectPreDelete, ProjectPostDelete, 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) # Добавление проекта bd_item_add.AddBDItem3RegisterHandlers(dp, \ diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py index c32fbc7..c508baf 100644 --- a/bot_modules/tasks.py +++ b/bot_modules/tasks.py @@ -269,11 +269,9 @@ def RegisterHandlers(dp : Dispatcher): a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'view_task', list_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, a_Prefix, parent_id_field, table_name, key_name, ShowMessageTemplate(task_open_message), GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func, access_mode = user_access.AccessMode.VIEW) - ''' # Удаление задачи - # a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'del_task', add_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) - # bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_task_button_name, table_name, key_name, TaskPreDelete, TaskPostDelete, GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func) - ''' + a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'del_task', del_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.DELETE) + bd_item_delete.DeleteBDItemRegisterHandlers(dp, a_Prefix, bd_item.GetCheckForPrefixFunc(a_Prefix), table_name, key_name, parent_id_field,TaskPreDelete, TaskPostDelete, GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func) # Добавление задачи a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'add_task', add_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index 1baea8e..0a4ac42 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -27,12 +27,15 @@ 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): + return min(max(int(math.sqrt(a_AllKeyCount) // 1), 1), 4) + def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): buttons = [] for b in a_ButtonList: if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): buttons += [types.KeyboardButton(b.label)] - step = max(int(math.sqrt(len(buttons)) // 1), 1) + step = GetKeyInRowCount(len(buttons)) key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True) return key @@ -52,7 +55,7 @@ def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str): buttons = [] for b in a_ButtonList: buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')] - step = max(int(math.sqrt(len(buttons)) // 1), 1) + step = GetKeyInRowCount(len(buttons)) inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step)) return inline_keyboard diff --git a/template/bd_item.py b/template/bd_item.py index 649f9c0..3a30a01 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -22,6 +22,13 @@ item_not_found = 'Элемент {item_id} не найден в таблице { skip_button_name = "⏩ Пропустить" canсel_button_name = "🚫 Отменить" +def HashPrefix(a_Str): + # callback data в сообщении имеет ограниченную длину, поэтому сокращаем префикс + summ = 0 + for i in a_Str: + summ += ord(i) + return f'{summ}:' + class FieldType(Enum): text = 'text' photo = 'photo' diff --git a/template/bd_item_delete.py b/template/bd_item_delete.py index 31d7fb4..033c5de 100644 --- a/template/bd_item_delete.py +++ b/template/bd_item_delete.py @@ -31,9 +31,13 @@ def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDele return simple_message.SimpleMessageTemplate(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode) -def DeleteBDItemRegisterHandlers(dp, a_ButtonName, a_TableName : str, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE): - a_Prefix = f'delete_{a_TableName}_{a_KeyName}:' - sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode) - dp.register_message_handler(sel_handler, text = a_ButtonName) +def DeleteBDItemRegisterHandlers(dp, a_PrevPrefix, a_StartCheckFunc, a_TableName : str, a_KeyName, a_ParentIDFieldName,a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE): + reg_func = dp.register_message_handler + if a_ParentIDFieldName: + reg_func = dp.register_callback_query_handler + + a_Prefix = bd_item.HashPrefix(f'delete_{a_TableName}_{a_KeyName}:') + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode) + reg_func(sel_handler, a_StartCheckFunc) dp.register_callback_query_handler(DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) diff --git a/template/bd_item_edit.py b/template/bd_item_edit.py index faf2df5..e2923f5 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -37,7 +37,7 @@ def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_Messa def EditBDItemRegisterHandlers(dp, a_FSM, a_ButtonName, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName : str, a_KeyName, a_FieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode) - a_Prefix = f'edit_{a_TableName}_{a_KeyName}_{a_FieldName}:' + a_Prefix = bd_item.HashPrefix(f'edit_{a_TableName}_{a_KeyName}_{a_FieldName}:') sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode) dp.register_message_handler(sel_handler, text = a_ButtonName) dp.register_callback_query_handler(StartEditBDItemTemplate(a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) diff --git a/template/bd_item_select.py b/template/bd_item_select.py index 5e5709a..de0c8d4 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -18,18 +18,14 @@ def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_ParentIDFieldName else: items = bd_item.GetAllItemsTemplate(a_TableName)() - print('GetBDItemsListKeyboardButtons', parent_id, a_PrevPrefix, a_ParentIDFieldName, "\n", items) items_button_list = [] for t in items: - print('GetBDItemsListKeyboardButtons items', t) 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) - print(bname, key_value, b) items_button_list += [b] - print(items_button_list) return keyboard.MakeInlineKeyboard(items_button_list, a_NextPrefix) return GetBDItemsListKeyboardButtons @@ -37,15 +33,9 @@ def SelectDBItemTemplate(a_TableName : str, a_ParentIDFieldName, a_GetButtonName keyboard_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) return simple_message.InfoMessageTemplate(a_StartMessage, keyboard_func, a_AccessFunc, access_mode) -def hash_str(a_Str): - summ = 0 - for i in a_Str: - summ += ord(i) - return f'{summ}:' - def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): - a_Prefix = hash_str(f'first_select_{a_TableName}_{a_KeyName}_in_base_{a_PrefixBase}:') + a_Prefix = bd_item.HashPrefix(f'first_select_{a_TableName}_{a_KeyName}_in_base_{a_PrefixBase}:') sel_handler = SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode = access_mode) dp.register_message_handler(sel_handler, text = a_ButtonName) @@ -53,7 +43,7 @@ def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableNam return a_Prefix, sel_handler def NextSelectBDItemRegisterHandlers(dp, a_PrevPrefix, a_ParentIDFieldName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): - a_Prefix = f'next_select_{a_TableName}_{a_KeyName}_{a_ParentIDFieldName}_after_prefix_{a_PrevPrefix}:' + a_Prefix = bd_item.HashPrefix(f'next_select_{a_TableName}_{a_KeyName}_{a_ParentIDFieldName}_after_prefix_{a_PrevPrefix}:') sel_handler = SelectDBItemTemplate(a_TableName, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode = access_mode) dp.register_callback_query_handler(sel_handler, bd_item.GetCheckForPrefixFunc(a_PrevPrefix)) diff --git a/template/simple_message.py b/template/simple_message.py index 6d5b175..b01a6b9 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -49,7 +49,6 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyboard_func(a_Message, user_groups)) if photo_id is None or photo_id == 0 or photo_id == '0': - print(keyboard_func) return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyboard_func(a_Message, user_groups)) await bot.send_photo(user_id, photo_id, msg, reply_markup = keyboard_func(a_Message, user_groups))