Browse Source

Добавление проектов

test_bot
Alexei 2 years ago
parent
commit
a9a5ba1681
  1. 75
      bot_modules/projects.py
  2. 27
      template/bd_item.py
  3. 112
      template/bd_item_add.py
  4. 37
      template/bd_item_edit.py

75
bot_modules/projects.py

@ -5,7 +5,7 @@
from bot_sys import bot_bd, log, config, keyboard, user_access from bot_sys import bot_bd, log, config, keyboard, user_access
from bot_modules import start, access, groups from bot_modules import start, access, groups
from template import bd_item_view, simple_message, bd_item_delete, bd_item_edit from template import bd_item_view, simple_message, bd_item_delete, bd_item_edit, bd_item, bd_item_add
from aiogram import Bot, types from aiogram import Bot, types
@ -17,9 +17,9 @@ import sqlite3
bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML) bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML)
class FSMCreateProject(StatesGroup): class FSMCreateProject(StatesGroup):
prjPhoto = State() name = State()
prjName = State() desc = State()
prjDesc = State() photo = State()
class FSMEditPhotoItem(StatesGroup): class FSMEditPhotoItem(StatesGroup):
item_id = State() item_id = State()
@ -71,26 +71,26 @@ error_find_proj_message = '''
''' '''
project_open_message = ''' project_open_message = '''
<b>Проект: @proj_name</b> <b>Проект: #field_name</b>
@proj_desk #field_desc
''' '''
project_create_message_1 = ''' project_create_message_3 = '''
Создание проекта. Шаг 1 Создание проекта. Шаг 3
Загрузите обложку для проекта (Фото): Загрузите обложку для проекта (Фото):
Она будет отображаться в его описании. Она будет отображаться в его описании.
''' '''
project_create_message_2 = ''' project_create_message_1 = '''
Создание проекта. Шаг 2 Создание проекта. Шаг 1
Введите название проекта: Введите название проекта:
''' '''
project_create_message_3 = ''' project_create_message_2 = '''
Создание проекта. Шаг 3 Создание проекта. Шаг 2
Введите описание проекта: Введите описание проекта:
''' '''
@ -114,14 +114,14 @@ project_edit_photo_message = '''
project_edit_name_message = ''' project_edit_name_message = '''
Текущее название проекта: Текущее название проекта:
@proj_name #field_name
Введите новое название проекта: Введите новое название проекта:
''' '''
project_edit_desc_message = ''' project_edit_desc_message = '''
Текущее описание проекта: Текущее описание проекта:
@proj_desc #field_desc
Введите новое описание проекта: Введите новое описание проекта:
''' '''
@ -184,11 +184,16 @@ def ShowMessageTemplate(a_StringMessage):
name = a_Item[1] name = a_Item[1]
desc = a_Item[2] desc = a_Item[2]
access = a_Item[3] access = a_Item[3]
msg = a_StringMessage.replace('@proj_name', name).replace('@proj_desk', desc) msg = a_StringMessage.replace('#field_name', name).replace('#field_desc', desc)
print(msg) print(msg)
return simple_message.WorkFuncResult(msg, photo_id = photo_id, item_access = access) return simple_message.WorkFuncResult(msg, photo_id = photo_id, item_access = access)
return ShowProject return ShowProject
def SimpleMessageTemplate(a_StringMessage):
async def ShowProject(a_CallbackQuery : types.CallbackQuery):
return simple_message.WorkFuncResult(a_StringMessage)
return ShowProject
select_handler = 0 select_handler = 0
# стартовое сообщение # стартовое сообщение
async def ProjectsOpen(a_Message : types.message, state = None): async def ProjectsOpen(a_Message : types.message, state = None):
@ -291,7 +296,7 @@ async def PhotoEditLoad(a_Message : types.message, state : FSMContext, a_FileID)
if msg != '': if msg != '':
await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups))
return return
await a_Message.answer(project_edit_message_2.replace('@proj_name', name), reply_markup = GetSkipAndCancelKeyboardButtons(user_groups)) 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): async def ProjectEditPhotoLoad(a_Message : types.message, state : FSMContext):
await PhotoEditLoad(a_Message, state, a_Message.photo[0].file_id) await PhotoEditLoad(a_Message, state, a_Message.photo[0].file_id)
@ -357,14 +362,15 @@ def GetProject(a_ProjectID):
db.close() db.close()
return project return project
def AddProject(a_prjPhoto, a_prjName, a_prjDesc): def AddBDItemFunc(a_ItemData):
db = sqlite3.connect(bot_bd.GetBDFileName()) 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)))
cursor = db.cursor()
cursor.execute('INSERT INTO projects(projectPhoto, projectName, projectDesc, projectAccess) VALUES(?, ?, ?, ?)', (a_prjPhoto, a_prjName, a_prjDesc, access.GetItemDefaultAccessForModule(module_name))) if error:
db.commit() log.Error(f'Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
cursor.close() else:
db.close() log.Success(f'Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
return
return res, error
def EditProject(a_ProjectID, a_prjPhoto, a_prjName, a_prjDesc): def EditProject(a_ProjectID, a_prjPhoto, a_prjName, a_prjDesc):
db = sqlite3.connect(bot_bd.GetBDFileName()) db = sqlite3.connect(bot_bd.GetBDFileName())
@ -406,8 +412,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_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(ProjectCreate, text = add_project_button_name)
dp.register_message_handler(ProjectPhotoSkip, text = projects_skip_button_name, state = FSMCreateProject.prjPhoto) 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.prjPhoto)
@ -418,16 +426,7 @@ def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(ProjectDescLoad, state = FSMCreateProject.prjDesc)''' dp.register_message_handler(ProjectDescLoad, state = FSMCreateProject.prjDesc)'''
# Редактирование проекта # Редактирование проекта
dp.register_message_handler(simple_message.InfoMessageTemplate(project_start_edit_message, GetEditProjectKeyboardButtons, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name) 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_edit.FieldType.photo) 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)
bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditNameItem, edit_project_name_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_name_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, name_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item_edit.FieldType.text) bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditNameItem, edit_project_name_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_name_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, name_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text)
bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDeskItem, edit_project_desc_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_desc_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, desc_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item_edit.FieldType.text) bd_item_edit.EditBDItemRegisterHandlers(dp, FSMEditDeskItem, edit_project_desc_button_name, project_select_to_edit_message, ShowMessageTemplate(project_edit_desc_message), ShowMessageTemplate(project_success_edit_message), table_name, key_name, desc_field, GetButtonNameAndKeyValueAndAccess, GetAccess, GetEditProjectKeyboardButtons, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text)
'''
dp.register_message_handler(ProjectSelectForEdit, text = edit_project_button_name)
dp.register_callback_query_handler(ProjectEdit, lambda x: x.data.startswith(select_to_edit_project_callback_prefix))
dp.register_message_handler(ProjectEditPhotoSkip, text = projects_skip_button_name, state = FSMEditProject.prjPhoto)
dp.register_message_handler(ProjectEditCancel, text = projects_canсel_button_name, state = FSMEditProject.prjPhoto)
dp.register_message_handler(ProjectEditCancel, text = projects_canсel_button_name, state = FSMEditProject.prjName)
dp.register_message_handler(ProjectEditCancel, text = projects_canсel_button_name, state = FSMEditProject.prjDesc)
dp.register_message_handler(ProjectEditPhotoLoad, content_types = ['photo'], state = FSMEditProject.prjPhoto)
dp.register_message_handler(ProjectEditNameLoad, state = FSMEditProject.prjName)
dp.register_message_handler(ProjectEditDescLoad, state = FSMEditProject.prjDesc)'''

27
template/bd_item.py

@ -2,9 +2,11 @@
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> # Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Работа с элементом в БД # Работа с элементом в БД
from enum import Enum
from bot_sys import user_access, bot_bd from bot_sys import user_access, bot_bd, keyboard, log
from bot_modules import groups from bot_modules import groups
from template import simple_message
from aiogram import types from aiogram import types
@ -17,6 +19,29 @@ class TableListParam():
''' '''
item_not_found = 'Элемент {item_id} не найден в таблице {a_TableName}' item_not_found = 'Элемент {item_id} не найден в таблице {a_TableName}'
skip_button_name = "⏩ Пропустить"
canсel_button_name = "🚫 Отменить"
class FieldType(Enum):
text = 'text'
photo = 'photo'
def GetCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode):
def GetCancelKeyboardButtons(a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()),
]
return keyboard.MakeKeyboard(cur_buttons, a_UserGroups)
return GetCancelKeyboardButtons
def GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode):
def GetSkipAndCancelKeyboardButtons(a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(skip_button_name, a_AccessMode, a_AccessFunc()),
keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc()),
]
return keyboard.MakeKeyboard(cur_buttons, a_UserGroups)
return GetSkipAndCancelKeyboardButtons
def GetAllItemsTemplate(a_TableName): def GetAllItemsTemplate(a_TableName):
def GetAllItems(): def GetAllItems():

112
template/bd_item_add.py

@ -0,0 +1,112 @@
# -*- coding: utf8 -*-
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Добавление элемента в БД
from bot_sys import user_access, bot_bd, log
from bot_modules import access, groups
from template import simple_message, bd_item_select, bd_item
from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
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):
async def StartAddBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext):
user_id = str(a_CallbackQuery.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
await a_FSMStart.set()
key_item_id = None
if hasattr(a_CallbackQuery, 'data'):
key_item_id = str(a_CallbackQuery.data).replace(a_Prefix, '')
res_of_work_func = None
check = None
async with state.proxy() as item_data:
item_data['key_item_id'] = 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_CallbackQuery)
else:
res_of_work_func = await a_MessageFunc(a_CallbackQuery)
if not check is None:
await state.finish()
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 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 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):
async def FinishAddBDItem(a_Message : types.CallbackQuery, state : FSMContext):
state_func = None
if a_Finish:
state_func = state.finish
else:
state_func = a_FSM.next
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
error = None
res_of_work_func = None
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)
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:
await state_func()
return check
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)
field_value = a_Message.photo[0].file_id
else:
field_value = a_Message.text
item_data[a_FieldName] = field_value
if a_Finish:
res, error = a_AddBDItemFunc(item_data)
await state_func()
if error:
return simple_message.WorkFuncResult(error)
return res_of_work_func
return simple_message.SimpleMessageTemplate(FinishAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode)
def AddBDItem3RegisterHandlers(dp, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_ButtonName, a_AddBDItemFunc, a_AddNameMessageFunc, a_AddDescMessageFunc, a_AddPhotoMessageFunc, a_FinishMessageFunc, a_TableName : str, a_KeyName, a_NameField, a_DescField, a_PhotoField, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD):
keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode)
keyboard_skip_and_cancel = bd_item.GetSkipAndCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode)
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)

37
template/bd_item_edit.py

@ -3,9 +3,7 @@
# Редактирование элемента в БД # Редактирование элемента в БД
from enum import Enum from bot_sys import user_access, bot_bd, log
from bot_sys import user_access, bot_bd, keyboard, log
from bot_modules import access, groups from bot_modules import access, groups
from template import simple_message, bd_item_select, bd_item from template import simple_message, bd_item_select, bd_item
@ -13,7 +11,6 @@ from aiogram import types
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup from aiogram.dispatcher.filters.state import State, StatesGroup
canсel_button_name = "🚫 Отменить"
cancel_message = ''' cancel_message = '''
🚫 Редактирование отменено 🚫 Редактирование отменено
@ -23,18 +20,6 @@ error_photo_type_message = '''
🚫 Фотографий не найдено 🚫 Фотографий не найдено
''' '''
def GetCancelKeyboardButtonsTemplate(a_AccessFunc, a_AccessMode):
def GetCancelKeyboardButtons(a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(canсel_button_name, a_AccessMode, a_AccessFunc())
]
return keyboard.MakeKeyboard(cur_buttons, a_UserGroups)
return GetCancelKeyboardButtons
class FieldType(Enum):
text = 'text'
photo = 'photo'
def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT): def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT):
async def StartEditBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext): async def StartEditBDItem(a_CallbackQuery : types.CallbackQuery, state : FSMContext):
user_id = str(a_CallbackQuery.from_user.id) user_id = str(a_CallbackQuery.from_user.id)
@ -54,15 +39,15 @@ def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_Fiel
return res_of_work_func return res_of_work_func
return simple_message.SimpleMessageTemplate(StartEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode) return simple_message.SimpleMessageTemplate(StartEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode)
def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = FieldType.text): def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
async def FinishEditBDItem(a_Message : types.CallbackQuery, state : FSMContext): async def FinishEditBDItem(a_Message : types.CallbackQuery, state : FSMContext):
user_id = str(a_Message.from_user.id) user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id) user_groups = groups.GetUserGroupData(user_id)
error = None error = None
res_of_work_func = None res_of_work_func = None
async with state.proxy() as item_data: async with state.proxy() as item_data:
if a_Message.text == canсel_button_name: if a_Message.text == bd_item.canсel_button_name:
print('canсel_button_name', canсel_button_name) print('canсel_button_name', bd_item.canсel_button_name)
await state.finish() await state.finish()
return simple_message.WorkFuncResult(cancel_message) return simple_message.WorkFuncResult(cancel_message)
@ -74,7 +59,7 @@ def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_Messa
return check return check
field_value = '' field_value = ''
if field_type == FieldType.photo: if field_type == bd_item.FieldType.photo:
if a_Message.photo == None or len(a_Message.photo) == 0: if a_Message.photo == None or len(a_Message.photo) == 0:
await state.finish() await state.finish()
return simple_message.WorkFuncResult(error_photo_type_message) return simple_message.WorkFuncResult(error_photo_type_message)
@ -90,14 +75,14 @@ def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_Messa
return simple_message.SimpleMessageTemplate(FinishEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode) return simple_message.SimpleMessageTemplate(FinishEditBDItem, a_ButtonFunc, a_AccessFunc, access_mode)
def EditBDItemRegisterHandlers(dp, a_FSM, a_ButtonName, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName : str, a_KeyName, a_FieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = FieldType.text): def EditBDItemRegisterHandlers(dp, a_FSM, a_ButtonName, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName : str, a_KeyName, a_FieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
keyboard_cancel = GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode) keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode)
a_Prefix = f'edit_{a_TableName}_{a_KeyName}_{a_FieldName}:' a_Prefix = f'edit_{a_TableName}_{a_KeyName}_{a_FieldName}:'
sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_Prefix, access_mode) 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(sel_handler, text = a_ButtonName)
dp.register_callback_query_handler(StartEditBDItemTemplate(a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, keyboard_cancel, access_mode), lambda x: x.data.startswith(a_Prefix)) dp.register_callback_query_handler(StartEditBDItemTemplate(a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, keyboard_cancel, access_mode), lambda x: x.data.startswith(a_Prefix))
if field_type == FieldType.photo: if field_type == bd_item.FieldType.photo:
dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['photo'], state = a_FSM.item_field) dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['photo'], state = a_FSM.item_field)
dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['text'], state = a_FSM.item_field) dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['text'], state = a_FSM.item_field)
else: else:
dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), state = a_FSM.item_field) dp.register_message_handler(FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), state = a_FSM.item_field)

Loading…
Cancel
Save