Browse Source

Шаблоны БД

test_bot
Alexei 2 years ago
parent
commit
74db81acb7
  1. 8
      bot_modules/access.py
  2. 2
      bot_modules/backup.py
  3. 16
      bot_modules/groups.py
  4. 4
      bot_modules/profile.py
  5. 41
      bot_sys/bot_bd.py

8
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)

2
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=-');"
]
# ---------------------------------------------------------

16
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)
# ---------------------------------------------------------

4
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]

41
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

Loading…
Cancel
Save