From 166f5ce98252f35358499251133cc56d7a68810e Mon Sep 17 00:00:00 2001 From: Alexei Date: Sun, 28 May 2023 10:00:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/mod_table_operate.py | 80 ++++++++++++++++++++++++---------------- bot_modules/orders.py | 78 ++++++++++++++++++++++++++++++++++----- template/bd_item_add.py | 3 ++ 3 files changed, 119 insertions(+), 42 deletions(-) diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 1f0bcfb..9dff4db 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -22,6 +22,7 @@ class ButtonNames(Enum): EDIT_DESC = auto() EDIT_ACCESS = auto() EDIT_DEFAULT_ACCESS = auto() + EDIT_ADDRESS = auto() DEL = auto() class Messages(Enum): @@ -39,6 +40,7 @@ class Messages(Enum): EDIT_DESC = auto() EDIT_ACCESS = auto() EDIT_DEFAULT_ACCESS = auto() + EDIT_ADDRESS = auto() SUCCESS_EDIT = auto() SELECT_TO_DELETE = auto() SUCCESS_DELETE = auto() @@ -49,7 +51,8 @@ class FSMs(Enum): EDIT_NAME = auto() EDIT_DESC = auto() EDIT_ACCESS = auto() - EDIT_DEFAULT_ACCESS = auto() + EDIT_DEFAULT_ACCESS = auto() + EDIT_ADDRESS = auto() create_fsms_cmd = ''' class FSMCreate{a_ModName}(StatesGroup): @@ -72,6 +75,9 @@ class FSMEdit{a_ModName}AccessItem(StatesGroup): class FSMEdit{a_ModName}DefaultAccessItem(StatesGroup): item_field = State() +class FSMEdit{a_ModName}AddressItem(StatesGroup): + item_field = State() + fsm = { FSMs.CREATE: FSMCreate{a_ModName}, FSMs.EDIT_NAME: FSMEdit{a_ModName}NameItem, @@ -79,6 +85,7 @@ fsm = { FSMs.EDIT_PHOTO: FSMEdit{a_ModName}PhotoItem, FSMs.EDIT_ACCESS: FSMEdit{a_ModName}AccessItem, FSMs.EDIT_DEFAULT_ACCESS: FSMEdit{a_ModName}DefaultAccessItem, + FSMs.EDIT_ADDRESS: FSMEdit{a_ModName}AddressItem, } ''' @@ -266,6 +273,38 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): return a_Prefix + def RegisterEdit(self, a_ButtonName, a_FSM, a_EditMessage, a_FieldName, a_FieldType, a_AccessMode = user_access.AccessMode.EDIT): + if not a_ButtonName: + return + + def OnChange(): + return self.OnChange() + + table_name = self.m_Table.GetName() + key_name = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY) + edit_keyboard_func = self.m_GetEditKeyboardButtonsFunc + GetButtonNameAndKeyValueAndAccess = self.m_GetButtonNameAndKeyValueAndAccessFunc + GetAccess = self.m_GetAccessFunc + + a_Prefix = self.RegisterSelect(a_ButtonName, a_AccessMode, only_parent = True) + + bd_item_edit.EditBDItemRegisterHandlers(self.m_Bot, \ + self.SelectSourceTemplate(a_Prefix, a_ButtonName), \ + a_FSM, \ + self.GetMessage(Messages.SELECT_TO_EDIT), \ + self.ShowMessageTemplate(a_EditMessage), \ + self.ShowMessageTemplate(self.GetMessage(Messages.SUCCESS_EDIT)), \ + table_name, \ + key_name, \ + a_FieldName, \ + GetButtonNameAndKeyValueAndAccess, \ + GetAccess, \ + edit_keyboard_func, \ + OnChange,\ + access_mode = a_AccessMode, \ + field_type = a_FieldType\ + ) + def RegisterHandlers(self): super().RegisterHandlers() table_name = self.m_Table.GetName() @@ -361,32 +400,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): # Редактирование 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: - return - - def OnChange(): - return self.OnChange() - - a_Prefix = self.RegisterSelect(a_ButtonName, a_AccessMode, only_parent = True) - - bd_item_edit.EditBDItemRegisterHandlers(self.m_Bot, \ - self.SelectSourceTemplate(a_Prefix, a_ButtonName), \ - a_FSM, \ - self.GetMessage(Messages.SELECT_TO_EDIT), \ - self.ShowMessageTemplate(a_EditMessage), \ - self.ShowMessageTemplate(self.GetMessage(Messages.SUCCESS_EDIT)), \ - table_name, \ - key_name, \ - a_FieldName, \ - GetButtonNameAndKeyValueAndAccess, \ - GetAccess, \ - edit_keyboard_func, \ - OnChange,\ - access_mode = a_AccessMode, \ - field_type = a_FieldType\ - ) - a_ButtonName = self.GetButton(ButtonNames.EDIT) if a_ButtonName: self.m_Bot.RegisterMessageHandler(\ @@ -400,11 +413,14 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): bd_item.GetCheckForTextFunc(a_ButtonName)\ ) - RegisterEdit(self.GetButton(ButtonNames.EDIT_NAME), self.GetFSM(FSMs.EDIT_NAME), self.GetMessage(Messages.EDIT_NAME), name_field, bd_item.FieldType.text) - RegisterEdit(self.GetButton(ButtonNames.EDIT_DESC), self.GetFSM(FSMs.EDIT_DESC), self.GetMessage(Messages.EDIT_DESC), desc_field, bd_item.FieldType.text) - RegisterEdit(self.GetButton(ButtonNames.EDIT_PHOTO), self.GetFSM(FSMs.EDIT_PHOTO), self.GetMessage(Messages.EDIT_PHOTO), photo_field, bd_item.FieldType.photo) - RegisterEdit(self.GetButton(ButtonNames.EDIT_ACCESS), self.GetFSM(FSMs.EDIT_ACCESS), self.GetMessage(Messages.EDIT_ACCESS), access_field, bd_item.FieldType.text) - RegisterEdit(self.GetButton(ButtonNames.EDIT_DEFAULT_ACCESS), self.GetFSM(FSMs.EDIT_DEFAULT_ACCESS), self.GetMessage(Messages.EDIT_DEFAULT_ACCESS), def_access_field, bd_item.FieldType.text) + address_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ADDRESS) + + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_NAME), self.GetFSM(FSMs.EDIT_NAME), self.GetMessage(Messages.EDIT_NAME), name_field, bd_item.FieldType.text) + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_DESC), self.GetFSM(FSMs.EDIT_DESC), self.GetMessage(Messages.EDIT_DESC), desc_field, bd_item.FieldType.text) + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_PHOTO), self.GetFSM(FSMs.EDIT_PHOTO), self.GetMessage(Messages.EDIT_PHOTO), photo_field, bd_item.FieldType.photo) + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_ACCESS), self.GetFSM(FSMs.EDIT_ACCESS), self.GetMessage(Messages.EDIT_ACCESS), access_field, bd_item.FieldType.text) + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_DEFAULT_ACCESS), self.GetFSM(FSMs.EDIT_DEFAULT_ACCESS), self.GetMessage(Messages.EDIT_DEFAULT_ACCESS), def_access_field, bd_item.FieldType.text) + self.RegisterEdit(self.GetButton(ButtonNames.EDIT_ADDRESS), self.GetFSM(FSMs.EDIT_ADDRESS), self.GetMessage(Messages.EDIT_ADDRESS), address_field, bd_item.FieldType.text) def OnChange(self): pass diff --git a/bot_modules/orders.py b/bot_modules/orders.py index bc59059..74a7e7d 100644 --- a/bot_modules/orders.py +++ b/bot_modules/orders.py @@ -5,7 +5,7 @@ from bot_sys import bot_bd, keyboard, user_access, bd_table from bot_modules import mod_table_operate, mod_simple_message -from template import bd_item_select +from template import bd_item_select, bd_item_view from enum import Enum from enum import auto @@ -49,21 +49,18 @@ init_access = f'{user_access.user_access_group_new}=va' # Сообщения и кнопки class ButtonNames(Enum): - LIST_ALL = auto() - EDIT_ADDRESS = auto() - -class Messages(Enum): - EDIT_ADDRESS = auto() + LIST_CURRENT = auto() button_names = { mod_simple_message.ButtonNames.START: "‍🛒 Заказы", - mod_table_operate.ButtonNames.LIST: "📃 Список текущих заказов", - ButtonNames.LIST_ALL: "📃 Список всех заказов", + mod_table_operate.ButtonNames.LIST: "📃 Список всех заказов", + ButtonNames.LIST_CURRENT: "📃 Список текущих заказов", mod_table_operate.ButtonNames.ADD: "✅ Добавить заказ", mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать заказ", mod_table_operate.ButtonNames.EDIT_PHOTO: "☐ Изменить изображение в заказе", mod_table_operate.ButtonNames.EDIT_NAME: "≂ Изменить название в заказе", mod_table_operate.ButtonNames.EDIT_DESC: "𝌴 Изменить описание в заказе", + mod_table_operate.ButtonNames.EDIT_ADDRESS: "𝌴 Изменить адрес в заказе", mod_table_operate.ButtonNames.EDIT_ACCESS: "✋ Изменить доступ к заказу", mod_table_operate.ButtonNames.DEL: "❌ Удалить заказ", } @@ -129,7 +126,7 @@ messages = { Введите новое описание заказа: ''', - Messages.EDIT_ADDRESS: f''' + mod_table_operate.Messages.EDIT_ADDRESS: f''' Текущий адрес заказа: #{desc_field} @@ -151,6 +148,11 @@ messages = { mod_table_operate.Messages.SUCCESS_DELETE: '''✅ Заказ успешно удалён!''', } +def GetCurItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName, a_StatusFieldName): + def GetBDItem(a_KeyValue): + return a_Bot.SQLRequest(f'SELECT * FROM {a_TableName} WHERE {a_UserIDFieldName} = ? AND {a_StatusFieldName} != ?', param = ([a_KeyValue, OrderStatus.FINISH])) + return GetBDItem + def GetBDItemsForUserTemplate(a_GetItemsFunc): def GetBDItems(a_Message, a_UserGroups, a_ParentID): def GetBDItem(a_KeyValue): @@ -162,8 +164,15 @@ def GetBDItemsForUserTemplate(a_GetItemsFunc): return GetBDItems class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): + def __init__(self, a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_ButtonName, a_OnlyCurent = False): + super().__init__(a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_ButtonName) + self.m_OnlyCurent = a_OnlyCurent + def GetItemsFunc(self): - return GetBDItemsForUserTemplate(super().GetItemsFunc()) + get_items_func = super().GetItemsFunc() + if self.m_OnlyCurent: + get_items_func = GetCurItemsTemplate(self.m_Bot, self.m_TableName, self.m_ParentIDFieldName, status_field) + return GetBDItemsForUserTemplate(get_items_func) class ModuleOrders(mod_table_operate.TableOperateModule): def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log): @@ -176,4 +185,53 @@ class ModuleOrders(mod_table_operate.TableOperateModule): parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) return DBItemForUserSelectSource(self.m_Bot, self.m_Table.GetName(), parent_id_field, a_PrevPrefix, a_ButtonName) + def SelectSourceForCurrentTemplate(self, a_PrevPrefix, a_ButtonName): + parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + return DBItemForUserSelectSource(self.m_Bot, self.m_Table.GetName(), parent_id_field, a_PrevPrefix, a_ButtonName, a_OnlyCurent = True) + + def AddBDItemFunc(self, a_ItemData, a_UserID): + parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + a_ItemData[parent_id_field] = a_UserID + return super().AddBDItemFunc(a_ItemData, a_UserID) + + def GetStartKeyboardButtons(self, a_Message, a_UserGroups): + parent_buttons = super().GetStartKeyboardButtons(a_Message, a_UserGroups) + cur_buttons = [ + keyboard.ButtonWithAccess(self.GetButton(ButtonNames.LIST_CURRENT), user_access.AccessMode.VIEW, self.GetAccess()), + ] + return parent_buttons + keyboard.MakeButtons(self.m_Bot, cur_buttons, a_UserGroups) + + def RegisterHandlers(self): + super().RegisterHandlers() + table_name = self.m_Table.GetName() + key_name = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY) + + def GetViewItemInlineKeyboardTemplate(a_ItemID): + return self.GetViewItemInlineKeyboardTemplate(a_ItemID) + + GetButtonNameAndKeyValueAndAccess = self.m_GetButtonNameAndKeyValueAndAccessFunc + GetAccess = self.m_GetAccessFunc + + default_keyboard_func = self.m_GetStartKeyboardButtonsFunc + + # Список текущих, открытых заказов + a_ButtonName = self.GetButton(ButtonNames.LIST_CURRENT) + if a_ButtonName: + a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True) + a_Prefix = bd_item_select.SelectRegisterHandlers(self.m_Bot,\ + self.SelectSourceForCurrentTemplate(a_Prefix, a_ButtonName), \ + GetButtonNameAndKeyValueAndAccess,\ + self.GetMessage(mod_table_operate.Messages.SELECT),\ + GetAccess,\ + access_mode = user_access.AccessMode.VIEW\ + ) + bd_item_view.ShowBDItemRegisterHandlers(self.m_Bot,\ + a_Prefix,\ + table_name,\ + key_name,\ + self.ShowMessageTemplate(self.GetMessage(mod_table_operate.Messages.OPEN),GetViewItemInlineKeyboardTemplate),\ + GetAccess,\ + default_keyboard_func,\ + access_mode = user_access.AccessMode.VIEW\ + ) diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 2bcb39d..73cbd71 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -10,6 +10,9 @@ from template import simple_message, bd_item_select, bd_item cancel_message = ''' 🚫 Добавление отменено ''' +error_photo_type_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):