Browse Source

Редактирование проекта окончательное

test_bot
Alexei 2 years ago
parent
commit
3c62ab2296
  1. 32
      bot_modules/projects.py
  2. 4
      bot_modules/start.py
  3. 17
      template/bd_item.py
  4. 3
      template/bd_item_delete.py
  5. 49
      template/bd_item_edit.py

32
bot_modules/projects.py

@ -21,17 +21,23 @@ class FSMCreateProject(StatesGroup):
prjName = State() prjName = State()
prjDesc = State() prjDesc = State()
class FSMEditProject(StatesGroup): class FSMEditPhotoItem(StatesGroup):
prjID = State() item_id = State()
prjPhoto = State() item_field = State()
prjName = State()
prjDesc = State() class FSMEditNameItem(StatesGroup):
item_id = State()
item_field = State()
class FSMEditDeskItem(StatesGroup):
item_id = State()
item_field = State()
# --------------------------------------------------------- # ---------------------------------------------------------
# БД # БД
module_name = 'projects' module_name = 'projects'
table_name = 'projects' table_name = module_name
key_name = 'projectID' key_name = 'projectID'
photo_field = 'projectPhoto' photo_field = 'projectPhoto'
name_field = 'projectName' name_field = 'projectName'
@ -136,15 +142,15 @@ add_project_button_name = "✅ Добавить проект"
del_project_button_name = "❌ Удалить проект" del_project_button_name = "❌ Удалить проект"
edit_project_button_name = "🛠 Редактировать проект" edit_project_button_name = "🛠 Редактировать проект"
edit_project_photo_button_name = "🛠 Изменить изображение" edit_project_photo_button_name = " Изменить изображение"
edit_project_name_button_name = "🛠 Изменить название" edit_project_name_button_name = " Изменить название"
edit_project_desc_button_name = "🛠 Изменить описание" edit_project_desc_button_name = "𝌴 Изменить описание"
# --------------------------------------------------------- # ---------------------------------------------------------
# Работа с кнопками # Работа с кнопками
def GetEditProjectKeyboardButtons(a_UserGroups): def GetEditProjectKeyboardButtons(a_UserGroups):
cur_buttons = [ cur_buttons = GetModuleButtons() + [
keyboard.ButtonWithAccess(edit_project_photo_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(edit_project_photo_button_name, user_access.AccessMode.EDIT, GetAccess()),
keyboard.ButtonWithAccess(edit_project_name_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(edit_project_name_button_name, user_access.AccessMode.EDIT, GetAccess()),
keyboard.ButtonWithAccess(edit_project_desc_button_name, user_access.AccessMode.EDIT, GetAccess()), keyboard.ButtonWithAccess(edit_project_desc_button_name, user_access.AccessMode.EDIT, GetAccess()),
@ -412,9 +418,9 @@ 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, 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_edit.FieldType.photo)
bd_item_edit.EditBDItemRegisterHandlers(dp, 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_edit.FieldType.text)
bd_item_edit.EditBDItemRegisterHandlers(dp, 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_edit.FieldType.text)
''' '''
dp.register_message_handler(ProjectSelectForEdit, text = edit_project_button_name) 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_callback_query_handler(ProjectEdit, lambda x: x.data.startswith(select_to_edit_project_callback_prefix))

4
bot_modules/start.py

@ -23,9 +23,9 @@ f"INSERT OR IGNORE INTO module_access (modName, modAccess, itemDefaultAccess) VA
start_message = ''' start_message = '''
<b>Добро пожаловать!</b> <b>Добро пожаловать!</b>
Выберите возмжные действия на кнопах ниже ''' Выберите возможные действия на кнопах ниже '''
start_menu_button_name = " Главное меню" start_menu_button_name = " Главное меню"
# --------------------------------------------------------- # ---------------------------------------------------------
# Работа с кнопками # Работа с кнопками

17
template/bd_item.py

@ -39,7 +39,7 @@ def EditBDItemInTableTemplate(a_TableName : str, a_KeyName : str, a_FieldName :
return EditBDItemInTable return EditBDItemInTable
def CheckAccessBDItemTemplate(a_TableName, a_KeyName, a_KeyValue, a_WorkFunc, a_AccessMode : user_access.AccessMode): def CheckAccessBDItemTemplate(a_TableName, a_KeyName, a_KeyValue, a_WorkFunc, a_AccessMode : user_access.AccessMode):
async def CheckAccessBDItem(a_CallbackQuery : types.CallbackQuery, a_ResultWorkFunc): async def CheckAccessBDItem(a_CallbackQuery : types.CallbackQuery):
user_id = str(a_CallbackQuery.from_user.id) user_id = str(a_CallbackQuery.from_user.id)
user_groups = groups.GetUserGroupData(user_id) user_groups = groups.GetUserGroupData(user_id)
item_id = a_KeyValue item_id = a_KeyValue
@ -47,15 +47,14 @@ def CheckAccessBDItemTemplate(a_TableName, a_KeyName, a_KeyValue, a_WorkFunc, a_
if len(item) < 1: if len(item) < 1:
msg = item_not_found.replace('{item_id}', str(item_id)).replace('{a_TableName}', a_TableName) msg = item_not_found.replace('{item_id}', str(item_id)).replace('{a_TableName}', a_TableName)
log.Error(msg) log.Error(msg)
return simple_message.WorkFuncResult(msg) return simple_message.WorkFuncResult(msg), None
a_ResultWorkFunc = await a_WorkFunc(a_CallbackQuery, item[0]) result_work_func = await a_WorkFunc(a_CallbackQuery, item[0])
if a_ResultWorkFunc is None or a_ResultWorkFunc.string_message is None: if result_work_func is None or result_work_func.string_message is None:
print('a_ResultWorkFunc', a_ResultWorkFunc) return result_work_func, result_work_func
return a_ResultWorkFunc
if not a_ResultWorkFunc.item_access is None and not user_access.CheckAccessString(a_ResultWorkFunc.item_access, user_groups, a_AccessMode): if not result_work_func.item_access is None and not user_access.CheckAccessString(result_work_func.item_access, user_groups, a_AccessMode):
return simple_message.WorkFuncResult(access.access_denied_message) return simple_message.WorkFuncResult(access.access_denied_message), None
return None return None, result_work_func
return CheckAccessBDItem return CheckAccessBDItem

3
template/bd_item_delete.py

@ -14,8 +14,7 @@ def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDele
user_id = str(a_CallbackQuery.from_user.id) user_id = str(a_CallbackQuery.from_user.id)
user_groups = groups.GetUserGroupData(user_id) user_groups = groups.GetUserGroupData(user_id)
item_id = str(a_CallbackQuery.data).replace(a_Prefix, '') item_id = str(a_CallbackQuery.data).replace(a_Prefix, '')
res_of_pre_del = None check, res_of_pre_del = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_PreDeleteWorkFunc, access_mode)(a_CallbackQuery)
check = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_PreDeleteWorkFunc, access_mode)(a_CallbackQuery, res_of_pre_del)
if not check is None: if not check is None:
return check return check

49
template/bd_item_edit.py

@ -5,7 +5,7 @@
from enum import Enum from enum import Enum
from bot_sys import user_access, bot_bd, keyboard 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
@ -23,9 +23,17 @@ 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): class FieldType(Enum):
text = 0 text = 'text'
photo = 1 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):
@ -37,44 +45,43 @@ def StartEditBDItemTemplate(a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_Fiel
async with state.proxy() as item_data: async with state.proxy() as item_data:
item_data['item_id'] = item_id item_data['item_id'] = item_id
check_func = bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode) check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode)(a_CallbackQuery)
print('check_func', check_func)
print('res_of_work_func', res_of_work_func)
check = await check_func(a_CallbackQuery, res_of_work_func)
print('res_of_work_func', res_of_work_func)
print('check', check)
if not check is None: if not check is None:
await state.finish()
return check return check
await a_FSM.next() await a_FSM.next()
print('res_of_work_func', res_of_work_func)
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_Prefix, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = FieldType.text):
async def FinishEditBDItem(a_CallbackQuery : 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 == canсel_button_name:
print('canсel_button_name', canсel_button_name)
await state.finish()
return simple_message.WorkFuncResult(cancel_message) return simple_message.WorkFuncResult(cancel_message)
item_id = item_data['item_id'] item_id = item_data['item_id']
check = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode)(a_CallbackQuery, res_of_work_func) check, res_of_work_func = await bd_item.CheckAccessBDItemTemplate(a_TableName, a_KeyName, item_id, a_MessageFunc, access_mode)(a_Message)
if not check is None: if not check is None:
await state.finish()
return check return check
field_value = '' field_value = ''
if field_type == FieldType.photo: if field_type == 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()
return simple_message.WorkFuncResult(error_photo_type_message) return simple_message.WorkFuncResult(error_photo_type_message)
field_value = a_Message.photo[0].file_id field_value = a_Message.photo[0].file_id
else: else:
field_value = a_Message.text field_value = a_Message.text
res, error = bot_item.EditBDItemInTableTemplate(a_TableName, a_KeyName, a_FieldName)(item_id, field_value) res, error = bd_item.EditBDItemInTableTemplate(a_TableName, a_KeyName, a_FieldName)(item_id, field_value)
log.Success(f'Изменено поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Пользователь {user_id}.') log.Success(f'Изменено поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Пользователь {user_id}.')
await state.finish() await state.finish()
if error: if error:
@ -83,12 +90,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_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 = FieldType.text):
class FSMEditItem(StatesGroup): keyboard_cancel = GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode)
item_id = State()
item_field = State()
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(FSMEditItem, a_EditMessageFunc, a_TableName, a_KeyName, a_FieldName, a_Prefix, a_AccessFunc, a_ButtonFunc, 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))
dp.register_message_handler(FinishEditBDItemTemplate(FSMEditItem, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), state = FSMEditItem.item_field) if field_type == 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_Prefix, a_AccessFunc, a_ButtonFunc, access_mode, field_type = field_type), content_types = ['text'], state = a_FSM.item_field)
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)

Loading…
Cancel
Save