From 95e0ff33efbbabc103d2a3426356de2d52ac5641 Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 1 May 2023 15:37:50 +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=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D1=81=D0=BE=D0=BE=D0=B1?= =?UTF-8?q?=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=85=D0=BE=D0=B4=D1=83!=20=D0=9F=D0=BE=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D1=83=D0=BB=D0=B5=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/languages.py | 6 +++- bot_modules/messages.py | 7 +++-- bot_modules/profile.py | 14 +++++++--- bot_modules/projects.py | 72 ++++++++++++++++++++++++++---------------------- bot_modules/start.py | 11 ++++---- bot_sys/log.py | 5 +++- bot_sys/user_messages.py | 26 +++++++++++++---- main.py | 4 ++- 8 files changed, 91 insertions(+), 54 deletions(-) diff --git a/bot_modules/languages.py b/bot_modules/languages.py index 0f73e90..0d57b88 100644 --- a/bot_modules/languages.py +++ b/bot_modules/languages.py @@ -259,15 +259,18 @@ def AddOrIgnoreLang(a_Lang : str): g_languages = {} def GetLangID(a_Lang): + global g_languages return g_languages.get(a_Lang, None) def GetLangName(a_LangID): + global g_languages for lang_name, lang_id in g_languages: if a_LangID == lang_id: return lang_name - return None + return user_messages.default_language def FlushLanguages(): + global g_languages msg = user_messages.GetMessages() for lang, msg_dict in msg.items(): AddOrIgnoreLang(lang) @@ -275,6 +278,7 @@ def FlushLanguages(): if langs: for l in langs: g_languages[l[1]] = str(l[0]) + print('FlushLanguages', g_languages) # Инициализация БД def GetInitBDCommands(): diff --git a/bot_modules/messages.py b/bot_modules/messages.py index d3580f0..ca2d763 100644 --- a/bot_modules/messages.py +++ b/bot_modules/messages.py @@ -216,6 +216,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): if message_success_edit_message == a_StringMessage: FlushMessages() + # TODO FlushMessages происходит рано. Нужно после изменений msg = a_StringMessage.\ replace(f'#{name_field}', a_Item[1]).\ replace(f'#{desc_field}', a_Item[2]).\ @@ -278,13 +279,13 @@ def FlushMessages(): if msgs_bd: for m in msgs_bd: name = m[1] - lang_id = m[5] + lang_id = m[6] lang_name = languages.GetLangName(lang_id) - new_msg = user_messages.Message(name, m[2], lang_name, m[3]) + new_msg = user_messages.Message(name, m[2], lang_name, m[3], log.GetTimeNow()) if not msg.get(lang_name, None): msg[lang_name] = {} msg[lang_name][name] = new_msg - user_messages.UpdateMSG(new_msg) + user_messages.UpdateSignal(log.GetTimeNow()) # Инициализация БД def GetInitBDCommands(): diff --git a/bot_modules/profile.py b/bot_modules/profile.py index 477fb44..0b027f2 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -3,7 +3,7 @@ # Профиль пользователя -from bot_sys import bot_bd, log, config, keyboard, user_access +from bot_sys import bot_bd, log, config, keyboard, user_access, user_messages from bot_modules import start, access, groups from template import simple_message @@ -38,10 +38,16 @@ init_bd_cmds = [f"""CREATE TABLE IF NOT EXISTS {table_name}( f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_new}=+', '{user_access.user_access_group_new}=+');" ] +def MSG(a_MessageName, a_MessageDesc): + def UpdateMSG(a_Message : user_messages.Message): + print(a_Message.m_MessageName, a_Message.m_MessageDesc) + globals()[a_Message.m_MessageName] = a_Message + user_messages.MSG(a_MessageName, a_MessageDesc, UpdateMSG, log.GetTimeNow()) + # --------------------------------------------------------- # Сообщения -profile_message = f''' +MSG('profile_message', f''' 📰 Профиль: ID: #{key_name} @@ -50,7 +56,7 @@ profile_message = f''' Имя2: #{name2_field} Код языка: #{language_code_field} Дата добавления: #{create_datetime_field} -''' +''') user_profile_button_name = "📰 Профиль" @@ -68,7 +74,7 @@ async def ProfileOpen(a_Message, state = None): user_info = GetUserInfo(a_Message.from_user.id) msg = profile_message if not user_info is None: - msg = msg.\ + msg = str(msg).\ replace(f'#{key_name}', str(user_info[0])).\ replace(f'#{name_field}', str(user_info[1])).\ replace(f'#{name1_field}', str(user_info[2])).\ diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 85ee494..74c5135 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -3,7 +3,7 @@ # Проекты -from bot_sys import bot_bd, log, keyboard, user_access +from bot_sys import bot_bd, log, keyboard, user_access, user_messages from bot_modules import start, access, groups, tasks, needs, comments from template import bd_item_view, simple_message, bd_item_delete, bd_item_edit, bd_item, bd_item_add @@ -53,110 +53,116 @@ init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}( f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_new}=va', '{user_access.user_access_group_new}=va');" ] +def MSG(a_MessageName, a_MessageDesc): + def UpdateMSG(a_Message : user_messages.Message): + print(a_Message.m_MessageName, a_Message.m_MessageDesc) + globals()[a_Message.m_MessageName] = a_Message + user_messages.MSG(a_MessageName, a_MessageDesc, UpdateMSG, log.GetTimeNow()) + # --------------------------------------------------------- # Сообщения projects_button_name = "🟥 Проекты" -base_project_message = ''' +MSG('base_project_message',''' 🟥 Проекты -''' +''') list_project_button_name = "📃 Список проектов" -select_project_message = ''' +MSG('select_project_message',''' Пожалуйста, выберите проект: -''' +''') -error_find_proj_message = ''' +MSG('error_find_proj_message',''' ❌ Ошибка, проект не найден -''' +''') -project_open_message = f''' +MSG('project_open_message',f''' Проект: #{name_field} #{desc_field} Время создания: #{create_datetime_field} -''' +''') # Создание проекта add_project_button_name = "✅ Добавить проект" -project_create_name_message = ''' +MSG('project_create_name_message',''' Создание проекта. Шаг №1 Введите название проекта: -''' +''') -project_create_desc_message = ''' +MSG('project_create_desc_message',''' Создание проекта. Шаг №2 Введите описание проекта: -''' +''') -project_create_photo_message = ''' +MSG('project_create_photo_message',''' Создание проекта. Шаг №3 Загрузите обложку для проекта (Фото): Она будет отображаться в его описании. -''' +''') -project_success_create_message = '''✅ Проект успешно добавлен!''' +MSG('project_success_create_message','''✅ Проект успешно добавлен!''') # Редактирование проекта. edit_project_button_name = "🛠 Редактировать проект" -project_start_edit_message= ''' +MSG('project_start_edit_message', ''' Пожалуйста, выберите действие: -''' +''') -project_select_to_edit_message = ''' +MSG('project_select_to_edit_message',''' Выберите проект, который вы хотите отредактировать. -''' +''') edit_project_photo_button_name = "☐ Изменить изображение в проекте" -project_edit_photo_message = ''' +MSG('project_edit_photo_message',''' Загрузите новую обложку для проекта (Фото): Она будет отображаться в его описании. -''' +''') edit_project_name_button_name = "≂ Изменить название в проекте" -project_edit_name_message = f''' +MSG('project_edit_name_message',f''' Текущее название проекта: #{name_field} Введите новое название проекта: -''' +''') edit_project_desc_button_name = "𝌴 Изменить описание в проекте" -project_edit_desc_message = f''' +MSG('project_edit_desc_message',f''' Текущее описание проекта: #{desc_field} Введите новое описание проекта: -''' +''') edit_project_access_button_name = "✋ Изменить доступ к проекту" -project_edit_access_message = f''' +MSG('project_edit_access_message',f''' Текущий доступ к проекту: #{access_field} {user_access.user_access_readme} Введите новую строку доступа: -''' +''') -project_success_edit_message = '''✅ Проект успешно отредактирован!''' +MSG('project_success_edit_message','''✅ Проект успешно отредактирован!''') # Удаление проекта del_project_button_name = "❌ Удалить проект" -project_select_to_delete_message = ''' +MSG('project_select_to_delete_message',''' Выберите проект, который вы хотите удалить. Все задачи и потребности в этом проекте так же будут удалены! -''' +''') -project_success_delete_message = '''✅ Проект успешно удалён!''' +MSG('project_success_delete_message','''✅ Проект успешно удалён!''') # --------------------------------------------------------- # Работа с кнопками @@ -205,7 +211,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): if (len(a_Item) < 6): return simple_message.WorkFuncResult(error_find_proj_message) - msg = a_StringMessage.\ + msg = str(a_StringMessage).\ replace(f'#{name_field}', a_Item[1]).\ replace(f'#{desc_field}', a_Item[2]).\ replace(f'#{create_datetime_field}', a_Item[5]).\ diff --git a/bot_modules/start.py b/bot_modules/start.py index f8def01..52e77fe 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -9,10 +9,11 @@ from template import simple_message from aiogram.dispatcher import Dispatcher -MSG = user_messages.MSG -def UpdateMSG(a_Message : user_messages.Message): - print(a_Message.m_MessageName, a_Message.m_MessageDesc) - globals()[a_Message.m_MessageName] = a_Message +def MSG(a_MessageName, a_MessageDesc): + def UpdateMSG(a_Message : user_messages.Message): + print(a_Message.m_MessageName, a_Message.m_MessageDesc) + globals()[a_Message.m_MessageName] = a_Message + user_messages.MSG(a_MessageName, a_MessageDesc, UpdateMSG, log.GetTimeNow()) # --------------------------------------------------------- # БД @@ -29,7 +30,7 @@ MSG('start_message', ''' Добро пожаловать! Выберите возможные действия на кнопках ниже ⌨''' -, UpdateMSG) +) start_menu_button_name = "☰ Главное меню" diff --git a/bot_sys/log.py b/bot_sys/log.py index 4695807..cfb28e3 100644 --- a/bot_sys/log.py +++ b/bot_sys/log.py @@ -18,8 +18,11 @@ import colorama import datetime colorama.init() +def GetTimeNow(): + return datetime.datetime.now() + def GetTime(): - now = datetime.datetime.now() + now = GetTimeNow() time = now.strftime(f"[%d.%m.%Y, %H:%M]") return time diff --git a/bot_sys/user_messages.py b/bot_sys/user_messages.py index 46a2b99..61e39b2 100644 --- a/bot_sys/user_messages.py +++ b/bot_sys/user_messages.py @@ -15,21 +15,38 @@ def GetMessages(): g_messages = {} return g_messages +g_last_update = None +def UpdateSignal(a_DateTime): + global g_last_update + g_last_update = a_DateTime # --------------------------------------------------------- # Функции работы с собщениями # --------------------------------------------------------- class Message: - def __init__(self, a_MessageName : str, a_MessageDesc : str, a_Language : str, a_PhotoID : str): + def __init__(self, a_MessageName : str, a_MessageDesc : str, a_Language : str, a_PhotoID : str, a_DateTime): self.m_MessageName = a_MessageName self.m_MessageDesc = a_MessageDesc self.m_Language = a_Language self.m_PhotoID = a_PhotoID + self.m_DateTime = a_DateTime + def __str__(self): + global g_last_update + last_update = g_last_update + if self.m_DateTime < last_update: + msg = GetMessages() + if not msg.get(self.m_Language, None): + msg[self.m_Language] = {} + new_msg = msg[self.m_Language].get(self.m_MessageName, self) + self.m_MessageName = new_msg.m_MessageName + self.m_MessageDesc = new_msg.m_MessageDesc + self.m_PhotoID = new_msg.m_PhotoID + self.m_DateTime = last_update return f'{self.m_MessageDesc}' -def MSG(a_MessageName, a_MessageDesc, a_UpdateMSG): - cur_msg = Message(a_MessageName, a_MessageDesc, default_language, 0) +def MSG(a_MessageName, a_MessageDesc, a_UpdateMSG, a_DateTime): + cur_msg = Message(a_MessageName, a_MessageDesc, default_language, 0, a_DateTime) msg = GetMessages() if not msg.get(default_language, None): msg[default_language] = {} @@ -37,6 +54,3 @@ def MSG(a_MessageName, a_MessageDesc, a_UpdateMSG): msg[default_language][a_MessageName] = cur_msg a_UpdateMSG(cur_msg) -def UpdateMSG(a_Message : Message): - print(a_Message.m_MessageName, a_Message.m_MessageDesc) - globals()[a_Message.m_MessageName] = a_Message diff --git a/main.py b/main.py index 4b74566..f91a227 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from aiogram.utils import executor from aiogram.dispatcher import Dispatcher from aiogram.contrib.fsm_storage.memory import MemoryStorage import sqlite3 -from bot_sys import config, log, bot_bd, user_access +from bot_sys import config, log, bot_bd, user_access, user_messages from bot_modules import profile, start, projects, groups, access, backup, tasks, needs, comments, messages, languages storage = MemoryStorage() @@ -27,6 +27,8 @@ for m in modules: # Первичная инициализация базы данных bot_bd.BDExecute(init_bd_cmd) +user_messages.UpdateSignal(log.GetTimeNow()) + languages.FlushLanguages() messages.FlushMessages()