Browse Source

Чистка от лишнего кода и устранение мелких багов

test_bot
Alexei 2 years ago
parent
commit
135539b290
  1. 1
      bot_modules/profile.py
  2. 263
      bot_modules/projects.py
  3. 2
      bot_modules/start.py
  4. 1
      bot_sys/bot_bd.py
  5. 30
      template/bd_item_add.py
  6. 1
      template/bd_item_edit.py
  7. 17
      template/simple_message.py

1
bot_modules/profile.py

@ -61,7 +61,6 @@ def AddUser(a_UserID, a_UserName):
def GetUserInfo(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]
return None

263
bot_modules/projects.py

@ -39,22 +39,23 @@ module_name = 'projects'
table_name = module_name
key_name = 'projectID'
photo_field = 'projectPhoto'
name_field = 'projectName'
desc_field = 'projectDesc'
photo_field = 'projectPhoto'
access_field = 'projectAccess'
create_datetime_field = 'projectCreateDateTime'
init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}(
{photo_field} TEXT,
{key_name} INTEGER PRIMARY KEY,
{name_field} TEXT,
{desc_field} TEXT,
{photo_field} TEXT,
{access_field} TEXT,
{key_name} INTEGER PRIMARY KEY
)''',
{create_datetime_field} TEXT
)''',
f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VALUES ('{module_name}', '{user_access.user_access_group_all}=va', '{user_access.user_access_group_all}=va');"
]
# ---------------------------------------------------------
# Сообщения
@ -62,6 +63,7 @@ base_project_message = '''
<b>🟥 Проекты</b>
'''
select_project_message = '''
Пожалуйста, выберите проект:
'''
@ -70,36 +72,36 @@ error_find_proj_message = '''
Ошибка, проект не найден
'''
project_open_message = '''
<b>Проект: #field_name</b>
#field_desc
'''
project_open_message = f'''
<b>Проект: #{name_field}</b>
project_create_message_3 = '''
Создание проекта. Шаг 3
#{desc_field}
Загрузите обложку для проекта (Фото):
Она будет отображаться в его описании.
Время создания: #{create_datetime_field}
'''
project_create_message_1 = '''
project_create_name_message = '''
Создание проекта. Шаг 1
Введите название проекта:
'''
project_create_message_2 = '''
project_create_desc_message = '''
Создание проекта. Шаг 2
Введите описание проекта:
'''
project_cancel_create_message = '🚫 Создание проекта отменено'
project_create_photo_message = '''
Создание проекта. Шаг 3
project_success_create_message = '✅ Проект успешно добавлен!'
project_success_delete_message = '✅ Проект успешно удалён!'
project_success_edit_message = '✅ Проект успешно отредактирован!'
Загрузите обложку для проекта (Фото):
Она будет отображаться в его описании.
'''
project_success_create_message = '''✅ Проект успешно добавлен!'''
project_success_delete_message = '''✅ Проект успешно удалён!'''
project_success_edit_message = '''✅ Проект успешно отредактирован!'''
# Редактирование проекта.
@ -112,16 +114,16 @@ project_edit_photo_message = '''
Она будет отображаться в его описании.
'''
project_edit_name_message = '''
project_edit_name_message = f'''
Текущее название проекта:
#field_name
#{name_field}
Введите новое название проекта:
'''
project_edit_desc_message = '''
project_edit_desc_message = f'''
Текущее описание проекта:
#field_desc
#{desc_field}
Введите новое описание проекта:
'''
@ -171,21 +173,30 @@ def GetStartProjectKeyboardButtons(a_UserGroups):
# ---------------------------------------------------------
# Обработка сообщений
select_handler = 0
# стартовое сообщение
async def ProjectsOpen(a_Message : types.message, state = None):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await a_Message.answer(base_project_message, reply_markup = GetStartProjectKeyboardButtons(user_groups))
await select_handler(a_Message)
return None
def GetButtonNameAndKeyValueAndAccess(a_Item):
# projectName projectID projectAccess
return a_Item[1], a_Item[4], a_Item[3]
return a_Item[1], a_Item[0], a_Item[4]
def ShowMessageTemplate(a_StringMessage):
async def ShowProject(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 4):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
photo_id = a_Item[0]
name = a_Item[1]
desc = a_Item[2]
access = a_Item[3]
msg = a_StringMessage.replace('#field_name', name).replace('#field_desc', desc)
print(msg)
photo_id = a_Item[3]
access = a_Item[4]
create_time = a_Item[5]
msg = a_StringMessage.replace(f'#{name_field}', name).replace(f'#{desc_field}', desc).replace(f'#{create_datetime_field}', create_time)
return simple_message.WorkFuncResult(msg, photo_id = photo_id, item_access = access)
return ShowProject
@ -194,154 +205,12 @@ def SimpleMessageTemplate(a_StringMessage):
return simple_message.WorkFuncResult(a_StringMessage)
return ShowProject
select_handler = 0
# стартовое сообщение
async def ProjectsOpen(a_Message : types.message, state = None):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await a_Message.answer(base_project_message, reply_markup = GetStartProjectKeyboardButtons(user_groups))
await select_handler(a_Message)
return None
'''
# Создание нового проекта
def GetProjectData(a_ProjectID):
project = GetProject(a_ProjectID)
if len(project) < 1:
log.Error(f'Проект не найден {project_id}')
msg = error_find_proj_message.replace('@project_id', project_id)
return msg, '', '', ''
p = project[0]
return '', p[0], p[1], p[2]
async def ProjectCreateCancel(a_Message : types.message, state : FSMContext):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await state.finish()
await a_Message.answer(project_cancel_create_message, reply_markup = GetEditProjectKeyboardButtons(user_groups))
async def ProjectCreate(a_Message : types.message):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await FSMCreateProject.prjPhoto.set()
await a_Message.answer(project_create_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups))
async def PhotoLoad(a_Message : types.message, state : FSMContext, a_FileID):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
async with state.proxy() as prjData:
prjData['photo'] = a_FileID
await FSMCreateProject.next()
await a_Message.answer(project_create_message_2, reply_markup = GetCancelKeyboardButtons(user_groups))
async def ProjectPhotoLoad(a_Message : types.message, state : FSMContext):
await PhotoLoad(a_Message, state, a_Message.photo[0].file_id)
async def ProjectPhotoSkip(a_Message : types.message, state : FSMContext):
await PhotoLoad(a_Message, state, 0)
async def ProjectNameLoad(a_Message : types.message, state : FSMContext):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
async with state.proxy() as prjData:
prjData['name'] = a_Message.text
await FSMCreateProject.next()
await a_Message.answer(project_create_message_3, reply_markup = GetCancelKeyboardButtons(user_groups))
async def ProjectDescLoad(a_Message : types.message, state : FSMContext):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
async with state.proxy() as prjData:
prjData['desc'] = a_Message.text
prjPhoto = prjData['photo']
prjName = prjData['name']
prjDesc = prjData['desc']
AddProject(prjPhoto, prjName, prjDesc)
log.Success(f'Добавлен проект {prjName} пользователем {a_Message.from_user.id}.')
await state.finish()
await a_Message.answer(project_success_create_message, reply_markup = GetEditProjectKeyboardButtons(user_groups))
# Редактирование проекта
async def ProjectSelectForEdit(a_Message : types.message):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await bot.send_message(a_Message.from_user.id, project_select_to_edit_message, reply_markup = GetProjectsListKeyboardButtons(user_groups, select_to_edit_project_callback_prefix))
async def ProjectEditCancel(a_Message : types.message, state : FSMContext):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await state.finish()
await a_Message.answer(project_cancel_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_groups))
async def ProjectEdit(a_CallbackQuery : types.CallbackQuery, state : FSMContext):
user_id = str(a_CallbackQuery.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await FSMEditProject.prjID.set()
prjID = str(a_CallbackQuery.data).replace(select_to_edit_project_callback_prefix, '')
async with state.proxy() as prjData:
prjData['prjID'] = prjID
await FSMEditProject.next()
await bot.send_message(a_CallbackQuery.from_user.id, project_edit_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups))
async def PhotoEditLoad(a_Message : types.message, state : FSMContext, a_FileID):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
project_id = 0
async with state.proxy() as prjData:
prjData['photo'] = a_FileID
project_id = prjData['prjID']
await FSMEditProject.next()
msg, photo_id, name, desc = GetProjectData(project_id)
if msg != '':
await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups))
return
await a_Message.answer(project_edit_message_2.replace('#field_name', name), reply_markup = GetSkipAndCancelKeyboardButtons(user_groups))
async def ProjectEditPhotoLoad(a_Message : types.message, state : FSMContext):
await PhotoEditLoad(a_Message, state, a_Message.photo[0].file_id)
async def ProjectEditPhotoSkip(a_Message : types.message, state : FSMContext):
project_id = 0
async with state.proxy() as prjData:
project_id = prjData['prjID']
msg, photo_id, name, desc = GetProjectData(project_id)
if msg != '':
await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups))
return
await PhotoEditLoad(a_Message, state, photo_id)
async def EditNameSkip(a_Message : types.message, state : FSMContext, a_Name):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
async with state.proxy() as prjData:
prjData['name'] = a_Name
await FSMEditProject.next()
await a_Message.answer(project_edit_message_3, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups))
async def ProjectEditNameLoad(a_Message : types.message, state : FSMContext):
await EditNameSkip(a_Message, state, a_Message.text)
async def ProjectEditDescLoad(a_Message : types.message, state : FSMContext):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
async with state.proxy() as prjData:
prjData['desc'] = a_Message.text
project_id = prjData['prjID']
prjPhoto = prjData['photo']
prjName = prjData['name']
prjDesc = prjData['desc']
EditProject(project_id, prjPhoto, prjName, prjDesc)
log.Success(f'Изменён проект {prjName} пользователем {a_Message.from_user.id}.')
await state.finish()
await a_Message.answer(project_success_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_groups))
'''
# Удаление проекта
async def ProjectPreDelete(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 4):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
access = a_Item[3]
access = a_Item[4]
return simple_message.WorkFuncResult('', None, item_access = access)
async def ProjectPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID):
@ -351,19 +220,9 @@ async def ProjectPostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID):
# ---------------------------------------------------------
# Работа с базой данных проектов
def GetProjectList():
return bot_bd.SelectBDTemplate(table_name)()
def GetProject(a_ProjectID):
db = sqlite3.connect(bot_bd.GetBDFileName())
cursor = db.cursor()
project = cursor.execute('SELECT * FROM projects WHERE projectID = ?', ([a_ProjectID])).fetchall()
cursor.close()
db.close()
return project
def AddBDItemFunc(a_ItemData):
res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}) VALUES(?, ?, ?, ?)', commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name)))
res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, datetime(\'now\'))',
commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name)))
if error:
log.Error(f'Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
@ -372,23 +231,6 @@ def AddBDItemFunc(a_ItemData):
return res, error
def EditProject(a_ProjectID, a_prjPhoto, a_prjName, a_prjDesc):
db = sqlite3.connect(bot_bd.GetBDFileName())
cursor = db.cursor()
cursor.execute('UPDATE projects SET projectPhoto = ?, projectName = ?, projectDesc = ? WHERE projectID = ?', (a_prjPhoto, a_prjName, a_prjDesc, a_ProjectID))
db.commit()
cursor.close()
db.close()
return
def DelProject(a_ProjectID):
db = sqlite3.connect(bot_bd.GetBDFileName())
cursor = db.cursor()
cursor.execute('DELETE FROM projects WHERE projectID = ?', ([a_ProjectID]))
db.commit()
db.close()
return
# ---------------------------------------------------------
# API
@ -405,6 +247,7 @@ def GetModuleButtons():
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
# Список проектов
dp.register_message_handler(simple_message.SimpleMessageTemplate(ProjectsOpen, GetStartProjectKeyboardButtons, GetAccess), text = projects_button_name)
global select_handler
@ -412,18 +255,10 @@ def RegisterHandlers(dp : Dispatcher):
# Удаление проекта
bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_project_button_name, table_name, key_name, ProjectPreDelete, ProjectPostDelete, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, GetStartProjectKeyboardButtons)
# Добавление проекта
bd_item_add.AddBDItem3RegisterHandlers(dp, FSMCreateProject, FSMCreateProject.name, FSMCreateProject.desc, FSMCreateProject.photo, add_project_button_name, AddBDItemFunc, SimpleMessageTemplate(project_create_message_1), SimpleMessageTemplate(project_create_message_2), SimpleMessageTemplate(project_create_message_3), SimpleMessageTemplate(project_success_create_message), table_name, key_name, name_field, desc_field, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetStartProjectKeyboardButtons)
'''
dp.register_message_handler(ProjectCreate, text = add_project_button_name)
dp.register_message_handler(ProjectPhotoSkip, text = projects_skip_button_name, state = FSMCreateProject.prjPhoto)
dp.register_message_handler(ProjectCreateCancel, text = projects_canсel_button_name, state = FSMCreateProject.prjPhoto)
dp.register_message_handler(ProjectCreateCancel, text = projects_canсel_button_name, state = FSMCreateProject.prjName)
dp.register_message_handler(ProjectCreateCancel, text = projects_canсel_button_name, state = FSMCreateProject.prjDesc)
dp.register_message_handler(ProjectPhotoLoad, content_types = ['photo'], state = FSMCreateProject.prjPhoto)
dp.register_message_handler(ProjectNameLoad, state = FSMCreateProject.prjName)
dp.register_message_handler(ProjectDescLoad, state = FSMCreateProject.prjDesc)'''
bd_item_add.AddBDItem3RegisterHandlers(dp, FSMCreateProject, FSMCreateProject.name, FSMCreateProject.desc, FSMCreateProject.photo, add_project_button_name, AddBDItemFunc, SimpleMessageTemplate(project_create_name_message), SimpleMessageTemplate(project_create_desc_message), SimpleMessageTemplate(project_create_photo_message), SimpleMessageTemplate(project_success_create_message), table_name, key_name, name_field, desc_field, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetStartProjectKeyboardButtons)
# Редактирование проекта
dp.register_message_handler(simple_message.InfoMessageTemplate(project_start_edit_message, GetEditProjectKeyboardButtons, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name)
bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditPhotoItem, edit_project_photo_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_photo_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, photo_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.photo)

2
bot_modules/start.py

@ -42,7 +42,7 @@ async def StartMenu(a_Message, state = None):
user_id = str(a_Message.from_user.id)
user_name = str(a_Message.from_user.username)
profile.AddUser(user_id, user_name)
log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте')
log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте. Полные данные {a_Message.from_user}.')
return simple_message.WorkFuncResult(start_message)
# ---------------------------------------------------------

1
bot_sys/bot_bd.py

@ -20,7 +20,6 @@ def BDExecute(a_Commands):
db = sqlite3.connect(GetBDFileName())
cursor = db.cursor()
for cmd in a_Commands:
print(cmd)
cursor.execute(cmd)
db.commit()
cursor.close()

30
template/bd_item_add.py

@ -16,7 +16,7 @@ cancel_message = '''
🚫 Добавление отменено
'''
def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT):
def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT):
async def StartAddBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext):
user_id = str(a_CallbackQuery.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
@ -36,19 +36,20 @@ def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_TableName, a_KeyN
if not check is None:
await state.finish()
check.keyboard_func = a_FinishButtonFunc
return check
if key_item_id:
await a_FSM.next()
return res_of_work_func
return simple_message.SimpleMessageTemplate(StartAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode)
def FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, True, access_mode = access_mode, field_type = field_type)
def FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, True, access_mode = access_mode, field_type = field_type)
def NextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, False, access_mode = access_mode, field_type = field_type)
def NextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, False, access_mode = access_mode, field_type = field_type)
def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_Finish, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, a_Finish, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text):
async def FinishAddBDItem(a_Message : types.CallbackQuery, state : FSMContext):
state_func = None
if a_Finish:
@ -62,15 +63,13 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName
check = None
async with state.proxy() as item_data:
if a_Message.text == bd_item.canсel_button_name:
print('canсel_button_name', bd_item.canсel_button_name)
await state.finish()
return simple_message.WorkFuncResult(cancel_message)
return simple_message.WorkFuncResult(cancel_message, keyborad_func = a_FinishButtonFunc)
key_item_id = item_data['key_item_id']
if key_item_id:
check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, key_item_id, a_MessageFunc, access_mode)(a_Message)
else:
print(a_MessageFunc)
res_of_work_func = await a_MessageFunc(a_Message)
if not check is None:
@ -80,12 +79,11 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName
field_value = ''
if field_type == bd_item.FieldType.photo:
if a_Message.text == bd_item.skip_button_name:
print('skip_button_name', bd_item.canсel_button_name)
field_value = '0'
else:
if a_Message.photo == None or len(a_Message.photo) == 0:
await state.finish()
return simple_message.WorkFuncResult(error_photo_type_message)
return simple_message.WorkFuncResult(error_photo_type_message, keyborad_func = a_FinishButtonFunc)
field_value = a_Message.photo[0].file_id
else:
field_value = a_Message.text
@ -105,8 +103,8 @@ def AddBDItem3RegisterHandlers(dp, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_Bu
a_Prefix = f'add_{a_TableName}_{a_KeyName}_{a_NameField}_{a_DescField}_{a_PhotoField}:'
# sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode)
# dp.register_message_handler(sel_handler, text = a_ButtonName)
dp.register_message_handler(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_TableName, a_KeyName, a_NameField, a_Prefix, a_AccessFunc, keyboard_cancel, access_mode), text = a_ButtonName)
dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_TableName, a_KeyName, a_NameField, a_AddDescMessageFunc, a_AccessFunc, keyboard_cancel, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName)
dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_TableName, a_KeyName, a_DescField, a_AddPhotoMessageFunc, a_AccessFunc, keyboard_skip_and_cancel, access_mode, field_type = bd_item.FieldType.text), state = a_FSMDesc)
dp.register_message_handler(NextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo'], state = a_FSMPhoto)
dp.register_message_handler(FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['text'], state = a_FSMPhoto)
dp.register_message_handler(StartAddBDItemTemplate(a_FSM, a_FSMName, a_AddNameMessageFunc, a_TableName, a_KeyName, a_NameField, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), text = a_ButtonName)
dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_TableName, a_KeyName, a_NameField, a_AddDescMessageFunc, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMName)
dp.register_message_handler(NextAddBDItemTemplate(a_FSM, None, a_TableName, a_KeyName, a_DescField, a_AddPhotoMessageFunc, a_AccessFunc, keyboard_skip_and_cancel, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.text), state = a_FSMDesc)
dp.register_message_handler(FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['photo'], state = a_FSMPhoto)
dp.register_message_handler(FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_TableName, a_KeyName, a_PhotoField, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, access_mode, field_type = bd_item.FieldType.photo), content_types = ['text'], state = a_FSMPhoto)

1
template/bd_item_edit.py

@ -47,7 +47,6 @@ def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_Messa
res_of_work_func = None
async with state.proxy() as item_data:
if a_Message.text == bd_item.canсel_button_name:
print('canсel_button_name', bd_item.canсel_button_name)
await state.finish()
return simple_message.WorkFuncResult(cancel_message)

17
template/simple_message.py

@ -11,10 +11,12 @@ from aiogram import Bot
bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML)
class WorkFuncResult():
def __init__(self, a_StringMessage : str, photo_id = None, item_access = None):
def __init__(self, a_StringMessage : str, photo_id = None, item_access = None, keyborad_func = None):
self.string_message = a_StringMessage
self.photo_id = photo_id
self.item_access = item_access
self.keyborad_func = keyborad_func
def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):
async def GetMessage(a_Message : types.message, state = None):
@ -29,11 +31,14 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod
if not user_access.CheckAccessString(a_AccessFunc(), user_groups, access_mode):
return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups))
print(a_WorkFunc)
res = await a_WorkFunc(a_Message, state = state)
if res is None:
return
keyborad_func = a_GetButtonsFunc
if res.keyborad_func:
keyborad_func = res.keyborad_func
msg = res.string_message
if msg is None:
return
@ -41,10 +46,10 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod
photo_id = res.photo_id
if not res.item_access is None and not user_access.CheckAccessString(res.item_access, user_groups, access_mode):
return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups))
return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyborad_func(user_groups))
if photo_id is None or photo_id == 0 or photo_id == '0':
return await bot.send_message(a_Message.from_user.id, msg, reply_markup = a_GetButtonsFunc(user_groups))
return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyborad_func(user_groups))
await bot.send_photo(user_id, photo_id, msg, reply_markup = a_GetButtonsFunc(user_groups))
await bot.send_photo(user_id, photo_id, msg, reply_markup = keyborad_func(user_groups))
return SimpleMessage

Loading…
Cancel
Save