From 7ef1fd3bf3f023cad9016dff86bd86cf9c068ad8 Mon Sep 17 00:00:00 2001 From: Alexei Bezborodov Date: Wed, 22 Nov 2023 13:00:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=8C,?= =?UTF-8?q?=20=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D1=83=D0=BA=D0=B0=D0=B7?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D1=82=D1=8C=20=D1=80=D0=B0=D0=B7=D0=BB=D0=B8?= =?UTF-8?q?=D1=87=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81?= =?UTF-8?q?=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D0=BE=D1=82=20parent=5F?= =?UTF-8?q?id=20#16?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/messages.py | 25 ++++++++++++++++++------- bot_modules/mod_table_operate.py | 34 +++++++++++++++++++++++++++++----- bot_modules/orders.py | 5 +++-- bot_sys/bot_bd.py | 1 + bot_sys/bot_messages.py | 31 ++++++++++++++++++++++++------- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/bot_modules/messages.py b/bot_modules/messages.py index 27ca51f..5db1dba 100644 --- a/bot_modules/messages.py +++ b/bot_modules/messages.py @@ -72,9 +72,12 @@ messages = { Время создания: #{create_datetime_field} ''', - mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.NAME): ''' + mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.NAME): f''' Создание сообщения. Шаг №1 +Вы можете добавить номер родителя ID в таком формате: ИмяСообщения{'?' + str(bd_table.TableFieldDestiny.PARENT_ID) + '=ID'} +, чтобы сообщение было только для него + Введите название сообщения: ''', mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): ''' @@ -146,12 +149,7 @@ class ModuleMessages(mod_table_operate.TableOperateModule): return self.m_Bot.SQLRequest(f'INSERT OR IGNORE 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_Message.m_PhotoID, a_Message.m_MessageName, a_Message.m_MessageDesc, access_utils.GetItemDefaultAccessForModule(self.m_Bot, module_name), lang_id)) - def FlushMessages(self): - msg = self.m_BotMessages.GetMessages() - for lang, msg_dict in msg.items(): - for msg_name, message in msg_dict.items(): - self.AddOrIgnoreMessage(message) - + def LoadAllMessages(self): table_name = self.m_Table.GetName() msgs_bd = bd_item.GetAllItemsTemplate(self.m_Bot, table_name)() if msgs_bd: @@ -163,7 +161,20 @@ class ModuleMessages(mod_table_operate.TableOperateModule): lang_name = self.GetModule(self.m_ParentModName).GetLangName(lang_id) self.m_BotMessages.CreateMessage(name, desc, self.m_Log.GetTimeNow(), a_MessagePhotoID = photo_id, a_MessageLang = lang_name) + + def FlushMessages(self): + msg = self.m_BotMessages.GetMessages() + for lang, msg_dict in msg.items(): + for msg_name, message in msg_dict.items(): + self.AddOrIgnoreMessage(message) + + self.LoadAllMessages() + self.m_BotMessages.UpdateSignal(self.m_Log.GetTimeNow()) def OnChange(self): self.FlushMessages() + + async def PostDelete(self, a_CallbackQuery, a_ItemID): + self.LoadAllMessages() # Сначала загружаем, т.к. одно сообщение удалилось + await super().PostDelete(a_CallbackQuery, a_ItemID) diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 4188812..b31642e 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -214,23 +214,47 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): a_Msg.UpdatePhotoID(','.join(photos)) return a_Msg + def GetParentID(self, a_Item, a_ItemDict, a_TableName): + parent_id = None + parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + if parent_id_field and a_ItemDict and parent_id_field in a_ItemDict: + parent_id = str(a_ItemDict[parent_id_field]) + parent_id_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID) + if not parent_id and parent_id_field_id != None and a_Item and len(a_Item) == self.m_Table.GetFieldsCount() and self.m_Table.GetName() == a_TableName: + parent_id = str(a_Item[parent_id_field_id]) + if self.m_ParentModName: + parent_mod = self.GetModule(self.m_ParentModName) + if parent_mod: + key_field_id = parent_mod.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY) + if not parent_id and key_field_id != None and a_Item and len(a_Item) == parent_mod.m_Table.GetFieldsCount() and parent_mod.m_Table.GetName() == a_TableName: + parent_id = str(a_Item[key_field_id]) + return parent_id + + def ShowMessageTemplate(self, a_Message, Inline_keyboard_template_func = None, a_EnablePhoto = False): async def ShowMessage(a_CallbackQuery, a_Item, a_ItemDict, table_name = self.m_Table.GetName()): + lang = str(a_CallbackQuery.from_user.language_code) msg = a_Message.StaticCopy() + + # Подменяем сообщение, если оно уже есть для PARENT_ID + parent_id = self.GetParentID(a_Item, a_ItemDict, table_name) + if parent_id: + name = msg.GetName() + '?' + str(bd_table.TableFieldDestiny.PARENT_ID) + '=' + parent_id + parent_id_msg = a_Message.FindMessageForLang(name, lang) + if parent_id_msg: + msg = parent_id_msg + + msg = msg.GetMessageForLang(lang).StaticCopy() # TODO: добавить поддержку языков в a_MessageName Inline_keyboard_func = None item_access = None if a_ItemDict: - lang = str(a_CallbackQuery.from_user.language_code) - msg = msg.GetMessageForLang(lang).StaticCopy() msg = self.UpdateMessageByDict(msg, lang, a_ItemDict, a_EnablePhoto = a_EnablePhoto) if a_Item and self.m_Table.GetName() == table_name: if len(a_Item) < self.m_Table.GetFieldsCount() - 1: # Для проектов это нужно. Там на 1 меньше поле. TODO разделить отправку сообщений item_access и Inline_keyboard_func return simple_message.WorkFuncResult(self.GetMessage(Messages.ERROR_FIND)) elif len(a_Item) == self.m_Table.GetFieldsCount(): - lang = str(a_CallbackQuery.from_user.language_code) - msg = msg.GetMessageForLang(lang).StaticCopy() msg = self.UpdateMessage(msg, lang, a_Item, a_EnablePhoto = a_EnablePhoto) item_access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)] if Inline_keyboard_template_func: @@ -247,7 +271,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): async def PostDelete(self, a_CallbackQuery, a_ItemID): user_id = a_CallbackQuery.from_user.id - self.m_Log.Success(f'Задача №{a_ItemID} была удалена пользователем {user_id}.') + self.m_Log.Success(f'Элемент №{a_ItemID} была удалена пользователем {user_id}.') #TODO: удалить вложенные self.OnChange() diff --git a/bot_modules/orders.py b/bot_modules/orders.py index db1f1ae..8e45746 100644 --- a/bot_modules/orders.py +++ b/bot_modules/orders.py @@ -105,8 +105,9 @@ messages = { mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.PHOTO): ''' Создание заказа. Шаг №3 -Загрузите обложку для заказа (Фото): -Она будет отображаться в его описании. +Загрузите фотографию для заказа: +(Если фотографии нет, то нажмите "⏩ Пропустить") +Фото будет отображаться в его описании. ''', mod_table_operate.Messages.SUCCESS_CREATE: '''✅ Заказ успешно добавлен!''', mod_table_operate.Messages.START_EDIT: ''' diff --git a/bot_sys/bot_bd.py b/bot_sys/bot_bd.py index 47583b2..1bf84e0 100644 --- a/bot_sys/bot_bd.py +++ b/bot_sys/bot_bd.py @@ -21,6 +21,7 @@ def RequestSelectTemplate(a_Bot, a_TableName): return SelectBD def SQLRequest(a_Log, a_BDFileName, a_Request : str, commit = False, return_error = False, param = None): + #print('SQLRequest', a_Request, commit) db = sqlite3.connect(a_BDFileName) cursor = db.cursor() result = [] diff --git a/bot_sys/bot_messages.py b/bot_sys/bot_messages.py index c032df4..41f7fc0 100644 --- a/bot_sys/bot_messages.py +++ b/bot_sys/bot_messages.py @@ -37,19 +37,36 @@ class BotMessage: msg = self.GetMessageForLang(self.m_Language) return msg.GetDesc() + def GetCurLang(self, a_Language): + msg = self.m_BotMessages.GetMessages() + if not msg.get(a_Language, None): + a_Language = self.m_Language + if not msg.get(a_Language, None): + a_Language = self.m_BotMessages.a_DefaultLanguage + return a_Language + + def FindMessageForLang(self, a_Name, a_Language): + if not self.m_BotMessages: + return None + msg = self.m_BotMessages.GetMessages() + a_Language = self.GetCurLang(a_Language) + if not msg.get(a_Language, None): + return None + new_msg = msg[a_Language].get(a_Name, None) + return new_msg + + def GetMessageForLang(self, a_Language): if not self.m_BotMessages: return self last_update = self.m_BotMessages.m_LastUpdate new_msg = self if self.m_DateTime < last_update: - msg = self.m_BotMessages.GetMessages() - if not msg.get(a_Language, None): - a_Language = self.m_Language - if not msg.get(a_Language, None): - a_Language = self.m_BotMessages.a_DefaultLanguage - new_msg = msg[a_Language].get(self.m_MessageName, self) - if a_Language == self.m_Language: + a_Language = self.GetCurLang(a_Language) + new_msg = self.FindMessageForLang(self.m_MessageName, a_Language) + if not new_msg: + new_msg = self + elif a_Language == self.m_Language: self.m_MessageDesc = new_msg.m_MessageDesc self.m_Language = new_msg.m_Language self.m_PhotoID = new_msg.m_PhotoID