From f5cb6f1c6a4ea361705b9e2add21d034c0dc8c63 Mon Sep 17 00:00:00 2001 From: Alexei Date: Thu, 27 Apr 2023 21:02:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=20=D0=B2?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=BC=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=B6=D0=B5=D0=BD=D0=B8=D0=B8.=20=D0=9F?= =?UTF-8?q?=D0=BE=D0=BA=D0=B0=20=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D1=8E=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/access.py | 7 +- bot_modules/backup.py | 2 +- bot_modules/groups.py | 2 +- bot_modules/profile.py | 2 +- bot_modules/projects.py | 32 ++--- bot_modules/start.py | 2 +- bot_modules/tasks.py | 284 +++++++++++++++++++++++++++++++++++++++++++++ template/bd_item.py | 4 +- template/bd_item_add.py | 2 +- template/bd_item_delete.py | 2 +- template/bd_item_edit.py | 2 +- template/bd_item_select.py | 13 ++- template/bd_item_view.py | 21 +++- template/file_message.py | 6 +- template/simple_message.py | 8 +- template/sql_request.py | 12 +- 16 files changed, 355 insertions(+), 46 deletions(-) create mode 100644 bot_modules/tasks.py diff --git a/bot_modules/access.py b/bot_modules/access.py index 79d7c68..2a823d3 100644 --- a/bot_modules/access.py +++ b/bot_modules/access.py @@ -109,7 +109,7 @@ moduleaccess_success_edit_message = '''✅ Проект успешно отре # --------------------------------------------------------- # Работа с кнопками -def GetEditAccessKeyboardButtons(a_UserGroups): +def GetEditAccessKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(sql_request_button_name, user_access.AccessMode.ACCEES_EDIT, GetAccess()), keyboard.ButtonWithAccess(edit_moduleaccess_access_button_name, user_access.AccessMode.ACCEES_EDIT, GetAccess()), @@ -185,5 +185,6 @@ def RegisterHandlers(dp : Dispatcher): sql_request.RequestToBDRegisterHandlers(dp, sql_request_button_name, request_start_message, FSMRequestToBDAccess, defaul_keyboard_func, user_access.AccessMode.ACCEES_EDIT, GetAccess) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditAccessItem, edit_moduleaccess_access_button_name, moduleaccess_select_to_edit_message, ShowMessageTemplate(moduleaccess_edit_access_message), ShowMessageTemplate(moduleaccess_success_edit_message), table_name, mod_name_field, moduleaccess_field, GetButtonNameAndKeyValueAndAccess, GetAccess, defaul_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDefaultAccessItem, edit_moduleaccess_default_access_button_name, moduleaccess_select_to_edit_message, ShowMessageTemplate(moduleaccess_edit_default_access_message), ShowMessageTemplate(moduleaccess_success_edit_message), table_name, mod_name_field, mod_default_access_field, GetButtonNameAndKeyValueAndAccess, GetAccess, defaul_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) + edit_keyboard_func = defaul_keyboard_func + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditAccessItem, edit_moduleaccess_access_button_name, moduleaccess_select_to_edit_message, ShowMessageTemplate(moduleaccess_edit_access_message), ShowMessageTemplate(moduleaccess_success_edit_message), table_name, mod_name_field, moduleaccess_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDefaultAccessItem, edit_moduleaccess_default_access_button_name, moduleaccess_select_to_edit_message, ShowMessageTemplate(moduleaccess_edit_default_access_message), ShowMessageTemplate(moduleaccess_success_edit_message), table_name, mod_name_field, mod_default_access_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) diff --git a/bot_modules/backup.py b/bot_modules/backup.py index 8184d7a..0fe5e61 100644 --- a/bot_modules/backup.py +++ b/bot_modules/backup.py @@ -51,7 +51,7 @@ backup_log_button_name = "📃 Логи" # --------------------------------------------------------- # Работа с кнопками -def GetBackupKeyboardButtons(a_UserGroups): +def GetBackupKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(backup_bd_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(backup_log_button_name, user_access.AccessMode.EDIT, GetAccess()) diff --git a/bot_modules/groups.py b/bot_modules/groups.py index d6a22aa..68e386e 100644 --- a/bot_modules/groups.py +++ b/bot_modules/groups.py @@ -80,7 +80,7 @@ help_button_name = "📄 Информация по группам" # --------------------------------------------------------- # Работа с кнопками -def GetEditGroupKeyboardButtons(a_UserGroups): +def GetEditGroupKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(sql_request_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(help_button_name, user_access.AccessMode.VIEW, GetAccess()) diff --git a/bot_modules/profile.py b/bot_modules/profile.py index b90d5a6..477fb44 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -57,7 +57,7 @@ user_profile_button_name = "📰 Профиль" # --------------------------------------------------------- # Работа с кнопками -def GetStartKeyboardButtons(a_UserGroups): +def GetStartKeyboardButtons(a_Message, a_UserGroups): mods = [start] return keyboard.MakeKeyboardForMods(mods, a_UserGroups) diff --git a/bot_modules/projects.py b/bot_modules/projects.py index d1a2d63..12d2182 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -118,13 +118,13 @@ project_select_to_edit_message = ''' Выберите проект, который вы хотите отредактировать. ''' -edit_project_photo_button_name = "☐ Изменить изображение" +edit_project_photo_button_name = "☐ Изменить изображение в проекте" project_edit_photo_message = ''' Загрузите новую обложку для проекта (Фото): Она будет отображаться в его описании. ''' -edit_project_name_button_name = "≂ Изменить название" +edit_project_name_button_name = "≂ Изменить название в проекте" project_edit_name_message = f''' Текущее название проекта: #{name_field} @@ -132,7 +132,7 @@ project_edit_name_message = f''' Введите новое название проекта: ''' -edit_project_desc_button_name = "𝌴 Изменить описание" +edit_project_desc_button_name = "𝌴 Изменить описание в проекте" project_edit_desc_message = f''' Текущее описание проекта: #{desc_field} @@ -140,7 +140,7 @@ project_edit_desc_message = f''' Введите новое описание проекта: ''' -edit_project_access_button_name = "✋ Изменить доступ" +edit_project_access_button_name = "✋ Изменить доступ к проекту" project_edit_access_message = f''' Текущий доступ к проекту: #{access_field} @@ -165,7 +165,7 @@ project_success_delete_message = '''✅ Проект успешно удалён # --------------------------------------------------------- # Работа с кнопками -def GetEditProjectKeyboardButtons(a_UserGroups): +def GetEditProjectKeyboardButtons(a_Message, a_UserGroups): cur_buttons = GetModuleButtons() + [ keyboard.ButtonWithAccess(edit_project_photo_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(edit_project_name_button_name, user_access.AccessMode.EDIT, GetAccess()), @@ -175,7 +175,7 @@ def GetEditProjectKeyboardButtons(a_UserGroups): mods = [start] return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) -def GetStartProjectKeyboardButtons(a_UserGroups): +def GetStartProjectKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(list_project_button_name, user_access.AccessMode.VIEW, GetAccess()), keyboard.ButtonWithAccess(add_project_button_name, user_access.AccessMode.ADD, GetAccess()), @@ -193,7 +193,7 @@ select_handler = 0 async def ProjectsOpen(a_Message : types.message, state = None): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) - await a_Message.answer(base_project_message, reply_markup = GetStartProjectKeyboardButtons(user_groups)) + await a_Message.answer(base_project_message, reply_markup = GetStartProjectKeyboardButtons(a_Message, user_groups)) await select_handler(a_Message) return None @@ -261,22 +261,24 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): + defaul_keyboard_func = GetStartProjectKeyboardButtons # Список проектов - dp.register_message_handler(simple_message.SimpleMessageTemplate(ProjectsOpen, GetStartProjectKeyboardButtons, GetAccess), text = projects_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplate(ProjectsOpen, defaul_keyboard_func, GetAccess), text = projects_button_name) global select_handler - select_handler = bd_item_view.SelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowMessageTemplate(project_open_message), GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, GetStartProjectKeyboardButtons) + select_handler = bd_item_view.SelectAndShowBDItemRegisterHandlers(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, GetStartProjectKeyboardButtons) + 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_add.AddBDItem3RegisterHandlers(dp, FSMCreateProject, FSMCreateProject.name, FSMCreateProject.desc, FSMCreateProject.photo, add_project_button_name, AddBDItemFunc, SimpleMessageTemplate(project_create_name_message), SimpleMessageTemplate(project_create_desc_message), SimpleMessageTemplate(project_create_photo_message), SimpleMessageTemplate(project_success_create_message), table_name, key_name, name_field, desc_field, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetStartProjectKeyboardButtons) + edit_keyboard_func = GetEditProjectKeyboardButtons # Редактирование проекта - dp.register_message_handler(simple_message.InfoMessageTemplate(project_start_edit_message, GetEditProjectKeyboardButtons, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditPhotoItem, edit_project_photo_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_photo_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.photo) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditNameItem, edit_project_name_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_name_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, name_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDeskItem, edit_project_desc_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_desc_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, desc_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) - bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditAccessItem, edit_project_access_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_access_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, access_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) + dp.register_message_handler(simple_message.InfoMessageTemplate(project_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditPhotoItem, edit_project_photo_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_photo_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.photo) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditNameItem, edit_project_name_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_name_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, name_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDeskItem, edit_project_desc_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_desc_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, desc_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditAccessItem, edit_project_access_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_access_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, access_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) diff --git a/bot_modules/start.py b/bot_modules/start.py index 80ce400..d8ee9e3 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -30,7 +30,7 @@ start_menu_button_name = "☰ Главное меню" # --------------------------------------------------------- # Работа с кнопками -def GetStartKeyboardButtons(a_UserGroups): +def GetStartKeyboardButtons(a_Message, a_UserGroups): mods = [profile, projects, groups, access, backup] return keyboard.MakeKeyboardForMods(mods, a_UserGroups) diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py new file mode 100644 index 0000000..9328deb --- /dev/null +++ b/bot_modules/tasks.py @@ -0,0 +1,284 @@ +# -*- coding: utf8 -*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +# Задачи + +from bot_sys import bot_bd, log, keyboard, user_access +from bot_modules import start, access, groups +from template import bd_item_view, simple_message, bd_item_delete, bd_item_edit, bd_item, bd_item_add + +from aiogram import types + +from aiogram.dispatcher import FSMContext +from aiogram.dispatcher.filters.state import State, StatesGroup +from aiogram.dispatcher import Dispatcher +import sqlite3 + +class FSMCreateTask(StatesGroup): + name = State() + desc = State() + 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() +# --------------------------------------------------------- +# БД +module_name = 'tasks' + +table_name = module_name +key_name = 'taskID' +name_field = 'taskName' +desc_field = 'taskDesc' +photo_field = 'taskPhoto' +access_field = 'taskAccess' +create_datetime_field = 'taskCreateDateTime' + +init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}( + {key_name} INTEGER PRIMARY KEY, + {name_field} TEXT, + {desc_field} TEXT, + {photo_field} TEXT, + {access_field} TEXT, + {create_datetime_field} TEXT + )''', +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');" +] + +# --------------------------------------------------------- +# Сообщения + +tasks_button_name = "🟥 Задачи" +base_task_message = ''' +🟥 Задачи + +''' + +list_task_button_name = "📃 Список задач" +select_task_message = ''' +Пожалуйста, выберите задачу: +''' + +error_find_proj_message = ''' +❌ Ошибка, задача не найдена +''' + +task_open_message = f''' +Задача: #{name_field} + +#{desc_field} + +Время создания: #{create_datetime_field} +''' + +# Создание задачи + +add_task_button_name = "✅ Добавить задачу" +task_create_name_message = ''' +Создание задачи. Шаг №1 + +Введите название задачи: +''' + +task_create_desc_message = ''' +Создание задачи. Шаг №2 + +Введите описание задачи: +''' + +task_create_photo_message = ''' +Создание задачи. Шаг №3 + +Загрузите обложку для задачи (Фото): +Она будет отображаться в его описании. +''' + +task_success_create_message = '''✅ Задача успешно добавлена!''' + +# Редактирование задачи. + +edit_task_button_name = "🛠 Редактировать задачу" +task_start_edit_message= ''' +Пожалуйста, выберите действие: +''' + +task_select_to_edit_message = ''' +Выберите задачу, которую вы хотите отредактировать. +''' + +edit_task_photo_button_name = "☐ Изменить изображение у задачи" +task_edit_photo_message = ''' +Загрузите новую обложку для задачи (Фото): +Она будет отображаться в её описании. +''' + +edit_task_name_button_name = "≂ Изменить название у задачи" +task_edit_name_message = f''' +Текущее название задачи: +#{name_field} + +Введите новое название задачи: +''' + +edit_task_desc_button_name = "𝌴 Изменить описание у задачи" +task_edit_desc_message = f''' +Текущее описание задачи: +#{desc_field} + +Введите новое описание задачи: +''' + +edit_task_access_button_name = "✋ Изменить доступ к задаче" +task_edit_access_message = f''' +Текущий доступ к задаче: +#{access_field} + +{user_access.user_access_readme} + +Введите новую строку доступа: +''' + +task_success_edit_message = '''✅ Задача успешно отредактирован!''' + +# Удаление задачи + +del_task_button_name = "❌ Удалить задачу" +task_select_to_delete_message = ''' +Выберите задача, который вы хотите удалить. +Все потребности в этой задачае так же будут удалены! +''' + +task_success_delete_message = '''✅ Задача успешно удалена!''' + +# --------------------------------------------------------- +# Работа с кнопками + +def GetEditTaskKeyboardButtons(a_Message, a_UserGroups): + cur_buttons = GetModuleButtons() + [ + keyboard.ButtonWithAccess(edit_task_photo_button_name, user_access.AccessMode.EDIT, GetAccess()), + keyboard.ButtonWithAccess(edit_task_name_button_name, user_access.AccessMode.EDIT, GetAccess()), + keyboard.ButtonWithAccess(edit_task_desc_button_name, user_access.AccessMode.EDIT, GetAccess()), + keyboard.ButtonWithAccess(edit_task_access_button_name, user_access.AccessMode.ACCEES_EDIT, GetAccess()), + ] + mods = [start] + return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) + +def GetStartTaskKeyboardButtons(a_Message, a_UserGroups): + cur_buttons = [ + keyboard.ButtonWithAccess(list_task_button_name, user_access.AccessMode.VIEW, GetAccess()), + keyboard.ButtonWithAccess(add_task_button_name, user_access.AccessMode.ADD, GetAccess()), + keyboard.ButtonWithAccess(del_task_button_name, user_access.AccessMode.DELETE, GetAccess()), + keyboard.ButtonWithAccess(edit_task_button_name, user_access.AccessMode.EDIT, GetAccess()) + ] + mods = [start] + return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) + +# --------------------------------------------------------- +# Обработка сообщений + +select_handler = 0 +# стартовое сообщение +async def TasksOpen(a_Message : types.message, state = None): + user_id = str(a_Message.from_user.id) + user_groups = groups.GetUserGroupData(user_id) + await a_Message.answer(base_task_message, reply_markup = GetStartTaskKeyboardButtons(a_Message, user_groups)) + await select_handler(a_Message) + return None + +def GetButtonNameAndKeyValueAndAccess(a_Item): + # taskName taskID taskAccess + return a_Item[1], a_Item[0], a_Item[4] + +def ShowMessageTemplate(a_StringMessage): + async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): + if (len(a_Item) < 6): + return simple_message.WorkFuncResult(error_find_proj_message) + + msg = a_StringMessage.\ + replace(f'#{name_field}', a_Item[1]).\ + 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]) + return ShowMessage + +def SimpleMessageTemplate(a_StringMessage): + async def ShowMessage(a_CallbackQuery : types.CallbackQuery): + return simple_message.WorkFuncResult(a_StringMessage) + return ShowMessage + +# Удаление задачи + +async def TaskPreDelete(a_CallbackQuery : types.CallbackQuery, a_Item): + if (len(a_Item) < 6): + return simple_message.WorkFuncResult(error_find_proj_message) + access = a_Item[4] + return simple_message.WorkFuncResult('', None, item_access = access) + +async def TaskPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID): + log.Success(f'Задача №{a_ItemID} была удалена пользователем {a_CallbackQuery.from_user.id}.') + return simple_message.WorkFuncResult(task_success_delete_message) + +# --------------------------------------------------------- +# Работа с базой данных задач + +def AddBDItemFunc(a_ItemData): + 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))) + + if error: + log.Error(f'Ошибка добавления записи в таблицу {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)}).') + + return res, error + +# --------------------------------------------------------- +# API + +# Инициализация БД +def GetInitBDCommands(): + return init_bd_cmds + +def GetAccess(): + return access.GetAccessForModule(module_name) + +# Доступные кнопки +def GetModuleButtons(): + return [keyboard.ButtonWithAccess(tasks_button_name, user_access.AccessMode.VIEW, GetAccess())] + +# Обработка кнопок +def RegisterHandlers(dp : Dispatcher): + defaul_keyboard_func = GetStartTaskKeyboardButtons + + # Список задач + dp.register_message_handler(simple_message.SimpleMessageTemplate(TasksOpen, defaul_keyboard_func, GetAccess), text = tasks_button_name) + global select_handler + select_handler = bd_item_view.SelectAndShowBDItemRegisterHandlers(dp, list_task_button_name, table_name, key_name, ShowMessageTemplate(task_open_message), GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func) + + # Удаление задачи + bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_task_button_name, table_name, key_name, TaskPreDelete, TaskPostDelete, GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func) + + # Добавление задачи + bd_item_add.AddBDItem3RegisterHandlers(dp, FSMCreateTask, FSMCreateTask.name, FSMCreateTask.desc, FSMCreateTask.photo, add_task_button_name, AddBDItemFunc, SimpleMessageTemplate(task_create_name_message), SimpleMessageTemplate(task_create_desc_message), SimpleMessageTemplate(task_create_photo_message), SimpleMessageTemplate(task_success_create_message), table_name, key_name, name_field, desc_field, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, defaul_keyboard_func) + + edit_keyboard_func = GetEditTaskKeyboardButtons + # Редактирование задачи + dp.register_message_handler(simple_message.InfoMessageTemplate(task_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_task_button_name) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditPhotoItem, edit_task_photo_button_name, task_select_to_edit_message, ShowMessageTemplate(task_edit_photo_message), ShowMessageTemplate(task_success_edit_message), table_name, key_name, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.photo) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditNameItem, edit_task_name_button_name, task_select_to_edit_message, ShowMessageTemplate(task_edit_name_message), ShowMessageTemplate(task_success_edit_message), table_name, key_name, name_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDeskItem, edit_task_desc_button_name, task_select_to_edit_message, ShowMessageTemplate(task_edit_desc_message), ShowMessageTemplate(task_success_edit_message), table_name, key_name, desc_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text) + bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditAccessItem, edit_task_access_button_name, task_select_to_edit_message, ShowMessageTemplate(task_edit_access_message), ShowMessageTemplate(task_success_edit_message), table_name, key_name, access_field, GetButtonNameAndKeyValueAndAccess, GetAccess, edit_keyboard_func, access_mode = user_access.AccessMode.ACCEES_EDIT, field_type = bd_item.FieldType.text) + diff --git a/template/bd_item.py b/template/bd_item.py index fc3d9f1..c0e68ce 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -27,7 +27,7 @@ class FieldType(Enum): photo = 'photo' def GetCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): - def GetCancelKeyboardButtons(a_UserGroups): + def GetCancelKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()), ] @@ -35,7 +35,7 @@ def GetCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): return GetCancelKeyboardButtons def GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): - def GetSkipAndCancelKeyboardButtons(a_UserGroups): + def GetSkipAndCancelKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ keyboard.ButtonWithAccess(skip_button_name, a_AccessMode, a_AccessFunc()), keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()), diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 3da3527..4e18787 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -101,7 +101,7 @@ def AddBDItem3RegisterHandlers(dp, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_Bu 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_TableName}_{a_KeyName}_{a_NameField}_{a_DescField}_{a_PhotoField}:' - # sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) + # sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode) # dp.register_message_handler(sel_handler, text = a_ButtonName) dp.register_message_handler(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_TableName, a_KeyName, a_NameField, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), text = a_ButtonName) dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_TableName, a_KeyName, a_NameField, a_AddDescMessageFunc, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName) diff --git a/template/bd_item_delete.py b/template/bd_item_delete.py index 40a7d96..957ed99 100644 --- a/template/bd_item_delete.py +++ b/template/bd_item_delete.py @@ -33,7 +33,7 @@ def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDele 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, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, 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(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_edit.py b/template/bd_item_edit.py index 5229abf..1d20f03 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -78,7 +78,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}:' - sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, 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), lambda x: x.data.startswith(a_Prefix)) if field_type == bd_item.FieldType.photo: diff --git a/template/bd_item_select.py b/template/bd_item_select.py index 13bf91e..9624b50 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -9,8 +9,11 @@ from template import simple_message, bd_item from aiogram import types -def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_Prefix : str, a_GetButtonNameAndKeyValueAndAccessFunc, access_mode = user_access.AccessMode.VIEW): - def GetBDItemsListKeyboardButtons(a_UserGroups): +def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_PrevPrefix, a_NextPrefix : str, a_GetButtonNameAndKeyValueAndAccessFunc, access_mode = user_access.AccessMode.VIEW): + def GetBDItemsListKeyboardButtons(a_Message, a_UserGroups): + #if a_PrevPrefix: + + items = bd_item.GetAllItemsTemplate(a_TableName)() items_button_list = [] for t in items: @@ -19,10 +22,10 @@ def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_Prefix : str, a_G access = '' if not bname is None and user_access.CheckAccessString(access, a_UserGroups, access_mode): items_button_list += [keyboard.Button(bname, key_value)] - return keyboard.MakeInlineKeyboard(items_button_list, a_Prefix) + return keyboard.MakeInlineKeyboard(items_button_list, a_NextPrefix) return GetBDItemsListKeyboardButtons -def SelectDBItemTemplate(a_TableName : str, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode = user_access.AccessMode.VIEW): - keyborad_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_Prefix, a_GetButtonNameAndKeyValueAndAccessFunc) +def SelectDBItemTemplate(a_TableName : str, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_NextPrefix, access_mode = user_access.AccessMode.VIEW): + keyborad_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) return simple_message.InfoMessageTemplate(a_StartMessage, keyborad_func, a_AccessFunc, access_mode) diff --git a/template/bd_item_view.py b/template/bd_item_view.py index 7380a8e..a980f47 100644 --- a/template/bd_item_view.py +++ b/template/bd_item_view.py @@ -14,8 +14,27 @@ def ShowBDItemTemplate(a_TableName, a_KeyName, a_WorkFunc, a_Prefix, a_AccessFun def SelectAndShowBDItemRegisterHandlers(dp, a_ButtonName, 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}:' - sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode = access_mode) dp.register_message_handler(sel_handler, text = a_ButtonName) dp.register_callback_query_handler(ShowBDItemTemplate(a_TableName, a_KeyName, a_ShowItemWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), lambda x: x.data.startswith(a_Prefix)) return sel_handler + +def StartSelectBDItemRegisterHandlers(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}:' + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, 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 + +def NextSelectBDItemRegisterHandlers(dp, a_PrevPrefix, a_ButtonName, 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}:' + + sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode = access_mode) + dp.register_message_handler(sel_handler, text = a_ButtonName) + + return a_Prefix + +def ShowBDItemRegisterHandlers(dp, a_PrevPrefix, a_TableName : str, a_KeyName, a_ShowItemWorkFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.VIEW): + dp.register_callback_query_handler(ShowBDItemTemplate(a_TableName, a_KeyName, a_ShowItemWorkFunc, a_PrevPrefix, a_AccessFunc, a_ButtonFunc, access_mode = access_mode), lambda x: x.data.startswith(a_PrevPrefix)) + diff --git a/template/file_message.py b/template/file_message.py index 117bb81..aa69670 100644 --- a/template/file_message.py +++ b/template/file_message.py @@ -16,13 +16,13 @@ def BackupFileTemplate(a_Path, a_CaptionMessage, a_AccessFunc, a_ButtonFunc, a_E user_id = str(a_Message.from_user.id) user_groups= groups.GetUserGroupData(user_id) if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.EDIT): - return await a_Message.answer(access.access_denied_message, reply_markup = a_ButtonFunc(user_groups)) + return await a_Message.answer(access.access_denied_message, reply_markup = a_ButtonFunc(a_Message, user_groups)) document = await GetFile(a_Path) if document is None: - return await a_Message.answer(user_id, error_backup_message, reply_markup = a_ButtonFunc(user_groups)) + return await a_Message.answer(user_id, error_backup_message, reply_markup = a_ButtonFunc(a_Message, user_groups)) - await bot.send_document(user_id, document, caption = a_CaptionMessage.replace('@time', log.GetTime()), reply_markup = a_ButtonFunc(user_groups)) + await bot.send_document(user_id, document, caption = a_CaptionMessage.replace('@time', log.GetTime()), reply_markup = a_ButtonFunc(a_Message, user_groups)) return BackupFile async def GetFile(a_Path): diff --git a/template/simple_message.py b/template/simple_message.py index e05f15f..1b571c0 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -29,7 +29,7 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) if not user_access.CheckAccessString(a_AccessFunc(), user_groups, access_mode): - return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups)) + return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = a_GetButtonsFunc(a_Message, user_groups)) res = await a_WorkFunc(a_Message, state = state) if res 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(user_groups)) + return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyborad_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(user_groups)) + return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyborad_func(a_Message, user_groups)) - await bot.send_photo(user_id, photo_id, msg, reply_markup = keyborad_func(user_groups)) + await bot.send_photo(user_id, photo_id, msg, reply_markup = keyborad_func(a_Message, user_groups)) return SimpleMessage diff --git a/template/sql_request.py b/template/sql_request.py index 447a078..e498ac9 100644 --- a/template/sql_request.py +++ b/template/sql_request.py @@ -15,7 +15,7 @@ cancel_message = ''' 🚫 Запрос к БД отменён ''' -def GetCancelKeyboardButtons(a_UserGroups, a_AccessFunc, a_AccessMode): +def GetCancelKeyboardButtons(a_Message, a_UserGroups, a_AccessFunc, a_AccessMode): cur_buttons = [ keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()) ] @@ -26,9 +26,9 @@ def RequestToBDTemplate(a_StartMessage, a_AccessFunc, a_FSM, a_AccessMode): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) if not user_access.CheckAccessString(a_AccessFunc(), user_groups, a_AccessMode): - return await a_Message.answer(access.access_denied_message, reply_markup = GetCancelKeyboardButtons(user_groups, a_AccessFunc, a_AccessMode)) + return await a_Message.answer(access.access_denied_message, reply_markup = GetCancelKeyboardButtons(a_Message, user_groups, a_AccessFunc, a_AccessMode)) await a_FSM.sqlRequest.set() - await a_Message.answer(a_StartMessage, reply_markup = GetCancelKeyboardButtons(user_groups, a_AccessFunc, a_AccessMode), parse_mode='Markdown') + await a_Message.answer(a_StartMessage, reply_markup = GetCancelKeyboardButtons(a_Message, user_groups, a_AccessFunc, a_AccessMode), parse_mode='Markdown') return RequestToBDStart def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc, a_AccessMode): @@ -36,12 +36,12 @@ def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc, a_AccessMode): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) if not user_access.CheckAccessString(a_AccessFunc(), user_groups, a_AccessMode): - return await a_Message.answer(access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups)) + return await a_Message.answer(access.access_denied_message, reply_markup = a_GetButtonsFunc(a_Message, user_groups)) result = '' async with state.proxy() as prjData: if a_Message.text == canсel_button_name: await state.finish() - return await a_Message.answer(cancel_message, reply_markup = a_GetButtonsFunc(user_groups)) + return await a_Message.answer(cancel_message, reply_markup = a_GetButtonsFunc(a_Message, user_groups)) sql_request = a_Message.text log.Success(f'Сделан запрос [{sql_request}] пользователем {a_Message.from_user.id}.') @@ -52,7 +52,7 @@ def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc, a_AccessMode): else: log.Success(f'Результат запроса [{sql_request}] от пользователя {a_Message.from_user.id} следующий [{result}].') await state.finish() - await a_Message.answer(str(result), reply_markup = a_GetButtonsFunc(user_groups)) + await a_Message.answer(str(result), reply_markup = a_GetButtonsFunc(a_Message, user_groups)) return RequestToBDFinish def RequestToBDRegisterHandlers(dp, a_RequestButtonName, a_RequestStartMessage, a_FSM, a_GetButtonsFunc, a_AccessMode, a_AccessFunc):