From 4659fc2312a9ad46348fa2dea28c86f3c02babcb Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 29 Apr 2023 11:03:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=BE=20=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D1=85=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B3=D0=BE=D0=B2=20=D0=B8=20=D1=81=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/access.py | 2 -- bot_modules/projects.py | 7 ++----- bot_modules/tasks.py | 8 +++++--- bot_sys/keyboard.py | 2 +- template/bd_item.py | 2 +- template/bd_item_add.py | 29 ++++++++++++++--------------- template/bd_item_edit.py | 4 ++-- template/bd_item_select.py | 22 +++++++++++++++++----- template/simple_message.py | 1 + 9 files changed, 43 insertions(+), 34 deletions(-) diff --git a/bot_modules/access.py b/bot_modules/access.py index 2a823d3..bc76ed7 100644 --- a/bot_modules/access.py +++ b/bot_modules/access.py @@ -17,12 +17,10 @@ class FSMRequestToBDAccess(StatesGroup): class FSMEditAccessItem(StatesGroup): - item_id = State() item_field = State() class FSMEditDefaultAccessItem(StatesGroup): - item_id = State() item_field = State() # --------------------------------------------------------- # БД diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 9f66a6f..1529ebe 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -20,19 +20,15 @@ class FSMCreateProject(StatesGroup): photo = State() class FSMEditPhotoItem(StatesGroup): - item_id = State() item_field = State() class FSMEditNameItem(StatesGroup): - item_id = State() item_field = State() class FSMEditDeskItem(StatesGroup): - item_id = State() item_field = State() class FSMEditAccessItem(StatesGroup): - item_id = State() item_field = State() # --------------------------------------------------------- # БД @@ -216,7 +212,7 @@ def ShowMessageTemplate(a_StringMessage): return ShowMessage def SimpleMessageTemplate(a_StringMessage): - async def ShowMessage(a_CallbackQuery : types.CallbackQuery): + async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -281,6 +277,7 @@ def RegisterHandlers(dp : Dispatcher): SimpleMessageTemplate(project_create_desc_message), \ SimpleMessageTemplate(project_create_photo_message), \ SimpleMessageTemplate(project_success_create_message), \ + None,\ None, \ None, \ name_field, \ diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py index 1ba62e2..c32fbc7 100644 --- a/bot_modules/tasks.py +++ b/bot_modules/tasks.py @@ -215,7 +215,7 @@ def ShowMessageTemplate(a_StringMessage): return ShowMessage def SimpleMessageTemplate(a_StringMessage): - async def ShowMessage(a_CallbackQuery : types.CallbackQuery): + async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -236,7 +236,7 @@ async def TaskPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID): def AddBDItemFunc(a_ItemData, a_UserID): res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})', - commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name) + f";{a_UserID}=+", a_ItemData[desc_field])) + commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name) + f";{a_UserID}=+", a_ItemData[parent_id_field])) if error: log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') @@ -264,7 +264,8 @@ def RegisterHandlers(dp : Dispatcher): defaul_keyboard_func = GetStartTaskKeyboardButtons # Стартовое сообщение dp.register_message_handler(simple_message.SimpleMessageTemplate(TasksOpen, defaul_keyboard_func, GetAccess), text = tasks_button_name) - # Список задач projects. + + # Список задач 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) @@ -287,6 +288,7 @@ def RegisterHandlers(dp : Dispatcher): SimpleMessageTemplate(task_create_desc_message), \ SimpleMessageTemplate(task_create_photo_message), \ SimpleMessageTemplate(task_success_create_message), \ + a_Prefix,\ projects.table_name, \ projects.key_name, \ name_field, \ diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index b045991..1baea8e 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -46,7 +46,7 @@ def MakeKeyboardForMods(a_ModList, a_UserGroups): class Button: def __init__(self, a_Label, a_CallBackData): self.label = a_Label - self.callback_data = a_CallBackData + self.callback_data = str(a_CallBackData) def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str): buttons = [] diff --git a/template/bd_item.py b/template/bd_item.py index 450a8ac..649f9c0 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -34,7 +34,7 @@ def GetCheckForTextFunc(a_Text): def GetKeyDataFromCallbackMessage(a_Message, a_Prefix): key_item_id = None - if hasattr(a_Message, 'data'): + if a_Prefix and hasattr(a_Message, 'data'): key_item_id = str(a_Message.data).replace(a_Prefix, '') return key_item_id diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 341bd52..d770463 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -16,8 +16,6 @@ cancel_message = ''' 🚫 Добавление отменено ''' -parent_id_field_name = 'parent_id' - def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD): async def StartAddBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext): user_id = str(a_CallbackQuery.from_user.id) @@ -28,19 +26,18 @@ def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_ParentTableName, await a_FSMStart.set() async with state.proxy() as item_data: - item_data[parent_id_field_name] = parent_id + if a_ParentKeyFieldName: + item_data[a_ParentKeyFieldName] = parent_id if parent_id: check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_ParentTableName, a_ParentKeyFieldName, parent_id, a_MessageFunc, access_mode)(a_CallbackQuery) else: - res_of_work_func = await a_MessageFunc(a_CallbackQuery) + res_of_work_func = await a_MessageFunc(a_CallbackQuery, None) if not check is None: await state.finish() check.keyboard_func = a_FinishButtonFunc return check - if parent_id: - await a_FSM.next() return res_of_work_func return simple_message.SimpleMessageTemplate(StartAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) @@ -65,13 +62,15 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_P async with state.proxy() as item_data: if a_Message.text == bd_item.canсel_button_name: await state.finish() - return simple_message.WorkFuncResult(cancel_message, keyborad_func = a_FinishButtonFunc) + return simple_message.WorkFuncResult(cancel_message, keyboard_func = a_FinishButtonFunc) - parent_id = item_data[parent_id_field_name] + parent_id = None + if a_ParentKeyFieldName: + parent_id = item_data[a_ParentKeyFieldName] if parent_id: check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_ParentTableName, a_ParentKeyFieldName, parent_id, a_MessageFunc, access_mode)(a_Message) else: - res_of_work_func = await a_MessageFunc(a_Message) + res_of_work_func = await a_MessageFunc(a_Message, None) if not check is None: await state_func() @@ -84,7 +83,7 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_P else: if a_Message.photo == None or len(a_Message.photo) == 0: await state.finish() - return simple_message.WorkFuncResult(error_photo_type_message, keyborad_func = a_FinishButtonFunc) + return simple_message.WorkFuncResult(error_photo_type_message, keyboard_func = a_FinishButtonFunc) field_value = a_Message.photo[0].file_id else: field_value = a_Message.text @@ -98,14 +97,14 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_P return simple_message.SimpleMessageTemplate(FinishAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) -def AddBDItem3RegisterHandlers(dp, a_StartCheckFunc, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_AddBDItemFunc, a_AddNameMessageFunc, a_AddDescMessageFunc, a_AddPhotoMessageFunc, a_FinishMessageFunc, a_ParentTableName : str, a_ParentKeyFieldName, a_NameField, a_DescField, a_PhotoField, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD): +def AddBDItem3RegisterHandlers(dp, a_StartCheckFunc, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_AddBDItemFunc, a_AddNameMessageFunc, a_AddDescMessageFunc, a_AddPhotoMessageFunc, a_FinishMessageFunc, a_ParentPrefix, a_ParentTableName : str, a_ParentKeyFieldName, a_NameField, a_DescField, a_PhotoField, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD): keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode) keyboard_skip_and_cancel = bd_item.GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode) - a_Prefix = f'add_{a_ParentTableName}_{a_ParentKeyFieldName}_{a_NameField}_{a_DescField}_{a_PhotoField}:' + reg_func = dp.register_message_handler if a_ParentTableName: - dp.register_callback_query_handler(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), a_StartCheckFunc) - else: - dp.register_message_handler(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), a_StartCheckFunc) + reg_func = dp.register_callback_query_handler + reg_func(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_ParentPrefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), a_StartCheckFunc) + dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_NameField, a_AddDescMessageFunc, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName) dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_DescField, a_AddPhotoMessageFunc, a_AccessFunc, keyboard_skip_and_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMDesc) dp.register_message_handler(FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo', 'text'], state = a_FSMPhoto) diff --git a/template/bd_item_edit.py b/template/bd_item_edit.py index 2f9e19e..faf2df5 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -20,11 +20,11 @@ error_photo_type_message = ''' ''' def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT): - return bd_item_add.StartAddBDItemTemplate(a_FSM, a_FSM.item_id, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = access_mode) + return bd_item_add.StartAddBDItemTemplate(a_FSM, a_FSM.item_field, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = access_mode) def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): def EditBDItemFunc(a_ItemData, a_UserID): - item_id = a_ItemData[bd_item_add.parent_id_field_name] + item_id = a_ItemData[a_KeyName] field_value = a_ItemData[a_FieldName] res, error = bd_item.EditBDItemInTableTemplate(a_TableName, a_KeyName, a_FieldName)(item_id, field_value) if error: diff --git a/template/bd_item_select.py b/template/bd_item_select.py index 45c3868..5e5709a 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -18,30 +18,42 @@ 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): - items_button_list += [keyboard.Button(bname, key_value)] + 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 def SelectDBItemTemplate(a_TableName : str, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_NextPrefix, access_mode = user_access.AccessMode.VIEW): - keyborad_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) - return simple_message.InfoMessageTemplate(a_StartMessage, keyborad_func, a_AccessFunc, access_mode) + 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 = f'{a_PrefixBase}select_{a_TableName}_{a_KeyName}:' + a_Prefix = hash_str(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) 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'{a_PrevPrefix}_select_{a_TableName}_{a_KeyName}_{a_ParentIDFieldName}:' + a_Prefix = 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 b01a6b9..6d5b175 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -49,6 +49,7 @@ 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))