From b665e7cfbe40501be9ba2ea296c2a7fba357c3b9 Mon Sep 17 00:00:00 2001 From: Alexei Date: Wed, 31 May 2023 12:22:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B8?= =?UTF-8?q?=D0=BF=20=D0=BF=D0=BE=D0=BB=D1=8F=20-=20ENUM=20=D0=90=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D0=B4=20=D0=BD=D0=B5=D0=B3=D0=BE=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20=D0=BA=D0=BD?= =?UTF-8?q?=D0=BE=D0=BF=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B8=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D1=83=D1=8E=D1=82=D1=81=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D1=83=D0=B4=D0=BE=D0=B1=D0=BE=D0=B2=D0=B0=D1=80=D0=B8=D0=BC?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/mod_table_operate.py | 41 +++++++++++++++++++++++++++++++++++++--- bot_modules/orders.py | 36 ++++++++++++++++------------------- bot_sys/bd_table.py | 37 ++++++++++++++++++++++++++++-------- template/bd_item.py | 10 ++++++++++ template/bd_item_add.py | 23 ++++++++++++---------- template/bd_item_edit.py | 14 +++++++------- 6 files changed, 113 insertions(+), 48 deletions(-) diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 3a62594..8705d8d 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -14,13 +14,19 @@ from enum import Enum from enum import auto def EditButton(a_BDTableDestiny): - return 'edit' + str(a_BDTableDestiny) + return 'edit ' + str(a_BDTableDestiny) + +def EnumButton(a_EnumItem): + return 'enum ' + str(a_EnumItem) def EditMessage(a_BDTableDestiny): - return 'edit' + str(a_BDTableDestiny) + return 'edit ' + str(a_BDTableDestiny) def CreateMessage(a_BDTableDestiny): - return 'create' + str(a_BDTableDestiny) + return 'create ' + str(a_BDTableDestiny) + +def EnumMessageForView(a_EnumItem): + return 'enum ' + str(a_EnumItem) class ButtonNames(Enum): LIST = auto() @@ -170,8 +176,15 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): photo_field = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PHOTO) if photo_field: a_Msg.UpdatePhotoID(a_Item[photo_field]) + for f in self.m_Table.GetFields(): + if f.m_Type == bd_table.TableFieldType.ENUM: + for s in f.m_Enum: + msg = self.GetMessage(EnumMessageForView(s)) + if msg: + a_Msg.UpdateDesc(a_Msg.GetDesc().replace(str(s), str(msg.GetMessageForLang(a_Lang).StaticCopy()))) return a_Msg + def ShowMessageTemplate(self, a_Message, Inline_keyboard_template_func = None): async def ShowMessage(a_CallbackQuery, a_Item): msg = a_Message.StaticCopy() @@ -268,6 +281,26 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): return a_Prefix + def AdditionalKeyboardForEditTemplate(self, a_Field): + if a_Field.m_Type == bd_table.TableFieldType.ENUM: + def KeyboardButtons(a_Message, a_UserGroups): + cur_buttons = [] + for s in a_Field.m_Enum: + cur_buttons += [keyboard.ButtonWithAccess(self.GetButton(EnumButton(s)), user_access.AccessMode.EDIT, self.GetAccess()),] + return keyboard.MakeButtons(self.m_Bot, cur_buttons, a_UserGroups) + return KeyboardButtons + return None + + def PostProccessingForFieldForEditTemplate(self, a_Field): + if a_Field.m_Type == bd_table.TableFieldType.ENUM: + def PostProccessing(a_Message): + for s in a_Field.m_Enum: + if a_Message == str(self.GetButton(EnumButton(s))): + return str(s) + return a_Message + return PostProccessing + return None + def RegisterEdit(self, a_Field, a_AccessMode = user_access.AccessMode.EDIT): a_ButtonName = self.GetButton(EditButton(a_Field.m_Destiny)) a_EditMessage = self.GetMessage(EditMessage(a_Field.m_Destiny)) @@ -300,8 +333,10 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): table_name, \ key_name, \ a_FieldName, \ + self.PostProccessingForFieldForEditTemplate(a_Field),\ GetButtonNameAndKeyValueAndAccess, \ GetAccess, \ + self.AdditionalKeyboardForEditTemplate(a_Field),\ edit_keyboard_func, \ OnChange,\ access_mode = a_AccessMode, \ diff --git a/bot_modules/orders.py b/bot_modules/orders.py index b16421e..4bf4a9b 100644 --- a/bot_modules/orders.py +++ b/bot_modules/orders.py @@ -36,7 +36,7 @@ table = bd_table.Table(table_name, [ bd_table.TableField(name_field, bd_table.TableFieldDestiny.NAME, bd_table.TableFieldType.STR), bd_table.TableField(desc_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR), bd_table.TableField(photo_field, bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldType.STR), - bd_table.TableField(status_field, bd_table.TableFieldDestiny.STATUS, bd_table.TableFieldType.STR), + bd_table.TableField(status_field, bd_table.TableFieldDestiny.STATUS, bd_table.TableFieldType.ENUM, a_Enum = OrderStatus), bd_table.TableField(address_field, bd_table.TableFieldDestiny.ADDRESS, bd_table.TableFieldType.STR), bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR), bd_table.TableField(create_datetime_field, bd_table.TableFieldDestiny.CREATE_DATE, bd_table.TableFieldType.STR), @@ -63,6 +63,10 @@ button_names = { mod_table_operate.EditButton(bd_table.TableFieldDestiny.ADDRESS): "𝌴 Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ адрСс Π² Π·Π°ΠΊΠ°Π·Π΅", mod_table_operate.EditButton(bd_table.TableFieldDestiny.STATUS): "𝌴 Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ статус Π² Π·Π°ΠΊΠ°Π·Π΅", mod_table_operate.EditButton(bd_table.TableFieldDestiny.ACCESS): "βœ‹ Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ доступ ΠΊ Π·Π°ΠΊΠ°Π·Ρƒ", + mod_table_operate.EnumButton(OrderStatus.NEW): "Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΌΠΎΠ΄Π΅Ρ€Π°Ρ†ΠΈΠΈ", + mod_table_operate.EnumButton(OrderStatus.PAY): "Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΎΠΏΠ»Π°Ρ‚Ρ‹", + mod_table_operate.EnumButton(OrderStatus.ADDRESS): "Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ уточнСния адрСса", + mod_table_operate.EnumButton(OrderStatus.FINISH): "Π—Π°ΠΊΠ°Π· Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½", mod_table_operate.ButtonNames.DEL: "❌ Π£Π΄Π°Π»ΠΈΡ‚ΡŒ Π·Π°ΠΊΠ°Π·", } @@ -156,37 +160,35 @@ messages = { } messages_order_status = { - OrderStatus.NEW: f'''Π—Π°ΠΊΠ°Π· создан, ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΌΠΎΠ΄Π΅Ρ€Π°Ρ†ΠΈΠΈ''', - OrderStatus.PAY: f'''Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΎΠΏΠ»Π°Ρ‚Ρ‹''', - OrderStatus.ADDRESS: f'''Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ указания адрСса доставки''', - OrderStatus.FINISH: f'''Π—Π°ΠΊΠ°Π· Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½''', + mod_table_operate.EnumMessageForView(OrderStatus.NEW): f'''Π—Π°ΠΊΠ°Π· создан, ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΌΠΎΠ΄Π΅Ρ€Π°Ρ†ΠΈΠΈ''', + mod_table_operate.EnumMessageForView(OrderStatus.PAY): f'''Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ ΠΎΠΏΠ»Π°Ρ‚Ρ‹''', + mod_table_operate.EnumMessageForView(OrderStatus.ADDRESS): f'''Π—Π°ΠΊΠ°Π· ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ указания адрСса доставки''', + mod_table_operate.EnumMessageForView(OrderStatus.FINISH): f'''Π—Π°ΠΊΠ°Π· Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½''', } messages.update(messages_order_status) def GetCurItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName, a_StatusFieldName): def GetBDItems(a_Message, a_UserGroups, a_ParentID): + user_id = str(a_Message.from_user.id) request = f'SELECT * FROM {a_TableName} WHERE {a_UserIDFieldName} = ? AND {a_StatusFieldName} != ?' - print('GetCurItemsTemplate', a_TableName, a_UserIDFieldName, a_KeyValue) - return a_Bot.SQLRequest(request, param = ([a_KeyValue, OrderStatus.FINISH])) + return a_Bot.SQLRequest(request, param = ([user_id, str(OrderStatus.FINISH)])) return GetBDItems def GetBDItemsForUserTemplate(a_Bot, a_TableName, a_UserIDFieldName): def GetBDItems(a_Message, a_UserGroups, a_ParentID): user_id = str(a_Message.from_user.id) - print('user_id', a_Message, user_id) return bd_item.GetBDItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName)(user_id) return GetBDItems class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): - def __init__(self, a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_ButtonName, a_OnlyCurent = False): + def __init__(self, a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_ButtonName, a_OnlyCurrent = False): super().__init__(a_Bot, a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_ButtonName) - self.m_OnlyCurent = a_OnlyCurent + self.m_OnlyCurrent = a_OnlyCurrent def GetItemsFunc(self): - get_items_func = super().GetItemsFunc() - if self.m_OnlyCurent: - get_items_func = GetCurItemsTemplate(self.m_Bot, self.m_TableName, self.m_ParentIDFieldName, status_field) + if self.m_OnlyCurrent: + return GetCurItemsTemplate(self.m_Bot, self.m_TableName, self.m_ParentIDFieldName, status_field) return GetBDItemsForUserTemplate(self.m_Bot, self.m_TableName, self.m_ParentIDFieldName) def IsFirst(self): @@ -205,7 +207,7 @@ class ModuleOrders(mod_table_operate.TableOperateModule): 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) + return DBItemForUserSelectSource(self.m_Bot, self.m_Table.GetName(), parent_id_field, a_PrevPrefix, a_ButtonName, a_OnlyCurrent = True) def AddBDItemFunc(self, a_ItemData, a_UserID): parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) @@ -221,12 +223,6 @@ class ModuleOrders(mod_table_operate.TableOperateModule): ] return parent_buttons + keyboard.MakeButtons(self.m_Bot, cur_buttons, a_UserGroups) - def UpdateMessage(self, a_Msg, a_Lang, a_Item): - a_Msg = super().UpdateMessage(a_Msg, a_Lang, a_Item) - for s in OrderStatus: - a_Msg.UpdateDesc(a_Msg.GetDesc().replace(str(s), str(self.GetMessage(s).GetMessageForLang(a_Lang).StaticCopy()))) - return a_Msg - def RegisterHandlers(self): super().RegisterHandlers() table_name = self.m_Table.GetName() diff --git a/bot_sys/bd_table.py b/bot_sys/bd_table.py index e4ed87b..6223308 100644 --- a/bot_sys/bd_table.py +++ b/bot_sys/bd_table.py @@ -8,6 +8,15 @@ from enum import auto class TableFieldType(Enum): INT = 'INTEGER' STR = 'TEXT' + ENUM = 'ENUM' + +def InitTableType(a_TableFieldType): + types = { + TableFieldType.INT: 'INTEGER', + TableFieldType.STR: 'TEXT', + TableFieldType.ENUM: 'TEXT', + } + return types.get(a_TableFieldType, None) # ΠŸΡ€Π΅Π΄Π½Π°Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ поля Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅ class TableFieldDestiny(Enum): @@ -24,10 +33,11 @@ class TableFieldDestiny(Enum): OTHER = auto() class TableField: - def __init__(self, a_Name, a_Destiny : TableFieldDestiny, a_Type : TableFieldType): + def __init__(self, a_Name, a_Destiny : TableFieldDestiny, a_Type : TableFieldType, a_Enum = None): self.m_Name = a_Name self.m_Destiny = a_Destiny self.m_Type = a_Type + self.m_Enum = a_Enum class Table: def __init__(self, a_TableName, a_Fields : [TableField], a_UniqueFields = None): @@ -67,7 +77,7 @@ class Table: request = f'CREATE TABLE IF NOT EXISTS {self.GetName()}(' items = [] for f in self.m_Fields: - item = f.m_Name + ' ' + str(f.m_Type.value) + item = f.m_Name + ' ' + str(InitTableType(f.m_Type)) if f.m_Destiny == TableFieldDestiny.KEY: item += ' PRIMARY KEY' items += [item] @@ -86,14 +96,20 @@ class Table: result = result.replace(f'#{f.m_Name}', str(a_BDItem[i])) return result +class Status(Enum): + NEW = auto() + FINISH = auto() + def Test(): f1 = TableField('f1', TableFieldDestiny.KEY, TableFieldType.INT) f2 = TableField('f2', TableFieldDestiny.NAME, TableFieldType.STR) f3 = TableField('f3', TableFieldDestiny.DESC, TableFieldType.STR) + f4 = TableField('f4', TableFieldDestiny.STATUS, TableFieldType.ENUM, a_Enum = Status) table = Table('tname', [ f1, f2, - f3 + f3, + f4, ], [[f1], [f2, f3]] ) @@ -113,16 +129,21 @@ def Test(): 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.STATUS).m_Name == 'f4' + assert table.GetFieldNameByDestiny(TableFieldDestiny.STATUS) == 'f4' + assert table.GetFieldByDestiny(TableFieldDestiny.STATUS).m_Destiny == TableFieldDestiny.STATUS + assert table.GetFieldByDestiny(TableFieldDestiny.STATUS).m_Type == TableFieldType.ENUM + assert table.GetFieldIDByDestiny(TableFieldDestiny.STATUS) == 3 assert table.GetFieldByDestiny(TableFieldDestiny.PHOTO) == None assert table.GetFieldIDByDestiny(TableFieldDestiny.PHOTO) == None assert table.GetFieldNameByDestiny(TableFieldDestiny.PHOTO) == None - assert table.GetFieldsCount() == 3 - assert len(table.GetFields()) == 3 + assert table.GetFieldsCount() == 4 + assert len(table.GetFields()) == 4 print(table.GetInitTableRequest()) - assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT, f3 TEXT, UNIQUE(f1), UNIQUE(f2, f3));' + assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT, f3 TEXT, f4 TEXT, UNIQUE(f1), UNIQUE(f2, f3));' - item = [10, 'i1', 'i2'] - assert table.ReplaceAllFieldTags('#f1 #f2 #f3', item) == '10 i1 i2' + item = [10, 'i2', 'i3', 'i4'] + assert table.ReplaceAllFieldTags('#f1 #f2 #f3 #f4', item) == '10 i2 i3 i4' diff --git a/template/bd_item.py b/template/bd_item.py index b0cadc6..5a08a84 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -42,6 +42,16 @@ def GetKeyDataFromCallbackMessage(a_Message, a_Prefix): key_item_id = str(a_Message.data).replace(a_Prefix, '') return key_item_id +def MixKeyboardFuncTemplate(a_Func1, a_Func2): + def KeyboardButtons(a_Message, a_UserGroups): + func = [a_Func1, a_Func2] + result = [] + for f in func: + if f: + result += f(a_Message, a_UserGroups) + return result + return KeyboardButtons + def GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, a_AccessMode): def GetCancelKeyboardButtons(a_Message, a_UserGroups): cur_buttons = [ diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 73cbd71..373bac3 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -39,13 +39,13 @@ def StartAddBDItemTemplate(a_Bot, a_FSM, a_FSMStart, a_MessageFunc, a_ParentTabl return res_of_work_func return simple_message.SimpleMessageTemplate(a_Bot, StartAddBDItem, a_ButtonFunc, None, a_AccessFunc, access_mode) -def FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): - return FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, True, access_mode = access_mode, field_type = field_type) +def FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): + return FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, True, access_mode = access_mode, field_type = field_type) -def NextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): - return FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, False, access_mode = access_mode, field_type = field_type) +def NextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): + return FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, False, access_mode = access_mode, field_type = field_type) -def FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, a_Finish, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): +def FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, a_Finish, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): async def FinishAddBDItem(a_Message, state): state_func = None if a_Finish: @@ -84,7 +84,10 @@ def FinishOrNextAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableNa return simple_message.WorkFuncResult(bot_messages.MakeBotMessage(error_photo_type_message), keyboard_func = a_FinishButtonFunc) field_value = a_Message.photo[0].file_id else: - field_value = a_Message.text + result = a_Message.text + if a_PostProcessFunc: + result = a_PostProcessFunc(result) + field_value = result item_data[a_FieldName] = field_value if a_Finish: res, error = a_AddBDItemFunc(item_data, user_id) @@ -104,9 +107,9 @@ def AddBDItem3RegisterHandlers(a_Bot, a_StartCheckFunc, a_FSM, a_FSMName, a_FSMD reg_func(StartAddBDItemTemplate(a_Bot, a_FSM, a_FSMName, a_AddNameMessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_ParentPrefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), a_StartCheckFunc) # TODO: Π‘Π΄Π΅Π»Π°Ρ‚ΡŒ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡ‚ΡŒ Π½Π΅ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ всС поля. ΠΏΡƒΡΡ‚ΡŒ a_FSMName ΠΈ a_NameField Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ пустыми. Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΡƒΡ‡ΡˆΠ΅ Π²ΠΎΠΎΠ±Ρ‰Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°Π²Π°Ρ‚ΡŒ список ΠΏΠΎΠ»Π΅ΠΉ ΠΈ ΠΈΡ… fsm - a_Bot.RegisterMessageHandler(NextAddBDItemTemplate(a_Bot, a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_NameField, a_AddDescMessageFunc, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName) - a_Bot.RegisterMessageHandler(NextAddBDItemTemplate(a_Bot, a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_DescField, a_AddPhotoMessageFunc, a_AccessFunc, keyboard_skip_and_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMDesc) - a_Bot.RegisterMessageHandler(FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo', 'text'], state = a_FSMPhoto) + a_Bot.RegisterMessageHandler(NextAddBDItemTemplate(a_Bot, a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_NameField, a_AddDescMessageFunc, None, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName) + a_Bot.RegisterMessageHandler(NextAddBDItemTemplate(a_Bot, a_FSM, None, a_ParentTableName, a_ParentKeyFieldName, a_DescField, a_AddPhotoMessageFunc, None, a_AccessFunc, keyboard_skip_and_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMDesc) + a_Bot.RegisterMessageHandler(FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_PhotoField, a_FinishMessageFunc, None, a_AccessFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo', 'text'], state = a_FSMPhoto) def AddBDItem1RegisterHandlers(a_Bot, a_StartCheckFunc, a_FSM, a_AddBDItemFunc, a_AddMessageFunc, a_FinishMessageFunc, a_ParentPrefix, a_ParentTableName : str, a_ParentKeyFieldName, a_FieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, a_FieldType, access_mode = user_access.AccessMode.ADD): keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode) @@ -114,7 +117,7 @@ def AddBDItem1RegisterHandlers(a_Bot, a_StartCheckFunc, a_FSM, a_AddBDItemFunc, if a_ParentTableName: reg_func = a_Bot.RegisterCallbackHandler reg_func(StartAddBDItemTemplate(a_Bot, a_FSM, a_FSM.bd_item, a_AddMessageFunc, a_ParentTableName, a_ParentKeyFieldName, a_ParentPrefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), a_StartCheckFunc) - finish_handler = FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = a_FieldType) + finish_handler = FinishAddBDItemTemplate(a_Bot, a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_FinishMessageFunc, None, a_AccessFunc, a_ButtonFunc, access_mode, field_type = a_FieldType) if a_FieldType == bd_item.FieldType.photo: a_Bot.RegisterMessageHandler(finish_handler, content_types = ['photo', 'text'], state = a_FSM.bd_item) else: diff --git a/template/bd_item_edit.py b/template/bd_item_edit.py index 972c715..8b85a66 100644 --- a/template/bd_item_edit.py +++ b/template/bd_item_edit.py @@ -18,7 +18,7 @@ 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_AccessFunc, a_ButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): +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 EditBDItemFunc(a_ItemData, a_UserID): item_id = a_ItemData[a_KeyName] field_value = a_ItemData[a_FieldName] @@ -31,15 +31,15 @@ def FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_OnChangeFunc() return res, error - return bd_item_add.FinishAddBDItemTemplate(a_Bot, a_FSM, EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type) + 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 : str, a_KeyName, a_FieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): - keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode) +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_cancel, a_FinishButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) + 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_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field) + 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_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), state = a_FSM.item_field) + 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)