From 74760c35f16b89f364e6d8ca329f1428ea2a8906 Mon Sep 17 00:00:00 2001 From: Alexei Date: Wed, 17 May 2023 11:47:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B0=D1=85=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D1=81=D0=BC=D0=BE=D1=82=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/mod_table_operate.py | 197 ++++++++++++++++++++++----------------- bot_modules/projects.py | 2 +- bot_sys/bd_table.py | 15 ++- bot_sys/bot_bd.py | 11 +-- bot_sys/keyboard.py | 2 +- main.py | 2 +- template/bd_item.py | 6 +- template/bd_item_add.py | 4 +- template/bd_item_delete.py | 7 +- template/bd_item_select.py | 3 +- 10 files changed, 140 insertions(+), 109 deletions(-) diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 96e70c6..d7b8355 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -3,7 +3,7 @@ # Модуль для редактирования и просмотра таблицы в БД -from bot_sys import keyboard, user_access, bd_table +from bot_sys import keyboard, user_access, bd_table, bot_bd from bot_modules import access_utils, mod_simple_message from template import simple_message, bd_item, bd_item_select, bd_item_view, bd_item_delete, bd_item_add, bd_item_edit @@ -75,15 +75,15 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): self.m_GetButtonNameAndKeyValueAndAccessFunc = GetButtonNameAndKeyValueAndAccess async def PreDelete(a_CallbackQuery, a_Item): - return await self.PreDelete(a_Item) + return await self.PreDelete(a_CallbackQuery, a_Item) self.m_PreDeleteFunc = PreDelete async def PostDelete(a_CallbackQuery, a_ItemID): - return await self.PostDelete(a_Item) + return await self.PostDelete(a_CallbackQuery, a_ItemID) self.m_PostDeleteFunc = PostDelete def AddBDItemFunc(a_ItemData, a_UserID): - return self.AddBDItemFunc(a_Item) + return self.AddBDItemFunc(a_ItemData, a_UserID) self.m_AddBDItemFunc = AddBDItemFunc @@ -101,20 +101,20 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): def GetStartKeyboardButtons(self, a_Message, a_UserGroups): mod_buttons = super().GetStartKeyboardButtons(a_Message, a_UserGroups) cur_buttons = [ - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.LIST), user_access.AccessMode.VIEW, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.ADD), user_access.AccessMode.ADD, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.DEL), user_access.AccessMode.DELETE, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT), user_access.AccessMode.EDIT, GetAccess()) + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.LIST), user_access.AccessMode.VIEW, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.ADD), user_access.AccessMode.ADD, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.DEL), user_access.AccessMode.DELETE, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT), user_access.AccessMode.EDIT, self.GetAccess()) ] return mod_buttons + keyboard.MakeButtons(cur_buttons, a_UserGroups) def GetEditKeyboardButtons(self, a_Message, a_UserGroups): mod_buttons = self.GetButtons(self.m_EditModuleNameList) cur_buttons = [ - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_PHOTO), user_access.AccessMode.VIEW, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_NAME), user_access.AccessMode.ADD, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_DESC), user_access.AccessMode.DELETE, GetAccess()), - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_ACCESS), user_access.AccessMode.EDIT, GetAccess()) + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_PHOTO), user_access.AccessMode.VIEW, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_NAME), user_access.AccessMode.ADD, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_DESC), user_access.AccessMode.DELETE, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.EDIT_ACCESS), user_access.AccessMode.EDIT, self.GetAccess()) ] return mod_buttons + keyboard.MakeButtons(cur_buttons, a_UserGroups) @@ -131,7 +131,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): return None child_mod = self.GetModule(self.m_ChildModName) cur_buttons = [ - keyboard.InlineButton(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + keyboard.InlineButton(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, self.GetAccess(), user_access.AccessMode.VIEW), ] return keyboard.MakeInlineKeyboardButtons(cur_buttons, a_UserGroups) @@ -141,19 +141,22 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)],\ a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)] - def ShowMessageTemplate(self, a_MessageName : Messages, keyboard_template_func = None): + def ShowMessageTemplate(self, a_Message, keyboard_template_func = None): async def ShowMessage(a_CallbackQuery, a_Item): - if len(a_Item) < self.m_Table.GetFieldsCount(): - return simple_message.WorkFuncResult(self.GetMessage(Messages.ERROR_FIND)) - - msg = self.GetMessage(a_MessageName).StaticCopy() - msg.UpdateDesc(table.ReplaceAllFieldTags(msg.GetDesc(), a_Item)) - msg.UpdatePhotoID(a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PHOTO)]) - + msg = a_Message.StaticCopy() + # TODO: добавить поддержку языка в a_MessageName keyboard_func = None - if keyboard_template_func: - keyboard_func = keyboard_template_func(a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)]) - return simple_message.WorkFuncResult(msg, item_access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)], keyboard_func = keyboard_func) + item_access = None + if a_Item: + if len(a_Item) < self.m_Table.GetFieldsCount(): + return simple_message.WorkFuncResult(self.GetMessage(Messages.ERROR_FIND)) + msg.UpdateDesc(self.m_Table.ReplaceAllFieldTags(msg.GetDesc(), a_Item)) + msg.UpdatePhotoID(a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PHOTO)]) + item_access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)] + if keyboard_template_func: + keyboard_func = keyboard_template_func(a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)]) + + return simple_message.WorkFuncResult(msg, item_access = item_access, keyboard_func = keyboard_func) return ShowMessage # TODO: delete? @@ -168,23 +171,29 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)] return simple_message.WorkFuncResult(self.GetMessage(Messages.SUCCESS_DELETE), None, item_access = access) - async def TaskPostDelete(self, a_CallbackQuery, a_ItemID): + async def PostDelete(self, a_CallbackQuery, a_ItemID): self.m_Log.Success(f'Задача №{a_ItemID} была удалена пользователем {a_CallbackQuery.from_user.id}.') #TODO: удалить вложенные return simple_message.WorkFuncResult(self.GetMessage(Messages.SUCCESS_DELETE)) def AddBDItemFunc(self, a_ItemData, a_UserID): table_name = self.m_Table.GetName() - name_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.NAME) - photo_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.PHOTO) - desc_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC) - access_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.ACCESS) - create_datetime_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.CREATE_DATE) - parent_id_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.PARENT_ID) - + name_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.NAME) + photo_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PHOTO) + desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC) + access_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ACCESS) + create_datetime_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.CREATE_DATE) + parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + + res, error = None, None def_access = access_utils.GetItemDefaultAccessForModule(self.m_Bot, self.GetName()) - res, error = self.m_Bot.SQLRequest(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], def_access + f";{a_UserID}=+", a_ItemData[parent_id_field])) + #TODO сделать список полей, чтобы запрос генерировался автоматически. + if parent_id_field: + res, error = self.m_Bot.SQLRequest(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], def_access + f";{a_UserID}=+", a_ItemData[parent_id_field])) + else: + res, error = self.m_Bot.SQLRequest(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], def_access + f";{a_UserID}=+")) if error: self.m_Log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {def_access}).') @@ -193,55 +202,56 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): return res, error - def RegisterSelect(self, a_ButtonName, access_mode): + def RegisterSelect(self, a_ButtonName, access_mode, only_parent = False): + a_Prefix = None if self.m_ParentModName: parent_mod = self.GetModule(self.m_ParentModName) a_Prefix = parent_mod.RegisterSelect(a_ButtonName, access_mode) - a_Prefix = bd_item_select.NextSelectBDItemRegisterHandlers(self.m_Bot, \ - a_Prefix, \ - self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.PARENT_ID), \ - self.m_Table.GetName(), \ - self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.KEY), \ - self.m_GetButtonNameAndKeyValueAndAccessFunc, \ - self.GetMessage(Messages.SELECT), \ - self.m_GetAccessFunc,\ - access_mode = access_mode\ - ) + if not only_parent: + a_Prefix = bd_item_select.NextSelectBDItemRegisterHandlers(self.m_Bot, \ + a_Prefix, \ + self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID), \ + self.m_Table.GetName(), \ + self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY), \ + self.m_GetButtonNameAndKeyValueAndAccessFunc, \ + self.GetMessage(Messages.SELECT), \ + self.m_GetAccessFunc,\ + access_mode = access_mode\ + ) else: - a_PrefixBase = a_ButtonName.GetDesc() - a_Prefix = bd_item_select.FirstSelectBDItemRegisterHandlers(self.m_Bot, \ - a_PrefixBase, \ - a_ButtonName, \ - self.m_Table.GetName(), \ - self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.KEY), \ - self.m_GetButtonNameAndKeyValueAndAccessFunc, \ - self.GetMessage(Messages.SELECT), \ - self.m_GetAccessFunc,\ - access_mode = access_mode\ - ) + if not only_parent: + a_PrefixBase = a_ButtonName.GetDesc() + a_Prefix = bd_item_select.FirstSelectBDItemRegisterHandlers(self.m_Bot, \ + a_PrefixBase, \ + a_ButtonName, \ + self.m_Table.GetName(), \ + self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY), \ + self.m_GetButtonNameAndKeyValueAndAccessFunc, \ + self.GetMessage(Messages.SELECT), \ + self.m_GetAccessFunc,\ + access_mode = access_mode\ + ) return a_Prefix def RegisterHandlers(self): super().RegisterHandlers() table_name = self.m_Table.GetName() - key_name = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.KEY) - name_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.NAME) - desc_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC) - photo_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.PHOTO) - access_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.ACCESS) - parent_id_field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + key_name = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY) + name_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.NAME) + desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC) + photo_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PHOTO) + access_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ACCESS) + parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) parent_table_name = None parent_key_name = None if self.m_ParentModName: parent_mod = self.GetModule(self.m_ParentModName) parent_table_name = parent_mod.m_Table.GetName() - parent_key_name = parent_mod.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.KEY) + parent_key_name = parent_mod.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY) def GetViewItemInlineKeyboardTemplate(a_ItemID): - def GetViewItemInlineKeyboard(a_ItemID): - return self.GetViewItemInlineKeyboardTemplate(a_ItemID) - return GetViewItemInlineKeyboard + return self.GetViewItemInlineKeyboardTemplate(a_ItemID) GetButtonNameAndKeyValueAndAccess = self.m_GetButtonNameAndKeyValueAndAccessFunc GetAccess = self.m_GetAccessFunc @@ -251,19 +261,31 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): # Список a_ButtonName = self.GetButton(ButtonNames.LIST) if a_ButtonName: - a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW) - bd_item_view.LastSelectAndShowBDItemRegisterHandlers(self.m_Bot, \ - a_Prefix,\ - parent_id_field, \ - table_name,\ - key_name, \ - self.ShowMessageTemplate(self.GetMessage(Messages.OPEN), GetViewItemInlineKeyboardTemplate), \ - GetButtonNameAndKeyValueAndAccess, \ - self.GetMessage(Messages.SELECT), \ - GetAccess, \ - defaul_keyboard_func, \ - access_mode = user_access.AccessMode.VIEW\ - ) + a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True) + if a_Prefix: + bd_item_view.LastSelectAndShowBDItemRegisterHandlers(self.m_Bot, \ + a_Prefix,\ + parent_id_field, \ + table_name,\ + key_name, \ + self.ShowMessageTemplate(self.GetMessage(Messages.OPEN), GetViewItemInlineKeyboardTemplate), \ + GetButtonNameAndKeyValueAndAccess, \ + self.GetMessage(Messages.SELECT), \ + GetAccess, \ + defaul_keyboard_func, \ + access_mode = user_access.AccessMode.VIEW\ + ) + else: + bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(self.m_Bot, \ + a_ButtonName, \ + table_name, \ + key_name, \ + self.ShowMessageTemplate(self.GetMessage(Messages.OPEN), GetViewItemInlineKeyboardTemplate), \ + GetButtonNameAndKeyValueAndAccess, \ + self.GetMessage(Messages.SELECT), \ + GetAccess, \ + defaul_keyboard_func\ + ) self.m_SelectPrefix = a_Prefix # Удаление @@ -272,14 +294,10 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.DELETE) bd_item_delete.DeleteBDItemRegisterHandlers(self.m_Bot, \ a_Prefix, \ - bd_item.GetCheckForPrefixFunc(a_Prefix), \ table_name, \ key_name, \ - parent_id_field, \ self.m_PreDeleteFunc, \ self.m_PostDeleteFunc, \ - GetButtonNameAndKeyValueAndAccess, \ - self.GetMessage(Messages.SELECT), \ GetAccess, \ defaul_keyboard_func\ ) @@ -287,9 +305,12 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): # Добавление a_ButtonName = self.GetButton(ButtonNames.ADD) if a_ButtonName: - a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.ADD) + a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.ADD, only_parent = True) + check_func = bd_item.GetCheckForTextFunc(a_ButtonName) + if a_Prefix: + check_func = bd_item.GetCheckForPrefixFunc(a_Prefix) bd_item_add.AddBDItem3RegisterHandlers(self.m_Bot, \ - bd_item.GetCheckForPrefixFunc(a_Prefix), \ + check_func, \ self.m_FSMs.m_FSMCreate, \ self.m_FSMs.m_FSMCreate.name,\ self.m_FSMs.m_FSMCreate.desc, \ @@ -309,8 +330,8 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): GetAccess, \ self.m_GetStartKeyboardButtonsFunc\ ) - - # Редактирование + ''' + # Редактирование edit_keyboard_func = self.m_GetEditKeyboardButtonsFunc def RegisterEdit(a_ButtonName, a_FSM, a_EditMessage, a_FieldName, a_FieldType, a_AccessMode = user_access.AccessMode.EDIT): if not a_ButtonName: @@ -351,3 +372,5 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): RegisterEdit(self.GetButton(ButtonNames.EDIT_DESC), self.m_FSMs.m_FSMEditDesc, self.GetMessage(Messages.EDIT_DESC), desc_field, bd_item.FieldType.text) RegisterEdit(self.GetButton(ButtonNames.EDIT_PHOTO), self.m_FSMs.m_FSMEditPhoto, self.GetMessage(Messages.EDIT_PHOTO), photo_field, bd_item.FieldType.photo) RegisterEdit(self.GetButton(ButtonNames.EDIT_ACCESS), self.m_FSMs.m_FSMEditAccess, self.GetMessage(Messages.EDIT_ACCESS), access_field, bd_item.FieldType.text) + ''' + diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 0f884a6..17b5a5a 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -147,7 +147,7 @@ class ModuleProjects(mod_table_operate.TableOperateModule): def GetModuleButtons(self): return super().GetModuleButtons() + [ - keyboard.ButtonWithAccess(self.GetButton(ButtonNames.LIST), user_access.AccessMode.VIEW, self.GetAccess()), + keyboard.ButtonWithAccess(self.GetButton(mod_table_operate.ButtonNames.LIST), user_access.AccessMode.VIEW, self.GetAccess()), ] diff --git a/bot_sys/bd_table.py b/bot_sys/bd_table.py index f68b393..233a806 100644 --- a/bot_sys/bd_table.py +++ b/bot_sys/bd_table.py @@ -38,7 +38,7 @@ class Table: return self.TableFieldType def GetFieldsCount(self): - return len(self.TableFieldType) + return len(self.m_Fields) def GetFieldByDestiny(self, a_Destiny): for f in self.m_Fields: @@ -46,6 +46,12 @@ class Table: return f return None + def GetFieldNameByDestiny(self, a_Destiny): + f = self.GetFieldByDestiny(a_Destiny) + if f: + return f.m_Name + return None + def GetFieldIDByDestiny(self, a_Destiny): for i in range(len(self.m_Fields)): f = self.m_Fields[i] @@ -78,23 +84,28 @@ def Test(): ]) assert table.GetName() == 'tname' assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Name == 'f1' + assert table.GetFieldNameByDestiny(TableFieldDestiny.KEY) == 'f1' assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Destiny == TableFieldDestiny.KEY assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Type == TableFieldType.INT assert table.GetFieldIDByDestiny(TableFieldDestiny.KEY) == 0 assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Name == 'f2' + assert table.GetFieldNameByDestiny(TableFieldDestiny.NAME) == 'f2' assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Destiny == TableFieldDestiny.NAME assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Type == TableFieldType.STR assert table.GetFieldIDByDestiny(TableFieldDestiny.NAME) == 1 assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Name == 'f3' + assert table.GetFieldNameByDestiny(TableFieldDestiny.DESC) == 'f3' assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Destiny == TableFieldDestiny.DESC assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Type == TableFieldType.STR assert table.GetFieldIDByDestiny(TableFieldDestiny.DESC) == 2 assert table.GetFieldByDestiny(TableFieldDestiny.PHOTO) == None assert table.GetFieldIDByDestiny(TableFieldDestiny.PHOTO) == None + assert table.GetFieldNameByDestiny(TableFieldDestiny.PHOTO) == None + + assert table.GetFieldsCount() == 3 assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT , f3 TEXT );' item = [10, 'i1', 'i2'] - print( table.ReplaceAllFieldTags('#f1 #f2 #f3', item)) assert table.ReplaceAllFieldTags('#f1 #f2 #f3', item) == '10 i1 i2' diff --git a/bot_sys/bot_bd.py b/bot_sys/bot_bd.py index ea9a266..4a1d73f 100644 --- a/bot_sys/bot_bd.py +++ b/bot_sys/bot_bd.py @@ -19,18 +19,11 @@ def GetBDFileName(): def GetBDDateTimeNow(): return 'datetime(\'now\')' -def BDExecute(a_Commands): - for cmd in a_Commands: - SQLRequestToBD(cmd, commit = True) - -def SelectBDTemplate(a_TableName): +def SelectBDTemplate(a_Bot, a_TableName): def SelectBD(): - return SQLRequestToBD(f'SELECT * FROM {a_TableName}') + return a_Bot.SQLRequest(f'SELECT * FROM {a_TableName}') return SelectBD -def SQLRequestToBD(a_Request : str, commit = False, return_error = False, param = None): - return SQLRequest(GetBDFileName(), a_Request, commit = commit, return_error = return_error, param = param) - def RequestsExecute(a_BDFileName, a_Commands): for cmd in a_Commands: SQLRequest(a_BDFileName, cmd, commit = True) diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index 706ddc6..b0ecbc4 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -75,7 +75,7 @@ def MakeAiogramInlineKeyboard(a_ButtonList : [InlineButton]): r = [] for b in row: r += [types.InlineKeyboardButton(text = str(b.label), callback_data = b.callback_data)] - buttons += r + buttons += [r] inline_keyboard = InlineKeyboardMarkup(inline_keyboard=buttons) return inline_keyboard diff --git a/main.py b/main.py index 1b5ec47..a7863dc 100644 --- a/main.py +++ b/main.py @@ -38,7 +38,7 @@ g_ModuleAgregator.AddModule(mod_profile) mod_backup = backup.ModuleBackup(start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log) g_ModuleAgregator.AddModule(mod_backup) -start_mod_name_list = [mod_start_name, mod_tasks_name, mod_needs_name, mod_comments_name] +start_mod_name_list = [mod_start_name]#, mod_tasks_name, mod_needs_name, mod_comments_name] mod_project = projects.ModuleProjects(None, None, start_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log) g_ModuleAgregator.AddModule(mod_project) diff --git a/template/bd_item.py b/template/bd_item.py index 695030e..31f3316 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -47,7 +47,7 @@ def GetCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): cur_buttons = [ keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()), ] - return keyboard.MakeInlineKeyboardButtons(cur_buttons, a_UserGroups) + return keyboard.MakeButtons(cur_buttons, a_UserGroups) return GetCancelKeyboardButtons def GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): @@ -56,7 +56,7 @@ def GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode): keyboard.ButtonWithAccess(skip_button_name, a_AccessMode, a_AccessFunc()), keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()), ] - return keyboard.MakeInlineKeyboardButtons(cur_buttons, a_UserGroups) + return keyboard.MakeButtons(cur_buttons, a_UserGroups) return GetSkipAndCancelKeyboardButtons def GetAllItemsTemplate(a_Bot, a_TableName): @@ -91,7 +91,7 @@ def CheckAccessBDItemTemplate(a_Bot, a_TableName, a_KeyName, a_KeyValue, a_WorkF return simple_message.WorkFuncResult(bot_messages.MakeBotMessage(msg)), None result_work_func = await a_WorkFunc(a_CallbackQuery, item[0]) - if result_work_func is None or result_work_func.string_message is None: + if result_work_func is None or result_work_func.m_BotMessage is None: return result_work_func, result_work_func if not result_work_func.item_access is None and not user_access.CheckAccessString(result_work_func.item_access, user_groups, a_AccessMode): diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 394a950..a203da0 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -14,7 +14,7 @@ cancel_message = ''' def StartAddBDItemTemplate(a_Bot, 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, state): user_id = str(a_CallbackQuery.from_user.id) - user_groups = groups.GetUserGroupData(a_Bot, user_id) + user_groups = groups_utils.GetUserGroupData(a_Bot, user_id) parent_id = bd_item.GetKeyDataFromCallbackMessage(a_CallbackQuery, a_Prefix) res_of_work_func = None check = None @@ -50,7 +50,7 @@ def FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableNa else: state_func = a_FSM.next user_id = str(a_Message.from_user.id) - user_groups = groups.GetUserGroupData(a_Bot, user_id) + user_groups = groups_utils.GetUserGroupData(a_Bot, user_id) error = None res_of_work_func = None check = None diff --git a/template/bd_item_delete.py b/template/bd_item_delete.py index 4228e7e..7c7a355 100644 --- a/template/bd_item_delete.py +++ b/template/bd_item_delete.py @@ -10,7 +10,7 @@ from template import simple_message, bd_item_select, bd_item def DeleteBDItemTemplate(a_Bot, 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, state = None): user_id = str(a_CallbackQuery.from_user.id) - user_groups = groups.GetUserGroupData(a_Bot, user_id) + user_groups = groups_utils.GetUserGroupData(a_Bot, user_id) item_id = str(a_CallbackQuery.data).replace(a_Prefix, '') check, res_of_pre_del = await bd_item.CheckAccessBDItemTemplate(a_Bot, a_TableName, a_KeyName, item_id, a_PreDeleteWorkFunc, access_mode)(a_CallbackQuery) @@ -29,7 +29,7 @@ def DeleteBDItemTemplate(a_Bot, a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_P return simple_message.SimpleMessageTemplate(a_Bot, DeleteBDItem, a_ButtonFunc, None, a_AccessFunc, access_mode) -def DeleteBDItemRegisterHandlers(a_Bot, 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): +def SelectAndDeleteBDItemRegisterHandlers(a_Bot, 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 = a_Bot.RegisterMessageHandler if a_ParentIDFieldName: reg_func = a_Bot.RegisterCallbackHandler @@ -39,3 +39,6 @@ def DeleteBDItemRegisterHandlers(a_Bot, a_PrevPrefix, a_StartCheckFunc, a_TableN reg_func(sel_handler, a_StartCheckFunc) a_Bot.RegisterCallbackHandler(DeleteBDItemTemplate(a_Bot, a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) +def DeleteBDItemRegisterHandlers(a_Bot, a_PrevPrefix, a_TableName : str, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE): + a_Bot.RegisterCallbackHandler(DeleteBDItemTemplate(a_Bot, a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_PrevPrefix, a_AccessFunc, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_PrevPrefix)) + diff --git a/template/bd_item_select.py b/template/bd_item_select.py index 0109ee5..2931af2 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -23,13 +23,14 @@ def GetBDItemsListKeyboardButtonsTemplate(a_Bot, a_TableName : str, a_ParentIDFi access = '' if bname: b = keyboard.InlineButtonWithAccess(bname, a_NextPrefix, key_value, access, access_mode) + print('MakeInlineKeyboardButtons', b, bname, a_NextPrefix, key_value, access, access_mode) items_button_list += [b] return keyboard.MakeInlineKeyboardButtons(items_button_list, a_UserGroups) return GetBDItemsListKeyboardButtons def SelectDBItemTemplate(a_Bot, a_TableName : str, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_NextPrefix, access_mode = user_access.AccessMode.VIEW): keyboard_func = GetBDItemsListKeyboardButtonsTemplate(a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) - return simple_message.InfoMessageTemplate(a_Bot, a_StartMessage, keyboard_func, None, a_AccessFunc, access_mode) + return simple_message.InfoMessageTemplate(a_Bot, a_StartMessage, None, keyboard_func, a_AccessFunc, access_mode) def FirstSelectBDItemRegisterHandlers(a_Bot, a_PrefixBase, a_ButtonName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):