From 085ac9b8c8db5d233621c07b1023938b97aaabfa Mon Sep 17 00:00:00 2001 From: Alexei Date: Sun, 5 Nov 2023 17:06:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=B0=D1=82=D0=B5=D0=B3=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=20#11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/all_orders.py | 20 +++--- bot_modules/mod_table_operate.py | 5 +- bot_modules/orders.py | 27 ++++---- bot_modules/orders_cat.py | 135 +++++++++++++++++++++++++++++++++++++++ main.py | 13 ++-- template/bd_item.py | 2 +- 6 files changed, 171 insertions(+), 31 deletions(-) create mode 100644 bot_modules/orders_cat.py diff --git a/bot_modules/all_orders.py b/bot_modules/all_orders.py index 9f40bb9..efdbc2d 100644 --- a/bot_modules/all_orders.py +++ b/bot_modules/all_orders.py @@ -62,7 +62,7 @@ cur_messages = { Статус: #{orders.status_field} -Пользователь: #{orders.parent_id_field} +Пользователь: #{orders.user_id_field} Адрес доставки: #{orders.address_field} @@ -85,15 +85,16 @@ messages.update(orders.messages_order_status) messages.update(messages_notification) messages.update(cur_messages) -def GetCurItemsTemplate(a_Bot, a_TableName, a_StatusFieldName): +def GetCurItemsTemplate(a_Bot, a_TableName, a_StatusFieldName, a_ParentIDFieldName): def GetBDItems(a_Message, a_UserGroups, a_ParentID): - request = f'SELECT * FROM {a_TableName} WHERE {a_StatusFieldName} != ?' - return a_Bot.SQLRequest(request, param = ([str(orders.OrderStatus.FINISH)])) + request = f'SELECT * FROM {a_TableName} WHERE {a_ParentIDFieldName} = ? AND {a_StatusFieldName} != ?' + return a_Bot.SQLRequest(request, param = ([a_ParentID, str(orders.OrderStatus.FINISH)])) return GetBDItems -def GetBDItemsForUserTemplate(a_Bot, a_TableName): +def GetBDItemsForUserTemplate(a_Bot, a_TableName, a_ParentIDFieldName): def GetBDItems(a_Message, a_UserGroups, a_ParentID): - return bd_item.GetAllItemsTemplate(a_Bot, a_TableName)() + request = f'SELECT * FROM {a_TableName} WHERE {a_ParentIDFieldName} = ?' + return a_Bot.SQLRequest(request, param = ([a_ParentID])) return GetBDItems class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): @@ -103,11 +104,8 @@ class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): def GetItemsFunc(self): if self.m_OnlyCurrent: - return GetCurItemsTemplate(self.m_Bot, self.m_TableName, orders.status_field) - return GetBDItemsForUserTemplate(self.m_Bot, self.m_TableName) - - def IsFirst(self): - return True + return GetCurItemsTemplate(self.m_Bot, self.m_TableName, orders.status_field, self.m_ParentIDFieldName) + return GetBDItemsForUserTemplate(self.m_Bot, self.m_TableName, self.m_ParentIDFieldName) class ModuleAllOrders(orders.ModuleOrders): def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log): diff --git a/bot_modules/mod_table_operate.py b/bot_modules/mod_table_operate.py index 5c257c4..6e3505b 100644 --- a/bot_modules/mod_table_operate.py +++ b/bot_modules/mod_table_operate.py @@ -209,7 +209,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): return a_Msg def ShowMessageTemplate(self, a_Message, Inline_keyboard_template_func = None, a_EnablePhoto = False): - async def ShowMessage(a_CallbackQuery, a_Item, a_ItemDict): + async def ShowMessage(a_CallbackQuery, a_Item, a_ItemDict, table_name = self.m_Table.GetName()): msg = a_Message.StaticCopy() # TODO: добавить поддержку языков в a_MessageName Inline_keyboard_func = None @@ -218,7 +218,8 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule): 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: + + 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(): diff --git a/bot_modules/orders.py b/bot_modules/orders.py index 1c4a973..be1e655 100644 --- a/bot_modules/orders.py +++ b/bot_modules/orders.py @@ -22,6 +22,7 @@ module_name = 'orders' table_name = module_name key_name = 'orderID' +user_id_field = 'userID' name_field = 'orderName' desc_field = 'orderDesc' photo_field = 'orderPhoto' @@ -30,10 +31,11 @@ status_field = 'orderStatus' address_field = 'orderAddress' access_field = 'orderAccess' create_datetime_field = 'orderCreateDateTime' -parent_id_field = 'userID' +parent_id_field = 'catID' table = bd_table.Table(table_name, [ bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT), + bd_table.TableField(user_id_field, bd_table.TableFieldDestiny.USER_ID, bd_table.TableFieldType.INT), 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.PHOTO), @@ -134,7 +136,7 @@ messages = { ''', mod_table_operate.EditMessage(bd_table.TableFieldDestiny.ADDRESS): f''' Текущий адрес заказа: -#{desc_field} +#{address_field} Введите новый адрес доставки заказа (укажите, кто, когда и где его сможет забрать): ''', @@ -173,17 +175,19 @@ messages_subscribes = { messages.update(messages_subscribes) -def GetCurItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName, a_StatusFieldName): +def GetCurItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName, a_ParentIDFieldName, 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} != ?' - return a_Bot.SQLRequest(request, param = ([user_id, str(OrderStatus.FINISH)])) + request = f'SELECT * FROM {a_TableName} WHERE {a_ParentIDFieldName} = ? AND {a_UserIDFieldName} = ? AND {a_StatusFieldName} != ?' + return a_Bot.SQLRequest(request, param = ([a_ParentID, user_id, str(OrderStatus.FINISH)])) return GetBDItems -def GetBDItemsForUserTemplate(a_Bot, a_TableName, a_UserIDFieldName): +def GetBDItemsForUserTemplate(a_Bot, a_TableName, a_UserIDFieldName, a_ParentIDFieldName): def GetBDItems(a_Message, a_UserGroups, a_ParentID): user_id = str(a_Message.from_user.id) - return bd_item.GetBDItemsTemplate(a_Bot, a_TableName, a_UserIDFieldName)(user_id) + request = f'SELECT * FROM {a_TableName} WHERE {a_ParentIDFieldName} = ? AND {a_UserIDFieldName} = ?' + print('GetBDItemsForUserTemplate', user_id, request, a_ParentID) + return a_Bot.SQLRequest(request, param = ([a_ParentID, user_id])) return GetBDItems class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): @@ -193,11 +197,8 @@ class DBItemForUserSelectSource(bd_item_select.DBItemSelectSource): def GetItemsFunc(self): 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): - return True + return GetCurItemsTemplate(self.m_Bot, self.m_TableName, user_id_field, self.m_ParentIDFieldName, status_field) + return GetBDItemsForUserTemplate(self.m_Bot, self.m_TableName, user_id_field, self.m_ParentIDFieldName) class ModuleOrders(mod_table_operate.TableOperateModule): 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): @@ -213,7 +214,7 @@ class ModuleOrders(mod_table_operate.TableOperateModule): 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 + a_ItemData[user_id_field] = a_UserID a_ItemData[status_field] = str(OrderStatus.NEW) a_ItemData[address_field] = '' a_ItemData[photo_pay_field] = '0' diff --git a/bot_modules/orders_cat.py b/bot_modules/orders_cat.py new file mode 100644 index 0000000..b93c29b --- /dev/null +++ b/bot_modules/orders_cat.py @@ -0,0 +1,135 @@ +# -*- coding: utf8 -*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +# Проекты + +from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes +from bot_modules import mod_table_operate, mod_simple_message + +# --------------------------------------------------------- +# БД +module_name = 'orders_cat' + +table_name = module_name +key_name = 'catID' +name_field = 'catName' +desc_field = 'catDesc' +photo_field = 'catPhoto' +access_field = 'catAccess' +create_datetime_field = 'catCreateDateTime' + +table = bd_table.Table(table_name, [ + bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT), + 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.PHOTO), + 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), + ]) + +init_access = f'{user_access.user_access_group_new}=v' + +# --------------------------------------------------------- +# Сообщения и кнопки + +button_names = { + mod_simple_message.ButtonNames.START: "🟥 Категории", + mod_table_operate.ButtonNames.LIST: "📃 Список категорий", + mod_table_operate.ButtonNames.ADD: "✅ Добавить категорию", + mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать категорию", + mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в категории", + mod_table_operate.EditButton(bd_table.TableFieldDestiny.NAME): "≂ Изменить название в категории", + mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание в категории", + mod_table_operate.EditButton(bd_table.TableFieldDestiny.ACCESS): "✋ Изменить доступ к категории", + mod_table_operate.ButtonNames.DEL: "❌ Удалить категорию", +} + +messages = { + mod_simple_message.Messages.START: f''' +{button_names[mod_simple_message.ButtonNames.START]} + +''', + mod_table_operate.Messages.SELECT: ''' +Пожалуйста, выберите категорию: +''', + mod_table_operate.Messages.ERROR_FIND: ''' +❌ Ошибка, категория не найдена +''', + mod_table_operate.Messages.OPEN: f''' +Категория: #{name_field} + +#{desc_field} + +Время создания: #{create_datetime_field} +''', + mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.NAME): ''' +Создание категории. Шаг №1 + +Введите название категории: +''', + mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): ''' +Создание категории. Шаг №2 + +Введите описание категории: +''', + mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.PHOTO): ''' +Создание категории. Шаг №3 + +Загрузите обложку для категории (Фото): +Она будет отображаться в его описании. +''', + mod_table_operate.Messages.SUCCESS_CREATE: '''✅ Категория успешно добавлена!''', + mod_table_operate.Messages.START_EDIT: ''' +Пожалуйста, выберите действие: +''', + mod_table_operate.Messages.SELECT_TO_EDIT: ''' +Выберите проект, который вы хотите отредактировать. +''', + mod_table_operate.EditMessage(bd_table.TableFieldDestiny.PHOTO): ''' +Загрузите новую обложку для категории (Фото): +Она будет отображаться в его описании. +''', + mod_table_operate.EditMessage(bd_table.TableFieldDestiny.NAME): f''' +Текущее название категории: +#{name_field} + +Введите новое название категории: +''', + mod_table_operate.EditMessage(bd_table.TableFieldDestiny.DESC): f''' +Текущее описание категории: +#{desc_field} + +Введите новое описание категории: +''', + mod_table_operate.EditMessage(bd_table.TableFieldDestiny.ACCESS): f''' +Текущий доступ к категории: +#{access_field} + +{user_access.user_access_readme} + +Введите новую строку доступа: +''', + mod_table_operate.Messages.SUCCESS_EDIT: '''✅ Категория успешно отредактирована!''', + mod_table_operate.Messages.SELECT_TO_DELETE: ''' +Выберите категорию, которую вы хотите удалить. +''', + mod_table_operate.Messages.SUCCESS_DELETE: '''✅ Категория успешно удалёна!''', +} + +messages_subscribes = { + mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ADD):f'''Категория создана''', + mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT):f'''Категория отредактирована''', + mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL):f'''Категория удалена''', + mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_EDIT):f'''Категория отредактирована #item_id''', + mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_DEL):f'''Категория удалена #item_id''', +} + +messages.update(messages_subscribes) + +class ModuleOrdersCat(mod_table_operate.TableOperateModule): + def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log): + super().__init__(table, messages, button_names, a_ParentModName, a_ChildModName, init_access, init_access, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log) + + def GetName(self): + return module_name + diff --git a/main.py b/main.py index b064418..4a0e35a 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ import os from bot_sys import config, log, aiogram_bot, bot_messages, bd_table, user_access from bot_modules import mod_agregator, start, profile, backup, users_groups_agregator, access, projects, tasks, needs, comments from bot_modules import languages, messages, buttons, users, groups, user_in_groups -from bot_modules import orders, all_orders, authorize +from bot_modules import orders_cat, orders, all_orders, authorize from bot_modules import bd_version from bot_sys import bot_subscribes @@ -40,6 +40,7 @@ mod_buttons_name = buttons.module_name mod_users_name = users.module_name mod_groups_name = groups.module_name mod_user_in_groups_name = user_in_groups.module_name +mod_orders_cat_name = orders_cat.module_name mod_orders_name = orders.module_name mod_all_orders_name = all_orders.module_name @@ -96,11 +97,15 @@ mod_buttons = buttons.ModuleButtons(mod_languages_name, None, child_mod_name_lis g_ModuleAgregator.AddModule(mod_buttons) child_mod_name_list = [mod_start_name] -mod_orders = orders.ModuleUserOrders(None, None, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) -g_ModuleAgregator.AddModule(mod_orders) +mod_orders_cat = orders_cat.ModuleOrdersCat(None, mod_orders_name, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) +g_ModuleAgregator.AddModule(mod_orders_cat) child_mod_name_list = [mod_start_name] -mod_all_orders = all_orders.ModuleAllOrders(None, None, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) +mod_orders = orders.ModuleUserOrders(mod_orders_cat_name, None, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) +g_ModuleAgregator.AddModule(mod_orders) + +child_mod_name_list = [mod_start_name, mod_orders_cat_name] +mod_all_orders = all_orders.ModuleAllOrders(mod_orders_cat_name, None, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) g_ModuleAgregator.AddModule(mod_all_orders) child_mod_name_list = [mod_start_name] diff --git a/template/bd_item.py b/template/bd_item.py index 8cc9783..edfbf83 100644 --- a/template/bd_item.py +++ b/template/bd_item.py @@ -100,7 +100,7 @@ def CheckAccessBDItemTemplate(a_Bot, a_TableName, a_KeyName, a_KeyValue, a_WorkF a_Bot.GetLog().Error(msg) return simple_message.WorkFuncResult(bot_messages.MakeBotMessage(msg)), None - result_work_func = await a_WorkFunc(a_CallbackQuery, item[0], None) + result_work_func = await a_WorkFunc(a_CallbackQuery, item[0], None, table_name = a_TableName) if result_work_func is None or result_work_func.m_BotMessage is None: return result_work_func, result_work_func