catalogue operations, db migr and so on.

pre release version.
This commit is contained in:
2026-03-11 20:26:38 +07:00
parent 1af0cf1826
commit 7f4f8a883d
17 changed files with 284 additions and 63 deletions

View File

@@ -1,4 +1,4 @@
from typing import Optional, Callable
from typing import Optional, Callable, Union
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder
@@ -7,7 +7,7 @@ from config import PAGE_SIZE
from db.models import Category, Product
from dto.cart import CartItemDTO
from dto.catalogue import CatalogueView, CatalogueType
from .common import return_menu
from misc.mapper import serialize_cat_id
def pagination_row(
@@ -37,11 +37,33 @@ def cart_btn(cart_items: int) -> InlineKeyboardButton:
def get_back_to_catalogue(
cat_id: int, *, text: Optional[str] = "⬅️ Назад"
parent_id: int,
*,
text: Optional[str] = "⬅️ Назад",
show_controls: bool = False,
cat_id: Optional[Union[str, int]] = None,
fallback_cb: Optional[str] = "menu:main",
) -> InlineKeyboardMarkup:
return InlineKeyboardBuilder(
[[InlineKeyboardButton(text=text, callback_data=f"cat:{cat_id}")]]
).as_markup()
builder = InlineKeyboardBuilder()
if show_controls and cat_id:
builder.row(
InlineKeyboardButton(
text=" Категория", callback_data=f"new:category:{cat_id}"
),
InlineKeyboardButton(
text=" Товар", callback_data=f"new:product:{cat_id}"
),
)
builder.row(
InlineKeyboardButton(
text=text, callback_data=f"cat:{parent_id}" if parent_id else fallback_cb
),
)
return builder.as_markup()
def main_menu_kb(cart_items: int) -> InlineKeyboardMarkup:
@@ -58,9 +80,10 @@ def main_menu_kb(cart_items: int) -> InlineKeyboardMarkup:
def render_category(
children: list[Category],
parent_id: Optional[int] = None,
parent_id: Optional[Union[str, int]] = None,
show_menu: bool = False,
*,
cat_id: Optional[Union[str, int]] = None,
category_cb_factory: Callable[[Category], str],
is_admin: bool = False,
) -> InlineKeyboardMarkup:
@@ -75,12 +98,16 @@ def render_category(
nav: list[list[InlineKeyboardButton]] = []
if is_admin:
cat_id = serialize_cat_id(cat_id)
nav.append(
[
InlineKeyboardButton(
text="📄 Добавить категорию",
callback_data=f"new:category:{parent_id}",
)
text=" Категория",
callback_data=f"new:category:{cat_id}",
),
InlineKeyboardButton(
text="✍️", callback_data=f"edit:category:{cat_id}"
),
]
)
@@ -124,9 +151,7 @@ def render_products(
if is_admin:
builder.row(
InlineKeyboardButton(
text="📦 Добавить товар", callback_data=f"new:product:{cat_id}"
)
InlineKeyboardButton(text=" Товар", callback_data=f"new:product:{cat_id}")
)
nav_buttons = []
@@ -144,11 +169,12 @@ def render_products(
def render_catalogue(view: CatalogueView, *, is_admin: bool):
if view.view_type == CatalogueType.CATEGORIES:
return render_category(
children=view.children[:PAGE_SIZE], # type: ignore
children=view.children,
parent_id=view.parent_id,
show_menu=view.show_menu,
category_cb_factory=lambda c: f"cat:{c.id}",
is_admin=is_admin,
cat_id=view.category,
)
if view.view_type == CatalogueType.PRODUCTS:
return render_products(
@@ -162,9 +188,12 @@ def render_catalogue(view: CatalogueView, *, is_admin: bool):
f"products:{view.category}:{view.page + 1}" if view.has_next else None
),
is_admin=is_admin,
cat_id=view.category,
)
return return_menu
return get_back_to_catalogue(
view.parent_id, show_controls=is_admin, cat_id=serialize_cat_id(view.category)
)
def render_product_interactions(