diff --git a/bot_modules/projects.py b/bot_modules/projects.py index f8f2b83..c1d667a 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -5,7 +5,7 @@ from bot_sys import bot_bd, log, config, keyboard, user_access from bot_modules import start, access, groups -from template import bd_item_view, simple_message +from template import bd_item_view, simple_message, bd_item_delete from aiogram import Bot, types @@ -189,7 +189,7 @@ def GetButtonNameAndKeyValueAndAccess(a_Item): return a_Item[1], a_Item[4], a_Item[3] async def ShowProject(a_CallbackQuery : types.CallbackQuery, a_Item): - if (len(a_Item) < 3): + if (len(a_Item) < 4): return simple_message.WorkFuncResult(error_find_proj_message) photo_id = a_Item[0] @@ -343,18 +343,15 @@ async def ProjectEditDescLoad(a_Message : types.message, state : FSMContext): # Удаление проекта -async def ProjectDelete(a_Message : types.message): - user_id = str(a_Message.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - await bot.send_message(a_Message.from_user.id, project_select_to_delete_message, reply_markup = GetProjectsListKeyboardButtons(user_groups, delete_project_callback_prefix)) +async def ProjectPreDelete(a_CallbackQuery : types.CallbackQuery, a_Item): + if (len(a_Item) < 4): + return simple_message.WorkFuncResult(error_find_proj_message) + access = a_Item[3] + return simple_message.WorkFuncResult('', None, item_access = access) -async def prjDelete(a_CallbackQuery : types.CallbackQuery): - user_id = str(a_CallbackQuery.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - projectID = str(a_CallbackQuery.data).replace(delete_project_callback_prefix, '') - DelProject(projectID) - log.Success(f'Проект №{projectID} был удалён пользователем {a_CallbackQuery.from_user.id}.') - await bot.send_message(a_CallbackQuery.from_user.id, project_success_delete_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) +async def ProjectPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID): + log.Success(f'Проект №{a_ItemID} был удалён пользователем {a_CallbackQuery.from_user.id}.') + return simple_message.WorkFuncResult(project_success_delete_message) # --------------------------------------------------------- # Работа с базой данных проектов @@ -417,6 +414,8 @@ def RegisterHandlers(dp : Dispatcher): global select_handler select_handler = bd_item_view.SelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowProject, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, GetEditProjectKeyboardButtons) + bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_project_button_name, table_name, key_name, ProjectPreDelete, ProjectPostDelete, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, GetEditProjectKeyboardButtons) + # Добавление проекта dp.register_message_handler(ProjectCreate, text = add_project_button_name) dp.register_message_handler(ProjectPhotoSkip, text = projects_skip_button_name, state = FSMCreateProject.prjPhoto) @@ -437,5 +436,3 @@ def RegisterHandlers(dp : Dispatcher): dp.register_message_handler(ProjectEditNameLoad, state = FSMEditProject.prjName) dp.register_message_handler(ProjectEditDescLoad, state = FSMEditProject.prjDesc) # Удаление проекта - dp.register_message_handler(ProjectDelete, text = del_project_button_name) - dp.register_callback_query_handler(prjDelete, lambda x: x.data.startswith(delete_project_callback_prefix)) diff --git a/template/bd_item_delete.py b/template/bd_item_delete.py new file mode 100644 index 0000000..6901f1f --- /dev/null +++ b/template/bd_item_delete.py @@ -0,0 +1,63 @@ +# -*- coding: utf8 -*- +# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) + +# удаление элемента в БД + +from bot_sys import user_access, bot_bd, keyboard +from bot_modules import access, groups +from template import simple_message, bd_item_select + +from aiogram import types + +item_not_found = 'Элемент {item_id} не найден в таблице {a_TableName}' + +''' +class TableListParam(): + def __init__(self, a_TableName : str, a_KeyName : str, a_GetButtonNameAndKeyValueAndAccessFunc): + self.table_name = a_TableName + self.key_name = a_KeyName + self.get_bname_and_key_value_func = a_GetButtonNameAndKeyValueAndAccessFunc +''' + +def DeleteBDItemInTableTemplate(a_TableName : str, a_KeyName : str): + def DeleteBDItem(a_KeyValue): + return bot_bd.SQLRequestToBD(f'DELETE FROM {a_TableName} WHERE {a_KeyName} = ?', commit = True, return_error = True, param = ([a_KeyValue])) + return DeleteBDItem + +def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, delete = True, access_mode = user_access.AccessMode.DELETE): + async def DeleteBDItem(a_CallbackQuery : types.CallbackQuery): + user_id = str(a_CallbackQuery.from_user.id) + user_groups = groups.GetUserGroupData(user_id) + item_id = str(a_CallbackQuery.data).replace(a_Prefix, '') + item = bd_item_select.GetBDItemsTemplate(a_TableName, a_KeyName)(item_id) + if len(item) < 1: + msg = item_not_found.replace('{item_id}', str(item_id)).replace('{a_TableName}', a_TableName) + log.Error(msg) + return simple_message.WorkFuncResult(msg) + + res_of_pre_del = await a_PreDeleteWorkFunc(a_CallbackQuery, item[0]) + if not delete: + return res_of_pre_del + + if res_of_pre_del is None or res_of_pre_del.string_message is None: + return res_of_pre_del + + if not res_of_pre_del.item_access is None and not user_access.CheckAccessString(res_of_pre_del.item_access, user_groups, access_mode): + return simple_message.WorkFuncResult(access.access_denied_message) + + result, error = DeleteBDItemInTableTemplate(a_TableName, a_KeyName)(item_id) + if not error is None: + msg = error + log.Error(error) + return simple_message.WorkFuncResult(error) + + return await a_PostDeleteWorkFunc(a_CallbackQuery, item_id) + + return simple_message.SimpleMessageTemplate(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + +def DeleteBDItemRegisterHandlers(dp, a_RequestButtonName, 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, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) + dp.register_message_handler(sel_handler, text = a_RequestButtonName) + dp.register_callback_query_handler(DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), lambda x: x.data.startswith(a_Prefix)) + diff --git a/template/bd_item_view.py b/template/bd_item_view.py index 9ebf5a4..a2fcf6d 100644 --- a/template/bd_item_view.py +++ b/template/bd_item_view.py @@ -5,7 +5,7 @@ from bot_sys import user_access, bot_bd, keyboard from bot_modules import access, groups -from template import simple_message, bd_item_select +from template import simple_message, bd_item_select, bd_item_delete from aiogram import types @@ -18,19 +18,7 @@ class TableListParam(): ''' def ShowBDItemTemplate(a_TableName, a_KeyName, a_WorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.VIEW): - async def ShowBDItem(a_CallbackQuery : types.CallbackQuery): - user_id = str(a_CallbackQuery.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - item_id = str(a_CallbackQuery.data).replace(a_Prefix, '') - item = bd_item_select.GetBDItemsTemplate(a_TableName, a_KeyName)(item_id) - if len(item) < 1: - msg = f'Элемент {item_id} не найден в таблице {a_TableName}' - log.Error(msg) - await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup = a_ButtonFunc(user_groups)) - return None, None - - return await a_WorkFunc(a_CallbackQuery, item[0]) - return simple_message.SimpleMessageTemplate(ShowBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + return bd_item_delete.DeleteBDItemTemplate(a_TableName, a_KeyName, a_WorkFunc, None, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, delete = False) def SelectAndShowBDItemRegisterHandlers(dp, a_RequestButtonName, a_TableName : str, a_KeyName, a_ShowItemWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.VIEW): a_Prefix = f'select_{a_TableName}_{a_KeyName}:' diff --git a/template/simple_message.py b/template/simple_message.py index ec6816f..4f6ffa2 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -18,7 +18,6 @@ class WorkFuncResult(): def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): async def GetMessage(a_Message : types.message): - print(a_HelpMessage) return WorkFuncResult(a_HelpMessage) return SimpleMessageTemplate(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode) diff --git a/template/sql_request.py b/template/sql_request.py index bbbb697..f4c7c49 100644 --- a/template/sql_request.py +++ b/template/sql_request.py @@ -11,7 +11,7 @@ from aiogram.dispatcher import FSMContext canсel_button_name = "🚫 Отменить" -request_cancel_message = ''' +cancel_message = ''' 🚫 Запрос к БД отменён ''' @@ -41,7 +41,7 @@ def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc, a_AccessMode): async with state.proxy() as prjData: if a_Message.text == canсel_button_name: await state.finish() - return await a_Message.answer(request_cancel_message, reply_markup = a_GetButtonsFunc(user_groups)) + return await a_Message.answer(cancel_message, reply_markup = a_GetButtonsFunc(user_groups)) sql_request = a_Message.text log.Success(f'Сделан запрос [{sql_request}] пользователем {a_Message.from_user.id}.')