Update banned users handling with async support and improved date parsing
- Modified `change_page` function in `callback_handlers.py` to use async methods for retrieving banned users and their buttons. - Enhanced `get_banned_users_list` in `helper_func.py` to handle string timestamps and various date formats, ensuring robust date parsing. - Added a new test case in `test_utils.py` to validate the handling of string timestamps in the banned users list retrieval.
This commit is contained in:
@@ -219,14 +219,14 @@ async def change_page(
|
||||
reply_markup=keyboard
|
||||
)
|
||||
else:
|
||||
message_user = get_banned_users_list(int(page_number) * 7 - 7, bot_db)
|
||||
message_user = await get_banned_users_list(int(page_number) * 7 - 7, bot_db)
|
||||
await call.bot.edit_message_text(
|
||||
chat_id=call.message.chat.id,
|
||||
message_id=call.message.message_id,
|
||||
text=message_user
|
||||
)
|
||||
|
||||
buttons = get_banned_users_buttons(bot_db)
|
||||
buttons = await get_banned_users_buttons(bot_db)
|
||||
keyboard = create_keyboard_with_pagination(page_number, len(buttons), buttons, 'unlock')
|
||||
await call.bot.edit_message_reply_markup(
|
||||
chat_id=call.message.chat.id,
|
||||
|
||||
@@ -696,9 +696,22 @@ async def get_banned_users_list(offset: int, bot_db):
|
||||
if isinstance(unban_date, (int, float)):
|
||||
unban_datetime = datetime.fromtimestamp(unban_date)
|
||||
safe_unban_date = unban_datetime.strftime("%d-%m-%Y %H:%M")
|
||||
else:
|
||||
# Если это уже datetime объект
|
||||
elif isinstance(unban_date, str):
|
||||
# Если это строка, попытаемся её обработать
|
||||
try:
|
||||
# Попробуем преобразовать строку в timestamp
|
||||
timestamp = int(unban_date)
|
||||
unban_datetime = datetime.fromtimestamp(timestamp)
|
||||
safe_unban_date = unban_datetime.strftime("%d-%m-%Y %H:%M")
|
||||
except (ValueError, TypeError):
|
||||
# Если не удалось, показываем как есть
|
||||
safe_unban_date = html.escape(str(unban_date))
|
||||
elif hasattr(unban_date, 'strftime'):
|
||||
# Если это datetime объект
|
||||
safe_unban_date = unban_date.strftime("%d-%m-%Y %H:%M")
|
||||
else:
|
||||
# Для всех остальных случаев
|
||||
safe_unban_date = html.escape(str(unban_date))
|
||||
except (ValueError, TypeError, OSError):
|
||||
# В случае ошибки показываем исходное значение
|
||||
safe_unban_date = html.escape(str(unban_date))
|
||||
|
||||
Reference in New Issue
Block a user