add subgroups for gifts
This commit is contained in:
@@ -6,9 +6,11 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, delete, func
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||
|
||||
|
||||
from ..panel.keyboard import AdminPanelCallback
|
||||
from .keyboard import build_admin_gifts_kb, build_gifts_list_kb
|
||||
from database.models import Tariff, Gift, GiftUsage
|
||||
from handlers.utils import format_days, format_months
|
||||
|
||||
|
||||
router = Router()
|
||||
@@ -41,9 +43,16 @@ async def admin_create_gift_step1(callback: CallbackQuery, session: AsyncSession
|
||||
|
||||
kb = InlineKeyboardBuilder()
|
||||
for t in tariffs:
|
||||
kb.button(text=f"{t.name} – {t.duration_days // 30} мес", callback_data=f"admin_gift_select|{t.id}")
|
||||
kb.row(types.InlineKeyboardButton(text="🔙 Назад", callback_data=AdminPanelCallback(action="gifts").pack()
|
||||
))
|
||||
if t.duration_days % 30 == 0:
|
||||
duration_text = format_months(t.duration_days // 30)
|
||||
else:
|
||||
duration_text = format_days(t.duration_days)
|
||||
|
||||
kb.button(
|
||||
text=f"{t.name} – {duration_text}",
|
||||
callback_data=f"admin_gift_select|{t.id}"
|
||||
)
|
||||
|
||||
|
||||
await callback.message.edit_text(
|
||||
"🎁 Выберите тариф для подарка:",
|
||||
@@ -89,10 +98,12 @@ async def handle_limited_gift_input(message: types.Message, session: AsyncSessio
|
||||
await state.clear()
|
||||
await finalize_gift(message, session, bot, data, is_unlimited=False)
|
||||
|
||||
|
||||
@router.callback_query(F.data == "admin_gifts_all")
|
||||
async def show_gifts_page(callback: CallbackQuery, session: AsyncSession):
|
||||
await show_gift_list(callback, session, page=1)
|
||||
|
||||
|
||||
@router.callback_query(F.data.startswith("gifts_page|"))
|
||||
async def paginate_gifts(callback: CallbackQuery, session: AsyncSession):
|
||||
page = int(callback.data.split("|")[1])
|
||||
@@ -173,15 +184,20 @@ async def view_gift(callback: CallbackQuery, session: AsyncSession):
|
||||
select(func.count()).select_from(GiftUsage).where(GiftUsage.gift_id == gift_id)
|
||||
)
|
||||
used_count = usage_result.scalar_one()
|
||||
|
||||
usage_text = f"{used_count}/{gift.max_usages}" if gift.max_usages else "∞"
|
||||
|
||||
duration_days = (gift.expiry_time.date() - gift.created_at.date()).days
|
||||
if duration_days % 30 == 0:
|
||||
duration_text = format_months(duration_days // 30)
|
||||
else:
|
||||
duration_text = format_days(duration_days)
|
||||
|
||||
text = (
|
||||
f"🎁 <b>Подарок</b>\n"
|
||||
f"ID: <code>{gift.gift_id}</code>\n"
|
||||
f"Месяцев: <b>{gift.selected_months}</b>\n"
|
||||
f"Срок: <b>{duration_text}</b>\n"
|
||||
f"Активаций: <b>{usage_text}</b>\n"
|
||||
f"Срок действия: <i>{gift.expiry_time.strftime('%d.%m.%Y')}</i>\n"
|
||||
f"Истекает: <i>{gift.expiry_time.strftime('%d.%m.%Y')}</i>\n"
|
||||
f"<b>Ссылка для активации:</b>\n<blockquote>{gift.gift_link}</blockquote>"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||
from database.models import Tariff, Gift
|
||||
from ..panel.keyboard import AdminPanelCallback
|
||||
from handlers.utils import get_plural_form, format_months, format_days
|
||||
|
||||
from handlers.buttons import BACK
|
||||
|
||||
@@ -35,21 +36,38 @@ def build_gift_tariffs_kb(tariffs: list[Tariff]) -> InlineKeyboardMarkup:
|
||||
|
||||
def build_gifts_list_kb(gifts: list[Gift], page: int, total: int) -> InlineKeyboardMarkup:
|
||||
builder = InlineKeyboardBuilder()
|
||||
row = []
|
||||
|
||||
for gift in gifts:
|
||||
button_text = f"{gift.gift_id[:6]}... — {gift.selected_months} мес."
|
||||
builder.button(
|
||||
text=button_text,
|
||||
callback_data=f"gift_view|{gift.gift_id}",
|
||||
for i, gift in enumerate(gifts):
|
||||
if gift.selected_months > 0:
|
||||
duration_text = format_months(gift.selected_months)
|
||||
else:
|
||||
days = (gift.expiry_time.date() - gift.created_at.date()).days
|
||||
duration_text = format_days(days)
|
||||
|
||||
button_text = f"{gift.gift_id[:6]}... — {duration_text}"
|
||||
|
||||
row.append(
|
||||
InlineKeyboardButton(
|
||||
text=button_text,
|
||||
callback_data=f"gift_view|{gift.gift_id}",
|
||||
)
|
||||
)
|
||||
|
||||
if len(row) == 2 or i == len(gifts) - 1:
|
||||
builder.row(*row)
|
||||
row = []
|
||||
|
||||
nav = []
|
||||
if page > 1:
|
||||
nav.append(InlineKeyboardButton(text="⬅️ Назад", callback_data=f"gifts_page|{page - 1}"))
|
||||
if len(gifts) == 10:
|
||||
nav.append(InlineKeyboardButton(text="➡️ Далее", callback_data=f"gifts_page|{page + 1}"))
|
||||
if nav:
|
||||
builder.row(*nav)
|
||||
|
||||
builder.row(*nav)
|
||||
builder.button(text="🔙 Назад", callback_data=AdminPanelCallback(action="gifts").pack())
|
||||
builder.row(
|
||||
InlineKeyboardButton(text="🔙 Назад", callback_data=AdminPanelCallback(action="gifts").pack())
|
||||
)
|
||||
|
||||
return builder.as_markup()
|
||||
@@ -46,14 +46,22 @@ def build_cancel_kb() -> InlineKeyboardMarkup:
|
||||
|
||||
def build_tariff_groups_kb(groups: list[str]) -> InlineKeyboardMarkup:
|
||||
builder = InlineKeyboardBuilder()
|
||||
for group in groups:
|
||||
builder.button(
|
||||
text=group,
|
||||
callback_data=AdminTariffCallback(action=f"group|{group}").pack(),
|
||||
row = []
|
||||
|
||||
for i, group in enumerate(groups):
|
||||
row.append(
|
||||
InlineKeyboardButton(
|
||||
text=group,
|
||||
callback_data=AdminTariffCallback(action=f"group|{group}").pack(),
|
||||
)
|
||||
)
|
||||
if len(row) == 2 or i == len(groups) - 1:
|
||||
builder.row(*row)
|
||||
row = []
|
||||
builder.row(
|
||||
InlineKeyboardButton(
|
||||
text="⬅️ Назад", callback_data=AdminPanelCallback(action="tariffs").pack()
|
||||
text="⬅️ Назад",
|
||||
callback_data=AdminPanelCallback(action="tariffs").pack(),
|
||||
)
|
||||
)
|
||||
return builder.as_markup()
|
||||
|
||||
Binary file not shown.
@@ -173,13 +173,6 @@ def format_days(days: int) -> str:
|
||||
return f"{days} {get_plural_form(days, 'день', 'дня', 'дней')}"
|
||||
|
||||
|
||||
def format_hours(hours: int) -> str:
|
||||
"""Форматирует количество часов с правильным склонением"""
|
||||
if hours <= 0:
|
||||
return "0 часов"
|
||||
return f"{hours} {get_plural_form(hours, 'час', 'часа', 'часов')}"
|
||||
|
||||
|
||||
def format_minutes(minutes: int) -> str:
|
||||
"""Форматирует количество минут с правильным склонением"""
|
||||
if minutes <= 0:
|
||||
|
||||
Reference in New Issue
Block a user