From 74db81acb7b16e49a07518f86247d404edfbc026 Mon Sep 17 00:00:00 2001 From: Alexei Date: Tue, 25 Apr 2023 11:55:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A8=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=91=D0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/access.py | 8 ++++---- bot_modules/backup.py | 2 +- bot_modules/groups.py | 16 +++++++++++----- bot_modules/profile.py | 4 ++-- bot_sys/bot_bd.py | 41 ++++++++--------------------------------- 5 files changed, 26 insertions(+), 45 deletions(-) diff --git a/bot_modules/access.py b/bot_modules/access.py index 9277e9d..9c1e7ba 100644 --- a/bot_modules/access.py +++ b/bot_modules/access.py @@ -15,7 +15,7 @@ from aiogram.dispatcher import Dispatcher bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) -class FSMRequestToBD(StatesGroup): +class FSMRequestToBDAccess(StatesGroup): sqlRequest = State() # --------------------------------------------------------- @@ -116,8 +116,8 @@ def GetModuleButtons(): def RegisterHandlers(dp : Dispatcher): dp.register_message_handler(AccessStart, text = access_button_name) - dp.register_message_handler(groups.RequestToBDTemplate(request_start_message, GetAccess, FSMRequestToBD), text = sql_request_button_name) - dp.register_message_handler(groups.RequestToBDCancelTemplate(GetEditAccessKeyboardButtons, GetAccess), text = groups.canсel_button_name, state = FSMRequestToBD.sqlRequest) - dp.register_message_handler(groups.RequestToBDFinishTemplate(GetEditAccessKeyboardButtons, GetAccess), state = FSMRequestToBD.sqlRequest) + dp.register_message_handler(groups.RequestToBDTemplate(request_start_message, GetAccess, FSMRequestToBDAccess), text = sql_request_button_name) + dp.register_message_handler(groups.RequestToBDCancelTemplate(GetEditAccessKeyboardButtons, GetAccess), text = groups.canсel_button_name, state = FSMRequestToBDAccess.sqlRequest) + dp.register_message_handler(groups.RequestToBDFinishTemplate(GetEditAccessKeyboardButtons, GetAccess), state = FSMRequestToBDAccess.sqlRequest) dp.register_message_handler(groups.HelpTemplate(help_message, GetEditAccessKeyboardButtons, GetAccess), text = help_button_name) diff --git a/bot_modules/backup.py b/bot_modules/backup.py index f49eade..05a4d08 100644 --- a/bot_modules/backup.py +++ b/bot_modules/backup.py @@ -19,7 +19,7 @@ bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML module_name = 'backup' init_bd_cmds = [ -f"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('{module_name}', 'other=+');" +f"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('{module_name}', 'other=-');" ] # --------------------------------------------------------- diff --git a/bot_modules/groups.py b/bot_modules/groups.py index 047790a..22671c4 100644 --- a/bot_modules/groups.py +++ b/bot_modules/groups.py @@ -139,8 +139,12 @@ def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc): async with state.proxy() as prjData: sql_request = a_Message.text log.Success(f'Сделан запрос [{sql_request}] пользователем {a_Message.from_user.id}.') - result = bot_bd.SQLRequestToBDCommit(sql_request) - log.Success(f'Результат запроса [{sql_request}] от пользователя {a_Message.from_user.id} следующий [{result}].') + result, error = bot_bd.SQLRequestToBD(sql_request, commit = True, return_error = True) + if not error is None: + log.Error(f'Ошибка при выполнении запроса [{sql_request}] от пользователя {a_Message.from_user.id} ответ следующий [{str(error)}].') + result = str(error) + else: + log.Success(f'Результат запроса [{sql_request}] от пользователя {a_Message.from_user.id} следующий [{result}].') await state.finish() await a_Message.answer(str(result), reply_markup = a_GetButtonsFunc(user_groups)) return RequestToBDFinish @@ -149,16 +153,18 @@ def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc): # Работа с базой данных групп def GetGroupIDForUser(a_UserID): - return bot_bd.SQLRequestToBD1('SELECT group_id FROM user_in_groups WHERE user_id = ?', [a_UserID]) + return bot_bd.SQLRequestToBD('SELECT group_id FROM user_in_groups WHERE user_id = ?', param = [a_UserID]) def GetGroupNamesForUser(a_UserID): - return bot_bd.SQLRequestToBD1('SELECT groupName FROM user_groups WHERE group_id=(SELECT group_id FROM user_in_groups WHERE user_id = ?)', [a_UserID]) + return bot_bd.SQLRequestToBD('SELECT groupName FROM user_groups WHERE group_id=(SELECT group_id FROM user_in_groups WHERE user_id = ?)', param = [a_UserID]) def GetUserGroupData(a_UserID): r = GetGroupNamesForUser(a_UserID) groups = [] for i in r: - groups += [i[0]] + if len(i) > 0: + groups += [i[0]] + print(groups) return user_access.UserGroups(a_UserID, groups) # --------------------------------------------------------- diff --git a/bot_modules/profile.py b/bot_modules/profile.py index 9f06e59..1bac0df 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -65,11 +65,11 @@ async def ProfileOpen(a_Message): # Добавление пользователя, если он уже есть, то игнорируем def AddUser(a_UserID, a_UserName): - bot_bd.SQLRequestToBD2Commit("INSERT OR IGNORE INTO users (user_id, userName) VALUES (?, ?);", a_UserID, a_UserName) + bot_bd.SQLRequestToBD("INSERT OR IGNORE INTO users (user_id, userName) VALUES (?, ?);", commit=True, param = (a_UserID, a_UserName)) # Добавление пользователя, если он уже есть, то игнорируем def GetUserInfo(a_UserID): - user_info = bot_bd.SQLRequestToBD1('SELECT * FROM users WHERE user_id = ?', [a_UserID]) + user_info = bot_bd.SQLRequestToBD('SELECT * FROM users WHERE user_id = ?', param = [a_UserID]) print(user_info, str(user_info)) if len(user_info) != 0: return user_info[0] diff --git a/bot_sys/bot_bd.py b/bot_sys/bot_bd.py index 4377344..f07469c 100644 --- a/bot_sys/bot_bd.py +++ b/bot_sys/bot_bd.py @@ -31,49 +31,24 @@ def SelectBDTemplate(a_TableName): return SQLRequestToBD(f'SELECT * FROM {a_TableName}') return SelectBD -def SQLRequestToBDCommit(a_Request : str): - return SQLRequestToBD1Commit(a_Request, None) - -def SQLRequestToBD1Commit(a_Request : str, a_Param1): - return SQLRequestToBD2Commit(a_Request, a_Param1, None) - -def SQLRequestToBD2Commit(a_Request : str, a_Param1, a_Param2): - return SQLRequestToBD3Common(a_Request, True, a_Param1, a_Param2, None) - -def SQLRequestToBD3Commit(a_Request : str, a_Param1, a_Param2, a_Param3): - return SQLRequestToBD3Common(a_Request, True, a_Param1, a_Param2, a_Param3) - -def SQLRequestToBD(a_Request : str): - return SQLRequestToBD1(a_Request, None) - -def SQLRequestToBD1(a_Request : str, a_Param1): - return SQLRequestToBD2(a_Request, a_Param1, None) - -def SQLRequestToBD2(a_Request : str, a_Param1, a_Param2): - return SQLRequestToBD3Common(a_Request, False, a_Param1, a_Param2, None) - -def SQLRequestToBD3(a_Request : str, a_Param1, a_Param2, a_Param3): - return SQLRequestToBD3Common(a_Request, False, a_Param1, a_Param2, a_Param3) - -def SQLRequestToBD3Common(a_Request : str, a_Commit : bool, a_Param1, a_Param2, a_Param3): +def SQLRequestToBD(a_Request : str, commit = False, return_error = False, param = None): db = sqlite3.connect(GetBDFileName()) cursor = db.cursor() result = [] + error = None try: - if not a_Param3 is None: - cursor.execute(a_Request, (a_Param1, a_Param3, a_Param3)) - elif not a_Param2 is None: - cursor.execute(a_Request, (a_Param1, a_Param2)) - elif not a_Param1 is None: - cursor.execute(a_Request, (a_Param1)) + if not param is None: + cursor.execute(a_Request, (param)) else: cursor.execute(a_Request) result = cursor.fetchall() - if a_Commit: + if commit: db.commit() except sqlite3.Error as e: log.Error(f'Ошибка при обработке запроса [{a_Request}]:{str(e)}') - result = "Ошибка sqlite3:" + str(e) + error = "Ошибка sqlite3:" + str(e) cursor.close() db.close() + if return_error: + return result, error return result