chore: minor fixes and formatting

This commit is contained in:
2026-08-12 19:52:30 +07:00
parent a011b2724b
commit ca276cdd61
48 changed files with 394 additions and 354 deletions

View File

@@ -1,4 +1,4 @@
from typing import Optional, Callable, Union
from collections.abc import Callable
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder
@@ -6,15 +6,15 @@ from aiogram.utils.keyboard import InlineKeyboardBuilder
from config import PAGE_SIZE
from db.models import Category, Product
from dto.cart import CartItemDTO
from dto.catalogue import CatalogueView, CatalogueType
from dto.catalogue import CatalogueType, CatalogueView
from misc.mapper import serialize_cat_id
def pagination_row(
*,
page: int,
prev_cb: Optional[str] = None,
next_cb: Optional[str] = None,
prev_cb: str | None = None,
next_cb: str | None = None,
) -> list[InlineKeyboardButton]:
row = []
@@ -39,22 +39,18 @@ def cart_btn(cart_items: int) -> InlineKeyboardButton:
def get_back_to_catalogue(
parent_id: int,
*,
text: Optional[str] = "⬅️ Назад",
text: str | None = "⬅️ Назад",
show_controls: bool = False,
cat_id: Optional[Union[str, int]] = None,
fallback_cb: Optional[str] = "menu:main",
cat_id: str | int | None = None,
fallback_cb: str | None = "menu:main",
) -> InlineKeyboardMarkup:
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}"
),
InlineKeyboardButton(text="➕ Категория", callback_data=f"new:category:{cat_id}"),
InlineKeyboardButton(text="➕ Товар", callback_data=f"new:product:{cat_id}"),
)
builder.row(
@@ -82,7 +78,7 @@ def main_menu_kb(cart_items: int) -> InlineKeyboardMarkup:
[
[
InlineKeyboardButton(text="📦 Каталог", callback_data="cat:root"),
InlineKeyboardButton(text="🔍", callback_data="find"),
InlineKeyboardButton(text="🔍 Поиск", callback_data="find"),
],
[cart_btn(cart_items)],
]
@@ -91,10 +87,10 @@ def main_menu_kb(cart_items: int) -> InlineKeyboardMarkup:
def render_category(
children: list[Category],
parent_id: Optional[Union[str, int]] = None,
parent_id: str | int | None = None,
show_menu: bool = False,
*,
cat_id: Optional[Union[str, int]] = None,
cat_id: str | int | None = None,
category_cb_factory: Callable[[Category], str],
is_admin: bool = False,
) -> InlineKeyboardMarkup:
@@ -116,9 +112,7 @@ def render_category(
text="➕ Категория",
callback_data=f"new:category:{cat_id}",
),
InlineKeyboardButton(
text="✍️", callback_data=f"edit:category:{cat_id}"
),
InlineKeyboardButton(text="✍️", callback_data=f"edit:category:{cat_id}"),
]
)
@@ -126,7 +120,10 @@ def render_category(
nav.append([InlineKeyboardButton(text="⬅️ Назад", callback_data="menu:main")])
elif parent_id is not None:
nav.append(
[InlineKeyboardButton(text="⬅️ Назад", callback_data=f"cat:{parent_id}")]
[
InlineKeyboardButton(text="⬅️ Назад", callback_data=f"cat:{parent_id}"),
InlineKeyboardButton(text="🏠", callback_data="menu:main"),
]
)
return InlineKeyboardBuilder(
@@ -138,22 +135,25 @@ def render_products(
products: list[Product],
*,
product_cb_factory: Callable[[Product], str],
back_cb: Optional[str] = None,
prev_cb: Optional[str] = None,
next_cb: Optional[str] = None,
back_cb: str | None = None,
prev_cb: str | None = None,
next_cb: str | None = None,
columns: int = 2,
is_admin: bool = False,
cat_id: Optional[int] = None,
cat_id: int | None = None,
) -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
if back_cb:
builder.row(InlineKeyboardButton(text="⬅️ Назад", callback_data=back_cb))
builder.row(
InlineKeyboardButton(text="⬅️ Назад", callback_data=back_cb),
InlineKeyboardButton(text="🏠", callback_data="menu:main"),
)
for i in range(0, len(products), columns):
row = [
InlineKeyboardButton(
text=p.name,
text=f"{p.name} {'🙈' if is_admin and p.is_hidden else ''}".strip(),
callback_data=product_cb_factory(p),
)
for p in products[i : i + columns]
@@ -161,9 +161,7 @@ def render_products(
builder.row(*row)
if is_admin:
builder.row(
InlineKeyboardButton(text="➕ Товар", callback_data=f"new:product:{cat_id}")
)
builder.row(InlineKeyboardButton(text="➕ Товар", callback_data=f"new:product:{cat_id}"))
nav_buttons = []
if prev_cb:
@@ -193,12 +191,8 @@ def render_catalogue(view: CatalogueView, *, is_admin: bool):
products=view.products[:PAGE_SIZE], # type: ignore
product_cb_factory=lambda p: f"product:{p.id}",
back_cb=f"cat:{view.parent_id}",
prev_cb=(
f"products:{view.category}:{view.page - 1}" if view.page > 0 else None
),
next_cb=(
f"products:{view.category}:{view.page + 1}" if view.has_next else None
),
prev_cb=(f"products:{view.category}:{view.page - 1}" if view.page > 0 else None),
next_cb=(f"products:{view.category}:{view.page + 1}" if view.has_next else None),
is_admin=is_admin,
cat_id=view.category,
)
@@ -216,13 +210,9 @@ def render_product_interactions(
if show_cart:
btns.append(
[
InlineKeyboardButton(
text="➖", callback_data=f"cart_action:remove:{product_id}"
),
InlineKeyboardButton(text="➖", callback_data=f"cart_action:remove:{product_id}"),
InlineKeyboardButton(text=f"🛒 {cart_amount}", callback_data="..."),
InlineKeyboardButton(
text="➕", callback_data=f"cart_action:add:{product_id}"
),
InlineKeyboardButton(text="➕", callback_data=f"cart_action:add:{product_id}"),
]
)
btns.append(
@@ -243,7 +233,12 @@ def render_product_interactions(
]
)
btns.append([InlineKeyboardButton(text="⬅️", callback_data=f"cat:{category_id}")])
btns.append(
[
InlineKeyboardButton(text="⬅️", callback_data=f"cat:{category_id}"),
InlineKeyboardButton(text="🏠", callback_data="menu:main"),
]
)
return InlineKeyboardBuilder(btns).as_markup()
@@ -252,9 +247,9 @@ def render_cart(
cart_items: list[CartItemDTO],
*,
page: int = 0,
show_prev: Optional[bool] = False,
show_next: Optional[bool] = False,
show_purchase: Optional[bool] = False,
show_prev: bool | None = False,
show_next: bool | None = False,
show_purchase: bool | None = False,
) -> InlineKeyboardMarkup:
btns = [
InlineKeyboardButton(
@@ -284,9 +279,7 @@ def render_cart(
)
if show_purchase:
markup.append(
[InlineKeyboardButton(text="🔷 Оформить заказ", callback_data="checkout")]
)
markup.append([InlineKeyboardButton(text="🔷 Оформить заказ", callback_data="checkout")])
return InlineKeyboardBuilder(markup).as_markup()