Browse Source

Удаление задач подключено и работает. Установлена максимальная ширина клавиатуры в 4 кнопки.

test_bot
Alexei 2 years ago
parent
commit
b2fc78a3b5
  1. 2
      bot_modules/projects.py
  2. 6
      bot_modules/tasks.py
  3. 7
      bot_sys/keyboard.py
  4. 7
      template/bd_item.py
  5. 12
      template/bd_item_delete.py
  6. 2
      template/bd_item_edit.py
  7. 14
      template/bd_item_select.py
  8. 1
      template/simple_message.py

2
bot_modules/projects.py

@ -266,7 +266,7 @@ def RegisterHandlers(dp : Dispatcher):
select_handler = bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowMessageTemplate(project_open_message), GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func) select_handler = bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, list_project_button_name, table_name, key_name, ShowMessageTemplate(project_open_message), GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func)
# Удаление проекта # Удаление проекта
bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_project_button_name, table_name, key_name, ProjectPreDelete, ProjectPostDelete, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func) bd_item_delete.DeleteBDItemRegisterHandlers(dp, None, bd_item.GetCheckForTextFunc(del_project_button_name), table_name, key_name, None, ProjectPreDelete, ProjectPostDelete, GetButtonNameAndKeyValueAndAccess, select_project_message, GetAccess, defaul_keyboard_func)
# Добавление проекта # Добавление проекта
bd_item_add.AddBDItem3RegisterHandlers(dp, \ bd_item_add.AddBDItem3RegisterHandlers(dp, \

6
bot_modules/tasks.py

@ -269,11 +269,9 @@ def RegisterHandlers(dp : Dispatcher):
a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'view_task', list_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'view_task', list_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW)
bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, a_Prefix, parent_id_field, table_name, key_name, ShowMessageTemplate(task_open_message), GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func, access_mode = user_access.AccessMode.VIEW) bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, a_Prefix, parent_id_field, table_name, key_name, ShowMessageTemplate(task_open_message), GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func, access_mode = user_access.AccessMode.VIEW)
'''
# Удаление задачи # Удаление задачи
# a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'del_task', add_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'del_task', del_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.DELETE)
# bd_item_delete.DeleteBDItemRegisterHandlers(dp, del_task_button_name, table_name, key_name, TaskPreDelete, TaskPostDelete, GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func) bd_item_delete.DeleteBDItemRegisterHandlers(dp, a_Prefix, bd_item.GetCheckForPrefixFunc(a_Prefix), table_name, key_name, parent_id_field,TaskPreDelete, TaskPostDelete, GetButtonNameAndKeyValueAndAccess, select_task_message, GetAccess, defaul_keyboard_func)
'''
# Добавление задачи # Добавление задачи
a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'add_task', add_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW) a_Prefix, sel_handler = bd_item_select.FirstSelectBDItemRegisterHandlers(dp, 'add_task', add_task_button_name, projects.table_name, projects.key_name, projects.GetButtonNameAndKeyValueAndAccess, projects.select_project_message, projects.GetAccess, access_mode = user_access.AccessMode.VIEW)

7
bot_sys/keyboard.py

@ -27,12 +27,15 @@ def Chunks(a_List, a_ChunkSize):
for i in range(0, len(a_List), a_ChunkSize): for i in range(0, len(a_List), a_ChunkSize):
yield a_List[i: i + a_ChunkSize] yield a_List[i: i + a_ChunkSize]
def GetKeyInRowCount(a_AllKeyCount):
return min(max(int(math.sqrt(a_AllKeyCount) // 1), 1), 4)
def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups):
buttons = [] buttons = []
for b in a_ButtonList: for b in a_ButtonList:
if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode):
buttons += [types.KeyboardButton(b.label)] buttons += [types.KeyboardButton(b.label)]
step = max(int(math.sqrt(len(buttons)) // 1), 1) step = GetKeyInRowCount(len(buttons))
key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True) key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True)
return key return key
@ -52,7 +55,7 @@ def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str):
buttons = [] buttons = []
for b in a_ButtonList: for b in a_ButtonList:
buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')] buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')]
step = max(int(math.sqrt(len(buttons)) // 1), 1) step = GetKeyInRowCount(len(buttons))
inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step)) inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step))
return inline_keyboard return inline_keyboard

7
template/bd_item.py

@ -22,6 +22,13 @@ item_not_found = 'Элемент {item_id} не найден в таблице {
skip_button_name = "⏩ Пропустить" skip_button_name = "⏩ Пропустить"
canсel_button_name = "🚫 Отменить" canсel_button_name = "🚫 Отменить"
def HashPrefix(a_Str):
# callback data в сообщении имеет ограниченную длину, поэтому сокращаем префикс
summ = 0
for i in a_Str:
summ += ord(i)
return f'{summ}:'
class FieldType(Enum): class FieldType(Enum):
text = 'text' text = 'text'
photo = 'photo' photo = 'photo'

12
template/bd_item_delete.py

@ -31,9 +31,13 @@ def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDele
return simple_message.SimpleMessageTemplate(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode) return simple_message.SimpleMessageTemplate(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode)
def DeleteBDItemRegisterHandlers(dp, a_ButtonName, a_TableName : str, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE): def DeleteBDItemRegisterHandlers(dp, a_PrevPrefix, a_StartCheckFunc, a_TableName : str, a_KeyName, a_ParentIDFieldName,a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE):
a_Prefix = f'delete_{a_TableName}_{a_KeyName}:' reg_func = dp.register_message_handler
sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode) if a_ParentIDFieldName:
dp.register_message_handler(sel_handler, text = a_ButtonName) reg_func = dp.register_callback_query_handler
a_Prefix = bd_item.HashPrefix(f'delete_{a_TableName}_{a_KeyName}:')
sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode)
reg_func(sel_handler, a_StartCheckFunc)
dp.register_callback_query_handler(DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) dp.register_callback_query_handler(DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_Prefix, a_AccessFunc, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix))

2
template/bd_item_edit.py

@ -37,7 +37,7 @@ def FinishEditBDItemTemplate(a_FSM, a_TableName, a_KeyName, a_FieldName, a_Messa
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): 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 = bd_item.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 = bd_item.HashPrefix(f'edit_{a_TableName}_{a_KeyName}_{a_FieldName}:')
sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode) sel_handler = bd_item_select.SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, 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_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix)) dp.register_callback_query_handler(StartEditBDItemTemplate(a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard_cancel, a_ButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix))

14
template/bd_item_select.py

@ -18,18 +18,14 @@ def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_ParentIDFieldName
else: else:
items = bd_item.GetAllItemsTemplate(a_TableName)() items = bd_item.GetAllItemsTemplate(a_TableName)()
print('GetBDItemsListKeyboardButtons', parent_id, a_PrevPrefix, a_ParentIDFieldName, "\n", items)
items_button_list = [] items_button_list = []
for t in items: for t in items:
print('GetBDItemsListKeyboardButtons items', t)
bname, key_value, access = a_GetButtonNameAndKeyValueAndAccessFunc(t) bname, key_value, access = a_GetButtonNameAndKeyValueAndAccessFunc(t)
if access is None: if access is None:
access = '' access = ''
if not bname is None and user_access.CheckAccessString(access, a_UserGroups, access_mode): if not bname is None and user_access.CheckAccessString(access, a_UserGroups, access_mode):
b = keyboard.Button(bname, key_value) b = keyboard.Button(bname, key_value)
print(bname, key_value, b)
items_button_list += [b] items_button_list += [b]
print(items_button_list)
return keyboard.MakeInlineKeyboard(items_button_list, a_NextPrefix) return keyboard.MakeInlineKeyboard(items_button_list, a_NextPrefix)
return GetBDItemsListKeyboardButtons return GetBDItemsListKeyboardButtons
@ -37,15 +33,9 @@ def SelectDBItemTemplate(a_TableName : str, a_ParentIDFieldName, a_GetButtonName
keyboard_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) keyboard_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc)
return simple_message.InfoMessageTemplate(a_StartMessage, keyboard_func, a_AccessFunc, access_mode) return simple_message.InfoMessageTemplate(a_StartMessage, keyboard_func, a_AccessFunc, access_mode)
def hash_str(a_Str):
summ = 0
for i in a_Str:
summ += ord(i)
return f'{summ}:'
def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):
a_Prefix = hash_str(f'first_select_{a_TableName}_{a_KeyName}_in_base_{a_PrefixBase}:') a_Prefix = bd_item.HashPrefix(f'first_select_{a_TableName}_{a_KeyName}_in_base_{a_PrefixBase}:')
sel_handler = SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode = access_mode) sel_handler = SelectDBItemTemplate(a_TableName, None, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, None, a_Prefix, access_mode = access_mode)
dp.register_message_handler(sel_handler, text = a_ButtonName) dp.register_message_handler(sel_handler, text = a_ButtonName)
@ -53,7 +43,7 @@ def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableNam
return a_Prefix, sel_handler return a_Prefix, sel_handler
def NextSelectBDItemRegisterHandlers(dp, a_PrevPrefix, a_ParentIDFieldName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): def NextSelectBDItemRegisterHandlers(dp, a_PrevPrefix, a_ParentIDFieldName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):
a_Prefix = f'next_select_{a_TableName}_{a_KeyName}_{a_ParentIDFieldName}_after_prefix_{a_PrevPrefix}:' a_Prefix = bd_item.HashPrefix(f'next_select_{a_TableName}_{a_KeyName}_{a_ParentIDFieldName}_after_prefix_{a_PrevPrefix}:')
sel_handler = SelectDBItemTemplate(a_TableName, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode = access_mode) sel_handler = SelectDBItemTemplate(a_TableName, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_Prefix, access_mode = access_mode)
dp.register_callback_query_handler(sel_handler, bd_item.GetCheckForPrefixFunc(a_PrevPrefix)) dp.register_callback_query_handler(sel_handler, bd_item.GetCheckForPrefixFunc(a_PrevPrefix))

1
template/simple_message.py

@ -49,7 +49,6 @@ def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mod
return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyboard_func(a_Message, user_groups)) return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyboard_func(a_Message, user_groups))
if photo_id is None or photo_id == 0 or photo_id == '0': if photo_id is None or photo_id == 0 or photo_id == '0':
print(keyboard_func)
return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyboard_func(a_Message, user_groups)) return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyboard_func(a_Message, user_groups))
await bot.send_photo(user_id, photo_id, msg, reply_markup = keyboard_func(a_Message, user_groups)) await bot.send_photo(user_id, photo_id, msg, reply_markup = keyboard_func(a_Message, user_groups))

Loading…
Cancel
Save