diff --git a/bot_modules/all_orders.py b/bot_modules/all_orders.py index 99a8107..0ff2480 100644 --- a/bot_modules/all_orders.py +++ b/bot_modules/all_orders.py @@ -6,7 +6,7 @@ from bot_sys import bot_bd, keyboard, user_access, bd_table from bot_modules import mod_table_operate, mod_simple_message, orders, access_utils from bot_modules import users, user_message -from template import bd_item_select, bd_item_view, bd_item +from template import bd_item_select, bd_item_view, bd_item, bd_item_edit from enum import Enum from enum import auto @@ -25,6 +25,7 @@ init_access = f'{user_access.user_access_group_new}=-' class ButtonNames(Enum): SHOW_USER_INFO = auto() SEND_USER_MESSAGE = auto() + ADD_TO_DESC = auto() button_names = {} button_names.update(orders.button_names) @@ -37,6 +38,7 @@ cur_button_names = { orders.ButtonNames.LIST_ALL: "📃 Список всех заказов", ButtonNames.SHOW_USER_INFO: "👤 Информация о пользователе", ButtonNames.SEND_USER_MESSAGE: "📨 Отправить сообщение пользователю", + ButtonNames.ADD_TO_DESC: "𝌴 Добавить к описанию", mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать заказ", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO_PAY): "☐ Загрузить чек по оплате заказа", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в заказе", @@ -81,6 +83,12 @@ cur_messages = { Время создания: #{orders.create_datetime_field} ''', + mod_table_operate.InlineMessage(ButtonNames.ADD_TO_DESC): f''' +Текущее описание заказа: +#{orders.desc_field} + +Введите дополение к описанию заказа: +''', } messages_notification = { @@ -244,6 +252,13 @@ class ModuleAllOrders(orders.ModuleOrders): keyboard.InlineButtonWithAccess(self.GetButton(btn), prefix, cur_item[key_field_index], self.GetAccess(), access), ] + access = self.GetAccessForEditKeyboardButtons(self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC)) + a_Button = ButtonNames.ADD_TO_DESC + prefix = self.m_EditPrefix.get(a_Button, None) + cur_buttons += [ + keyboard.InlineButtonWithAccess(self.GetButton(a_Button), prefix, cur_item[key_field_index], self.GetAccess(), access), + ] + user_message_mod = self.GetModule(user_message.module_name) if user_message_mod: cur_buttons += [ @@ -258,3 +273,78 @@ class ModuleAllOrders(orders.ModuleOrders): return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups) + def RegisterAddToDesc(self): + a_AccessMode = user_access.AccessMode.EDIT + a_Button = ButtonNames.ADD_TO_DESC + a_ButtonName = self.GetButton(a_Button) + a_EditMessage = self.GetMessage(mod_table_operate.InlineMessage(a_Button)) + a_Field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC) + a_FieldName = a_Field.m_Name + + a_FieldType = bd_item.FieldType.text + if a_Field.m_Type == bd_table.TableFieldType.PHOTO: + a_FieldType = bd_item.FieldType.photo + + if not a_ButtonName or not a_EditMessage: + return + + async def OnChange(a_ItemID, a_ItemData, a_EditUserID): + await self.OnChangeField(a_Field, a_ItemID, a_ItemData, a_EditUserID) + return self.OnChange() + + table_name = self.m_Table.GetName() + key_name = self.GetKeyFieldName() + edit_keyboard_func = self.m_GetEditKeyboardButtonsFunc + GetButtonNameAndKeyValueAndAccess = self.m_GetButtonNameAndKeyValueAndAccessFunc + GetAccess = self.m_GetAccessFunc + + async def EditBDItemFunc(a_ItemData, a_UserID): + a_KeyName = key_name + a_TableName = table_name + item_id = a_ItemData[a_KeyName] + field_value = a_ItemData[a_FieldName] + + cur_item = mod_table_operate.GetCurItem(self.m_Bot, table_name, key_name, item_id) + + desc = "" + desc_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.DESC) + if desc_field_index and cur_item and cur_item[desc_field_index]: + desc = cur_item[desc_field_index] + + new_value = desc + "\n" + field_value + res, error = bd_item.EditBDItemInTableTemplate(self.m_Bot, a_TableName, a_KeyName, a_FieldName)(item_id, new_value) + if error: + self.m_Bot.GetLog().Error(f'Пользователю {a_UserID} не удалось изменить поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Ошибка: {error}') + else: + self.m_Bot.GetLog().Success(f'Пользователь {a_UserID} добавил в поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={new_value}. Старое {desc}.') + + await OnChange(item_id, a_ItemData, a_UserID) + return res, error + + a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True) + + a_Prefix = bd_item_edit.CustomEditBDItemRegisterHandlers(self.m_Bot, \ + EditBDItemFunc, + self.SelectSourceTemplate(a_Prefix, a_ButtonName), \ + mod_table_operate.MakeFSMForAddAndEdit(self.GetName(), a_FieldName), \ + self.GetMessage(mod_table_operate.Messages.SELECT_TO_EDIT), \ + self.ShowMessageTemplate(a_EditMessage), \ + self.ShowMessageTemplate(self.GetMessage(mod_table_operate.Messages.SUCCESS_EDIT)), \ + table_name, \ + key_name, \ + a_FieldName, \ + self.PostProccessingForFieldForEditTemplate(a_Field),\ + GetButtonNameAndKeyValueAndAccess, \ + GetAccess, \ + self.AdditionalKeyboardForEditTemplate(a_Field),\ + edit_keyboard_func, \ + access_mode = a_AccessMode, \ + field_type = a_FieldType\ + ) + + self.m_EditPrefix.update({a_Button: a_Prefix}) + + + def RegisterHandlers(self): + super().RegisterHandlers() + self.RegisterAddToDesc() diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 4058718..080ef51 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -22,6 +22,9 @@ def EnumButton(a_EnumItem): def EditMessage(a_BDTableDestiny): return 'edit ' + str(a_BDTableDestiny) +def InlineMessage(a_ButtonName): + return 'inline ' + str(a_ButtonName) + def CreateMessage(a_BDTableDestiny): return 'create ' + str(a_BDTableDestiny) @@ -89,6 +92,20 @@ def MakeFSMForEdit(a_ModName, a_FieldName): exec(cmd, globals(), _locals) return _locals['fsm'] +add_and_edit_fsm_cmd = ''' +class FSMAddAndEdit{a_ModName}_{a_FieldName}_Item(StatesGroup): + item_field = State() + +fsm = FSMAddAndEdit{a_ModName}_{a_FieldName}_Item +''' + +def MakeFSMForAddAndEdit(a_ModName, a_FieldName): + cmd = add_and_edit_fsm_cmd.replace("{a_ModName}", a_ModName).replace("{a_FieldName}", a_FieldName) + _locals = locals() + exec(cmd, globals(), _locals) + return _locals['fsm'] + + class TableOperateModule(mod_simple_message.SimpleMessageModule): def __init__(self, a_Table, a_Messages, a_Buttons, a_ParentModName, a_ChildModName, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log): super().__init__(a_Messages, a_Buttons, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log) @@ -697,9 +714,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): bd_item.GetCheckForTextFunc(a_ButtonName)\ ) - address_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ADDRESS) - status_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.STATUS) - for f in self.m_Table.GetFields(): self.RegisterEdit(f) diff --git a/template/bd_item_edit.py b/template/bd_item_edit.py index 2055881..e01ee8e 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -18,7 +18,23 @@ error_photo_type_message = ''' def StartEditBDItemTemplate(a_Bot, 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_Bot, a_FSM, a_FSM.item_field, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = access_mode) -def FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): +def FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): + return bd_item_add.FinishAddBDItemTemplate(a_Bot, a_FSM, a_EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type) + +def CustomEditBDItemRegisterHandlers(a_Bot, a_EditBDItemFunc, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): + keyboard = bd_item.MixKeyboardFuncTemplate(a_AdditionalButtonFunc, bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode)) + + a_Prefix = bd_item_select.SelectRegisterHandlers(a_Bot, a_SelectSource, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = access_mode) + + a_Bot.RegisterCallbackHandler(StartEditBDItemTemplate(a_Bot, a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard, a_FinishButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) + if field_type == bd_item.FieldType.photo: + a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field) + else: + a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, access_mode, field_type = field_type), state = a_FSM.item_field) + + return a_Prefix + +def EditBDItemRegisterHandlers(a_Bot, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): async def EditBDItemFunc(a_ItemData, a_UserID): item_id = a_ItemData[a_KeyName] field_value = a_ItemData[a_FieldName] @@ -30,18 +46,5 @@ def FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, await a_OnChangeFunc(item_id, a_ItemData, a_UserID) return res, error - - return bd_item_add.FinishAddBDItemTemplate(a_Bot, a_FSM, EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type) - -def EditBDItemRegisterHandlers(a_Bot, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): - keyboard = bd_item.MixKeyboardFuncTemplate(a_AdditionalButtonFunc, bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode)) - - a_Prefix = bd_item_select.SelectRegisterHandlers(a_Bot, a_SelectSource, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = access_mode) - a_Bot.RegisterCallbackHandler(StartEditBDItemTemplate(a_Bot, a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard, a_FinishButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) - if field_type == bd_item.FieldType.photo: - a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field) - else: - a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), state = a_FSM.item_field) - - return a_Prefix + return CustomEditBDItemRegisterHandlers(a_Bot, EditBDItemFunc, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, access_mode = access_mode, field_type = field_type)