chore: minor fixes and formatting
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
from misc.kb.admins import *
|
||||
from misc.kb.client import *
|
||||
|
||||
__all__ = ["admins", "client"]
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
from typing import Optional, Callable, Union
|
||||
from collections.abc import Callable
|
||||
|
||||
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||
|
||||
from db.models import Product
|
||||
|
||||
main_menu: InlineKeyboardMarkup = InlineKeyboardBuilder(
|
||||
[
|
||||
[InlineKeyboardButton(text="📦 Изменить каталог", callback_data="cat:root")],
|
||||
@@ -10,22 +13,18 @@ main_menu: InlineKeyboardMarkup = InlineKeyboardBuilder(
|
||||
).as_markup()
|
||||
|
||||
|
||||
def back_to_product_kb(
|
||||
product_id: Union[int, str], *, cb_factory: Callable[[int | str], str]
|
||||
):
|
||||
def back_to_product_kb(product_id: int | str, *, cb_factory: Callable[[int | str], str]):
|
||||
return InlineKeyboardBuilder(
|
||||
[[InlineKeyboardButton(text="⬅️", callback_data=cb_factory(product_id))]]
|
||||
).as_markup()
|
||||
|
||||
|
||||
def customer_contacts(
|
||||
user_id: int, has_mention: bool, mention: Optional[str] = None
|
||||
user_id: int, has_mention: bool, mention: str | None = None
|
||||
) -> InlineKeyboardMarkup:
|
||||
btns: list[list[InlineKeyboardButton]] = []
|
||||
(
|
||||
btns.append(
|
||||
[InlineKeyboardButton(text="👤 Перейти", url=f"https://t.me/{mention}")]
|
||||
)
|
||||
btns.append([InlineKeyboardButton(text="👤 Перейти", url=f"https://t.me/{mention}")])
|
||||
if has_mention
|
||||
else None
|
||||
)
|
||||
@@ -40,9 +39,7 @@ def customer_contacts(
|
||||
|
||||
|
||||
def payment_link(url: str) -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardBuilder(
|
||||
[[InlineKeyboardButton(text="💸 Оплатить", url=url)]]
|
||||
).as_markup()
|
||||
return InlineKeyboardBuilder([[InlineKeyboardButton(text="💸 Оплатить", url=url)]]).as_markup()
|
||||
|
||||
|
||||
def verify_payment(invoice_id: int):
|
||||
@@ -57,14 +54,15 @@ def verify_payment(invoice_id: int):
|
||||
).as_markup()
|
||||
|
||||
|
||||
def edit_product(product_id: int, category_id: int):
|
||||
def edit_product(product: Product):
|
||||
product_id = product.id
|
||||
prefix = f"edit_product:{product_id}"
|
||||
visibility_btn = "🙈 Скрыт" if product.is_hidden else "👁️ Видим"
|
||||
|
||||
return InlineKeyboardBuilder(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="✍️ Название", callback_data=f"{prefix}:name"
|
||||
),
|
||||
InlineKeyboardButton(text="✍️ Название", callback_data=f"{prefix}:name"),
|
||||
InlineKeyboardButton(
|
||||
text="✍️ Описание",
|
||||
callback_data=f"{prefix}:description",
|
||||
@@ -72,9 +70,9 @@ def edit_product(product_id: int, category_id: int):
|
||||
],
|
||||
[InlineKeyboardButton(text="💸 Цена", callback_data=f"{prefix}:price")],
|
||||
[
|
||||
InlineKeyboardButton(text="👁️", callback_data=f"{prefix}:hide"),
|
||||
InlineKeyboardButton(text=visibility_btn, callback_data=f"{prefix}:hide"),
|
||||
InlineKeyboardButton(text="❌", callback_data=f"{prefix}:delete"),
|
||||
],
|
||||
[InlineKeyboardButton(text="⬅️", callback_data=f"cat:{category_id}")],
|
||||
[InlineKeyboardButton(text="⬅️", callback_data=f"cat:{product.category_id}")],
|
||||
]
|
||||
).as_markup()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user