Refactor admin handlers to improve access control and state management. Added checks for admin rights in ban functions and streamlined router inclusion order in main bot file. Updated keyboard layouts for better user experience and removed unused state definitions.
This commit is contained in:
@@ -5,10 +5,12 @@ from aiogram.utils.keyboard import ReplyKeyboardBuilder, InlineKeyboardBuilder
|
||||
def get_reply_keyboard_for_post():
|
||||
builder = InlineKeyboardBuilder()
|
||||
builder.row(types.InlineKeyboardButton(
|
||||
text="Опубликовать", callback_data="publish")
|
||||
text="Опубликовать", callback_data="publish"),
|
||||
types.InlineKeyboardButton(
|
||||
text="Отклонить", callback_data="decline")
|
||||
)
|
||||
builder.row(types.InlineKeyboardButton(
|
||||
text="Отклонить", callback_data="decline")
|
||||
text="👮♂️ Забанить", callback_data="ban")
|
||||
)
|
||||
markup = builder.as_markup(resize_keyboard=True, one_time_keyboard=True)
|
||||
return markup
|
||||
@@ -37,7 +39,7 @@ def get_reply_keyboard_admin():
|
||||
builder.add(types.KeyboardButton(text="Бан (Список)"))
|
||||
builder.add(types.KeyboardButton(text="Бан по нику"))
|
||||
builder.add(types.KeyboardButton(text="Бан по ID"))
|
||||
builder.add(types.KeyboardButton(text="Тестовый бан"))
|
||||
builder.row()
|
||||
builder.add(types.KeyboardButton(text="Разбан (список)"))
|
||||
builder.add(types.KeyboardButton(text="Вернуться в бота"))
|
||||
markup = builder.as_markup(resize_keyboard=True, one_time_keyboard=True)
|
||||
@@ -49,7 +51,7 @@ def create_keyboard_with_pagination(page: int, total_items: int, array_items: li
|
||||
Создает клавиатуру с пагинацией для заданного набора элементов и устанавливает необходимый callback
|
||||
|
||||
Args:
|
||||
page: Номер текущей страницы.
|
||||
page: Номер текущей страницы (начинается с 1).
|
||||
total_items: Общее количество элементов.
|
||||
array_items: Лист кортежей. Содержит в себе user_name: user_id
|
||||
callback: Действие в коллбеке. Вернет callback вида ({callback}_{user_id})
|
||||
@@ -57,34 +59,75 @@ def create_keyboard_with_pagination(page: int, total_items: int, array_items: li
|
||||
Returns:
|
||||
InlineKeyboardMarkup: Клавиатура с кнопками пагинации.
|
||||
"""
|
||||
|
||||
# Проверяем валидность входных данных
|
||||
if page < 1:
|
||||
page = 1
|
||||
if not array_items:
|
||||
# Если нет элементов, возвращаем только кнопку "Назад"
|
||||
keyboard = InlineKeyboardBuilder()
|
||||
home_button = types.InlineKeyboardButton(text="🏠 Назад", callback_data="return")
|
||||
keyboard.row(home_button)
|
||||
return keyboard.as_markup()
|
||||
|
||||
# Определяем общее количество страниц
|
||||
total_pages = (total_items + 9 - 1) // 9
|
||||
items_per_page = 9
|
||||
total_pages = (total_items + items_per_page - 1) // items_per_page
|
||||
|
||||
# Ограничиваем страницу максимальным значением
|
||||
if page > total_pages:
|
||||
page = total_pages
|
||||
|
||||
# Создаем билдер для клавиатуры
|
||||
keyboard = InlineKeyboardBuilder()
|
||||
|
||||
# Вычисляем стартовый номер для текущей страницы
|
||||
start_index = (page - 1) * 9
|
||||
|
||||
# Кнопки с номерами страниц
|
||||
for i in range(start_index, min(start_index + 9, len(array_items))):
|
||||
keyboard.add(types.InlineKeyboardButton(
|
||||
start_index = (page - 1) * items_per_page
|
||||
|
||||
# Кнопки с элементами текущей страницы
|
||||
end_index = min(start_index + items_per_page, len(array_items))
|
||||
current_row = []
|
||||
|
||||
for i in range(start_index, end_index):
|
||||
current_row.append(types.InlineKeyboardButton(
|
||||
text=f"{array_items[i][0]}", callback_data=f"{callback}_{array_items[i][1]}"
|
||||
))
|
||||
keyboard.adjust(3)
|
||||
|
||||
next_button = types.InlineKeyboardButton(
|
||||
text="➡️ Следующая", callback_data=f"page_{page + 1}"
|
||||
)
|
||||
prev_button = types.InlineKeyboardButton(
|
||||
text="⬅️ Предыдущая", callback_data=f"page_{page - 1}"
|
||||
)
|
||||
keyboard.row(prev_button, next_button)
|
||||
home_button = types.InlineKeyboardButton(
|
||||
text="🏠 Назад", callback_data="return")
|
||||
|
||||
# Когда набирается 3 кнопки, добавляем ряд
|
||||
if len(current_row) == 3:
|
||||
keyboard.row(*current_row)
|
||||
current_row = []
|
||||
|
||||
# Добавляем оставшиеся кнопки, если они есть
|
||||
if current_row:
|
||||
keyboard.row(*current_row)
|
||||
|
||||
# Создаем кнопки навигации только если нужно
|
||||
navigation_buttons = []
|
||||
|
||||
# Кнопка "Предыдущая" - показываем только если не первая страница
|
||||
if page > 1:
|
||||
prev_button = types.InlineKeyboardButton(
|
||||
text="⬅️ Предыдущая", callback_data=f"page_{page - 1}"
|
||||
)
|
||||
navigation_buttons.append(prev_button)
|
||||
|
||||
# Кнопка "Следующая" - показываем только если не последняя страница
|
||||
if page < total_pages:
|
||||
next_button = types.InlineKeyboardButton(
|
||||
text="➡️ Следующая", callback_data=f"page_{page + 1}"
|
||||
)
|
||||
navigation_buttons.append(next_button)
|
||||
|
||||
# Добавляем кнопки навигации, если они есть
|
||||
if navigation_buttons:
|
||||
keyboard.row(*navigation_buttons)
|
||||
|
||||
# Кнопка "Назад"
|
||||
home_button = types.InlineKeyboardButton(text="🏠 Назад", callback_data="return")
|
||||
keyboard.row(home_button)
|
||||
k = keyboard.as_markup()
|
||||
return k
|
||||
|
||||
return keyboard.as_markup()
|
||||
|
||||
|
||||
def create_keyboard_for_ban_reason():
|
||||
|
||||
Reference in New Issue
Block a user