From 7f360b36b35229a56680f9bff6b35754fef91fc9 Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 29 Apr 2023 09:38:26 +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=A8=D0=B0=D0=B1=D0=BB=D0=BE?= =?UTF-8?q?=D0=BD=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D0=B5=D0=B4=D1=91=D0=BD=20=D0=BD=D0=B0=20=D1=88=D0=B0=D0=B1?= =?UTF-8?q?=D0=BB=D0=BE=D0=BD=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/projects.py | 8 +++--- bot_modules/start.py | 4 +-- bot_modules/tasks.py | 8 +++--- template/bd_item.py | 2 +- template/bd_item_add.py | 10 +++---- template/bd_item_edit.py | 68 ++++++++++------------------------------------ template/simple_message.py | 16 +++++------ 7 files changed, 38 insertions(+), 78 deletions(-) diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 3d7ecf2..9f66a6f 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -235,14 +235,14 @@ async def ProjectPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID): # --------------------------------------------------------- # Работа с базой данных проектов -def AddBDItemFunc(a_ItemData): +def AddBDItemFunc(a_ItemData, a_UserID): res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_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))) + 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}=+")) if error: - log.Error(f'Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') + log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') else: - log.Success(f'Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') + log.Success(f'Пользоватлель {a_UserID}. Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') return res, error diff --git a/bot_modules/start.py b/bot_modules/start.py index a4e06bd..d8ee9e3 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -4,7 +4,7 @@ # Стартовое меню from bot_sys import log, config, keyboard, user_access -from bot_modules import profile, projects, groups, access, backup, tasks +from bot_modules import profile, projects, groups, access, backup from template import simple_message from aiogram.dispatcher import Dispatcher @@ -31,7 +31,7 @@ start_menu_button_name = "☰ Главное меню" # Работа с кнопками def GetStartKeyboardButtons(a_Message, a_UserGroups): - mods = [profile, projects, groups, access, backup, tasks] + mods = [profile, projects, groups, access, backup] return keyboard.MakeKeyboardForMods(mods, a_UserGroups) # --------------------------------------------------------- diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py index 65e9d40..1ba62e2 100644 --- a/bot_modules/tasks.py +++ b/bot_modules/tasks.py @@ -234,14 +234,14 @@ async def TaskPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID): # --------------------------------------------------------- # Работа с базой данных задач -def AddBDItemFunc(a_ItemData): +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), 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[desc_field])) if error: - log.Error(f'Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') + log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') else: - log.Success(f'Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') + log.Success(f'Пользоватлель {a_UserID}. Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).') return res, error diff --git a/template/bd_item.py b/template/bd_item.py index c21e659..450a8ac 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -5,7 +5,7 @@ from enum import Enum from bot_sys import user_access, bot_bd, keyboard, log -from bot_modules import groups +from bot_modules import groups, access from template import simple_message from aiogram import types diff --git a/template/bd_item_add.py b/template/bd_item_add.py index ac647ad..341bd52 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -18,7 +18,7 @@ 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.EDIT): +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) user_groups = groups.GetUserGroupData(user_id) @@ -44,8 +44,8 @@ def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_ParentTableName, return res_of_work_func return simple_message.SimpleMessageTemplate(StartAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) -def FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): - return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, True, access_mode = access_mode, field_type = field_type) +def FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): + return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, True, access_mode = access_mode, field_type = field_type) def NextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, False, access_mode = access_mode, field_type = field_type) @@ -90,7 +90,7 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_P field_value = a_Message.text item_data[a_FieldName] = field_value if a_Finish: - res, error = a_AddBDItemFunc(item_data) + res, error = a_AddBDItemFunc(item_data, user_id) await state_func() if error: return simple_message.WorkFuncResult(error) @@ -108,4 +108,4 @@ def AddBDItem3RegisterHandlers(dp, a_StartCheckFunc, a_FSM, a_FSMName, a_FSMDesc 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) 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, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo', 'text'], state = a_FSMPhoto) + 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 8df985c..2f9e19e 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -5,14 +5,12 @@ from bot_sys import user_access, bot_bd, log from bot_modules import access, groups -from template import simple_message, bd_item_select, bd_item +from template import simple_message, bd_item_select, bd_item, bd_item_add from aiogram import types from aiogram.dispatcher import FSMContext from aiogram.dispatcher.filters.state import State, StatesGroup -#TODO: Переделать на FinishOrNextAddBDItemTemplate StartAddBDItemTemplate - cancel_message = ''' 🚫 Редактирование отменено ''' @@ -21,66 +19,28 @@ error_photo_type_message = ''' 🚫 Фотографий не найдено ''' -def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT): - async def StartEditBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext): - user_id = str(a_CallbackQuery.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - await a_FSM.item_id.set() - item_id = str(a_CallbackQuery.data).replace(a_Prefix, '') - res_of_work_func = None - async with state.proxy() as item_data: - item_data['item_id'] = item_id - - check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode)(a_CallbackQuery) - - if not check is None: - await state.finish() - return check - await a_FSM.next() - return res_of_work_func - return simple_message.SimpleMessageTemplate(StartEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode) +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) 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): - async def FinishEditBDItem(a_Message : types.CallbackQuery, state : FSMContext): - user_id = str(a_Message.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - error = None - res_of_work_func = None - 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) - - item_id = item_data['item_id'] - check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode)(a_Message) - - if not check is None: - await state.finish() - return check - - field_value = '' - if field_type == bd_item.FieldType.photo: - if a_Message.photo == None or len(a_Message.photo) == 0: - await state.finish() - return simple_message.WorkFuncResult(error_photo_type_message) - field_value = a_Message.photo[0].file_id - else: - field_value = a_Message.text - res, error = bd_item.EditBDItemInTableTemplate(a_TableName, a_KeyName, a_FieldName)(item_id, field_value) - log.Success(f'Изменено поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Пользователь {user_id}.') - await state.finish() + def EditBDItemFunc(a_ItemData, a_UserID): + item_id = a_ItemData[bd_item_add.parent_id_field_name] + field_value = a_ItemData[a_FieldName] + res, error = bd_item.EditBDItemInTableTemplate(a_TableName, a_KeyName, a_FieldName)(item_id, field_value) if error: - return simple_message.WorkFuncResult(error) - return res_of_work_func - - return simple_message.SimpleMessageTemplate(FinishEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + log.Error(f'Пользователю {a_UserID} не удалось изменить поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Ошибка: {error}') + else: + log.Success(f'Пользователь {a_UserID} изменил поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}.') + return res, error + + return bd_item_add.FinishAddBDItemTemplate(a_FSM, EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type) 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}:' 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_FieldName, a_Prefix, a_AccessFunc, keyboard_cancel, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) + 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)) if field_type == bd_item.FieldType.photo: dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field) else: diff --git a/template/simple_message.py b/template/simple_message.py index 1b571c0..b01a6b9 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -11,11 +11,11 @@ from aiogram import Bot bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML) class WorkFuncResult(): - def __init__(self, a_StringMessage : str, photo_id = None, item_access = None, keyborad_func = None): + def __init__(self, a_StringMessage : str, photo_id = None, item_access = None, keyboard_func = None): self.string_message = a_StringMessage self.photo_id = photo_id self.item_access = item_access - self.keyborad_func = keyborad_func + self.keyboard_func = keyboard_func def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): @@ -35,9 +35,9 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod if res is None: return - keyborad_func = a_GetButtonsFunc - if res.keyborad_func: - keyborad_func = res.keyborad_func + keyboard_func = a_GetButtonsFunc + if res.keyboard_func: + keyboard_func = res.keyboard_func msg = res.string_message if msg is None: @@ -46,10 +46,10 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod photo_id = res.photo_id if not res.item_access is None and not user_access.CheckAccessString(res.item_access, user_groups, access_mode): - return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyborad_func(a_Message, user_groups)) + 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': - return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyborad_func(a_Message, user_groups)) + 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 = keyborad_func(a_Message, user_groups)) + await bot.send_photo(user_id, photo_id, msg, reply_markup = keyboard_func(a_Message, user_groups)) return SimpleMessage