Refactor promo code management in admin panel for improved clarity and localization
- Simplified the handling of promo codes by consolidating text messages and improving localization for better user experience. - Updated the structure of promo details and management views, enhancing readability and consistency in the admin panel. - Removed deprecated text and streamlined the activation status messages, ensuring accurate and clear communication for admins.
This commit is contained in:
@@ -34,16 +34,12 @@ async def view_promo_codes_handler(callback: types.CallbackQuery,
|
||||
offset=0)
|
||||
|
||||
if not promo_models:
|
||||
text = _(
|
||||
"admin_no_promo_codes",
|
||||
default="📋 <b>Активные промокоды</b>\n\n❌ Нет активных промокодов"
|
||||
)
|
||||
text = f"{_('admin_active_promos_list_header')}\n\n{_('admin_no_active_promos')}"
|
||||
else:
|
||||
lines = [_("admin_active_promo_codes_title",
|
||||
default="📋 <b>Активные промокоды</b>\n")]
|
||||
lines = [_("admin_active_promos_list_header"), ""]
|
||||
|
||||
for promo in promo_models:
|
||||
validity_str = _("admin_promo_unlimited", default="Без ограничений")
|
||||
validity_str = _("admin_promo_valid_indefinitely")
|
||||
if promo.valid_until:
|
||||
validity_str = promo.valid_until.strftime("%d.%m.%Y")
|
||||
|
||||
@@ -90,10 +86,7 @@ async def promo_management_handler(callback: types.CallbackQuery,
|
||||
promo_models = await promo_code_dal.get_all_promo_codes_with_details(session, limit=50, offset=0)
|
||||
|
||||
if not promo_models:
|
||||
text = _(
|
||||
"admin_no_promo_codes_to_manage",
|
||||
default="🛠 <b>Управление промокодами</b>\n\n❌ Нет промокодов для управления"
|
||||
)
|
||||
text = _("admin_promo_management_empty")
|
||||
await callback.message.edit_text(
|
||||
text,
|
||||
reply_markup=get_back_to_admin_panel_keyboard(current_lang, i18n),
|
||||
@@ -103,21 +96,9 @@ async def promo_management_handler(callback: types.CallbackQuery,
|
||||
return
|
||||
|
||||
builder = InlineKeyboardBuilder()
|
||||
text_lines = [_("admin_promo_management_title", default="🛠 <b>Управление промокодами</b>\n")]
|
||||
text = _("admin_promo_management_title")
|
||||
|
||||
for promo in promo_models:
|
||||
status_emoji = "✅" if promo.is_active else "❌"
|
||||
validity_str = _("admin_promo_unlimited", default="∞")
|
||||
if promo.valid_until:
|
||||
validity_str = promo.valid_until.strftime("%d.%m")
|
||||
|
||||
text_lines.append(
|
||||
f"{status_emoji} <code>{promo.code}</code> | "
|
||||
f"🎁{promo.bonus_days}д | "
|
||||
f"📊{promo.current_activations}/{promo.max_activations} | "
|
||||
f"⏰{validity_str}"
|
||||
)
|
||||
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=f"📝 {promo.code}",
|
||||
@@ -127,13 +108,11 @@ async def promo_management_handler(callback: types.CallbackQuery,
|
||||
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_back_to_panel", default="🔙 В админ панель"),
|
||||
text=_("back_to_admin_panel_button"),
|
||||
callback_data="admin_action:main"
|
||||
)
|
||||
)
|
||||
|
||||
text = "\n".join(text_lines)
|
||||
|
||||
try:
|
||||
await callback.message.edit_text(
|
||||
text,
|
||||
@@ -170,80 +149,65 @@ async def promo_detail_handler(callback: types.CallbackQuery,
|
||||
promo = await promo_code_dal.get_promo_code_by_id(session, promo_id)
|
||||
|
||||
if not promo:
|
||||
await callback.answer(_("admin_promo_not_found", default="❌ Промокод не найден"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
return
|
||||
|
||||
# Format promo details
|
||||
status_text = _("admin_promo_active", default="✅ Активен") if promo.is_active else _("admin_promo_inactive", default="❌ Неактивен")
|
||||
validity_text = _("admin_promo_unlimited", default="Без ограничений")
|
||||
status = _("admin_promo_status_active") if promo.is_active else _("admin_promo_status_inactive")
|
||||
if promo.valid_until and promo.valid_until < datetime.now(timezone.utc):
|
||||
status = _("admin_promo_status_expired")
|
||||
elif promo.current_activations >= promo.max_activations:
|
||||
status = _("admin_promo_status_used_up")
|
||||
|
||||
validity = _("admin_promo_valid_indefinitely")
|
||||
if promo.valid_until:
|
||||
if promo.valid_until > datetime.now(timezone.utc):
|
||||
validity_text = promo.valid_until.strftime("%d.%m.%Y %H:%M")
|
||||
else:
|
||||
validity_text = f"{promo.valid_until.strftime('%d.%m.%Y %H:%M')} (истек)"
|
||||
validity = promo.valid_until.strftime("%d.%m.%Y %H:%M")
|
||||
|
||||
created_at = promo.created_at.strftime("%d.%m.%Y %H:%M") if promo.created_at else "N/A"
|
||||
created = promo.created_at.strftime("%d.%m.%Y %H:%M") if promo.created_at else "N/A"
|
||||
|
||||
text = _(
|
||||
"admin_promo_detail_info",
|
||||
default="🎟 <b>Детали промокода</b>\n\n"
|
||||
"📝 Код: <code>{code}</code>\n"
|
||||
"🎁 Бонусные дни: <b>{bonus_days}</b>\n"
|
||||
"📊 Использований: <b>{current}/{max}</b>\n"
|
||||
"📋 Статус: {status}\n"
|
||||
"⏰ Действует до: <b>{validity}</b>\n"
|
||||
"👤 Создал: <code>{admin_id}</code>\n"
|
||||
"🕐 Создан: <b>{created}</b>",
|
||||
code=promo.code,
|
||||
bonus_days=promo.bonus_days,
|
||||
current=promo.current_activations,
|
||||
max=promo.max_activations,
|
||||
status=status_text,
|
||||
validity=validity_text,
|
||||
admin_id=promo.created_by_admin_id,
|
||||
created=created_at
|
||||
)
|
||||
text = "\n".join([
|
||||
_("admin_promo_card_title", code=promo.code),
|
||||
_("admin_promo_card_bonus_days", days=promo.bonus_days),
|
||||
_("admin_promo_card_activations", current=promo.current_activations, max=promo.max_activations),
|
||||
_("admin_promo_card_validity", validity=validity),
|
||||
_("admin_promo_card_status", status=status),
|
||||
_("admin_promo_card_created", created=created),
|
||||
_("admin_promo_card_created_by", creator=promo.created_by_admin_id)
|
||||
])
|
||||
|
||||
# Create management buttons
|
||||
builder = InlineKeyboardBuilder()
|
||||
|
||||
# Edit button
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_promo_edit", default="✏️ Редактировать"),
|
||||
text=_("admin_promo_edit_button"),
|
||||
callback_data=f"promo_edit:{promo_id}"
|
||||
)
|
||||
)
|
||||
|
||||
# Toggle active/inactive
|
||||
toggle_text = _("admin_promo_deactivate", default="❌ Деактивировать") if promo.is_active else _("admin_promo_activate", default="✅ Активировать")
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=toggle_text,
|
||||
text=_("admin_promo_toggle_status_button"),
|
||||
callback_data=f"promo_toggle:{promo_id}"
|
||||
)
|
||||
)
|
||||
|
||||
# View activations
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_promo_view_activations", default="📋 Активации"),
|
||||
text=_("admin_promo_view_activations_button"),
|
||||
callback_data=f"promo_activations:{promo_id}"
|
||||
)
|
||||
)
|
||||
|
||||
# Delete button
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_promo_delete", default="🗑 Удалить"),
|
||||
text=_("admin_promo_delete_button"),
|
||||
callback_data=f"promo_delete:{promo_id}"
|
||||
)
|
||||
)
|
||||
|
||||
# Back button
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_back_to_management", default="🔙 К управлению"),
|
||||
text=_("admin_promo_back_to_list_button"),
|
||||
callback_data="admin_action:promo_management"
|
||||
)
|
||||
)
|
||||
@@ -263,7 +227,7 @@ async def promo_detail_handler(callback: types.CallbackQuery,
|
||||
await callback.answer()
|
||||
|
||||
except ValueError:
|
||||
await callback.answer(_("admin_invalid_promo_id", default="❌ Неверный ID промокода"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
except Exception as e:
|
||||
logging.error(f"Error in promo detail: {e}")
|
||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||
@@ -285,7 +249,7 @@ async def promo_toggle_handler(callback: types.CallbackQuery, i18n_data: dict,
|
||||
promo = await promo_code_dal.get_promo_code_by_id(session, promo_id)
|
||||
|
||||
if not promo:
|
||||
await callback.answer(_("admin_promo_not_found", default="❌ Промокод не найден"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
return
|
||||
|
||||
new_status = not promo.is_active
|
||||
@@ -294,9 +258,9 @@ async def promo_toggle_handler(callback: types.CallbackQuery, i18n_data: dict,
|
||||
updated = await promo_code_dal.update_promo_code(session, promo_id, update_data)
|
||||
if updated:
|
||||
await session.commit()
|
||||
status_text = _("admin_promo_activated", default="активирован") if new_status else _("admin_promo_deactivated", default="деактивирован")
|
||||
status_text = _("admin_promo_status_activated") if new_status else _("admin_promo_status_deactivated")
|
||||
await callback.answer(
|
||||
_("admin_promo_status_changed", default="✅ Промокод {status}", status=status_text)
|
||||
_("admin_promo_toggle_success", code=promo.code, status=status_text)
|
||||
)
|
||||
# Refresh the detail view
|
||||
callback.data = f"promo_detail:{promo_id}"
|
||||
@@ -305,7 +269,7 @@ async def promo_toggle_handler(callback: types.CallbackQuery, i18n_data: dict,
|
||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||
|
||||
except ValueError:
|
||||
await callback.answer(_("admin_invalid_promo_id", default="❌ Неверный ID промокода"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
except Exception as e:
|
||||
logging.error(f"Error toggling promo: {e}")
|
||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||
@@ -327,35 +291,31 @@ async def promo_activations_handler(callback: types.CallbackQuery, i18n_data: di
|
||||
promo = await promo_code_dal.get_promo_code_by_id(session, promo_id)
|
||||
|
||||
if not promo:
|
||||
await callback.answer(_("admin_promo_not_found", default="❌ Промокод не найден"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
return
|
||||
|
||||
activations = await promo_code_dal.get_promo_activations_by_code_id(session, promo_id)
|
||||
|
||||
text_lines = [
|
||||
_("admin_promo_activations_title",
|
||||
default="📋 <b>Активации промокода {code}</b>\n",
|
||||
code=promo.code)
|
||||
]
|
||||
|
||||
if not activations:
|
||||
text_lines.append(_("admin_no_activations", default="❌ Нет активаций"))
|
||||
text = _("admin_promo_no_activations", code=promo.code)
|
||||
else:
|
||||
for activation in activations[:10]: # Show last 10
|
||||
activated_at = activation.activated_at.strftime("%d.%m.%Y %H:%M") if activation.activated_at else "N/A"
|
||||
text_lines = [
|
||||
_("admin_promo_activations_header", code=promo.code)
|
||||
]
|
||||
for activation in activations[:20]:
|
||||
text_lines.append(
|
||||
f"👤 <code>{activation.user_id}</code> | 🕐 {activated_at}"
|
||||
_("admin_promo_activation_item",
|
||||
user_id=activation.user_id,
|
||||
date=activation.activated_at.strftime("%d.%m.%Y %H:%M"))
|
||||
)
|
||||
|
||||
if len(activations) > 10:
|
||||
text_lines.append(f"\n... и еще {len(activations) - 10}")
|
||||
|
||||
text = "\n".join(text_lines)
|
||||
if len(activations) > 20:
|
||||
text_lines.append(f"\n... (еще {len(activations) - 20})")
|
||||
text = "\n".join(text_lines)
|
||||
|
||||
builder = InlineKeyboardBuilder()
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text=_("admin_back_to_promo", default="🔙 К промокоду"),
|
||||
text=_("admin_promo_back_to_detail_button"),
|
||||
callback_data=f"promo_detail:{promo_id}"
|
||||
)
|
||||
)
|
||||
@@ -375,7 +335,7 @@ async def promo_activations_handler(callback: types.CallbackQuery, i18n_data: di
|
||||
await callback.answer()
|
||||
|
||||
except ValueError:
|
||||
await callback.answer(_("admin_invalid_promo_id", default="❌ Неверный ID промокода"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
except Exception as e:
|
||||
logging.error(f"Error viewing activations: {e}")
|
||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||
@@ -399,16 +359,16 @@ async def promo_delete_handler(callback: types.CallbackQuery, i18n_data: dict,
|
||||
if promo:
|
||||
await session.commit()
|
||||
await callback.answer(
|
||||
_("admin_promo_deleted", default="✅ Промокод удален", code=promo.code)
|
||||
_("admin_promo_deleted_success", code=promo.code)
|
||||
)
|
||||
# Go back to management
|
||||
callback.data = "admin_action:promo_management"
|
||||
await promo_management_handler(callback, i18n_data, settings, session)
|
||||
else:
|
||||
await callback.answer(_("admin_promo_not_found", default="❌ Промокод не найден"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
|
||||
except ValueError:
|
||||
await callback.answer(_("admin_invalid_promo_id", default="❌ Неверный ID промокода"), show_alert=True)
|
||||
await callback.answer(_("admin_promo_not_found"), show_alert=True)
|
||||
except Exception as e:
|
||||
logging.error(f"Error deleting promo: {e}")
|
||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||
|
||||
Reference in New Issue
Block a user