separated clients handlers, cart->checkout ppline
This commit is contained in:
91
handlers/client/catalogue.py
Normal file
91
handlers/client/catalogue.py
Normal file
@@ -0,0 +1,91 @@
|
||||
from aiogram import Router, F
|
||||
from aiogram.types import CallbackQuery
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import PAGE_SIZE
|
||||
from misc.kb import render_category
|
||||
from misc.kb.client import (
|
||||
get_back_to_catalogue,
|
||||
render_products,
|
||||
)
|
||||
from repositories.categories import CategoriesRepository
|
||||
from repositories.products import ProductRepository
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.callback_query(F.data.startswith("cat:"))
|
||||
async def subcatalogue(
|
||||
cb: CallbackQuery,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
categories_repo: CategoriesRepository,
|
||||
products_repo: ProductRepository,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
cat_id = cb.data.split(":")[1]
|
||||
|
||||
if cat_id == "root":
|
||||
children = await categories_repo.get_categories_by_parent_id(session)
|
||||
kb = render_category(children, show_menu=True)
|
||||
await cb.message.edit_text(
|
||||
"category selection",
|
||||
reply_markup=kb,
|
||||
)
|
||||
return
|
||||
|
||||
cat_id = int(cat_id)
|
||||
|
||||
children = await categories_repo.get_categories_by_parent_id(session, cat_id)
|
||||
category = await categories_repo.get_category_by_id(session, cat_id)
|
||||
parent_id = category.parent_id or "root"
|
||||
|
||||
if not children:
|
||||
products = await products_repo.get_product_by_category(
|
||||
session, cat_id, limit=PAGE_SIZE + 1, offset=0
|
||||
)
|
||||
products_len = len(products)
|
||||
|
||||
if not products:
|
||||
await cb.message.edit_text(
|
||||
"🍃 Здесь ничего нет...",
|
||||
reply_markup=get_back_to_catalogue(parent_id),
|
||||
)
|
||||
return
|
||||
|
||||
if cb.message.photo:
|
||||
await cb.message.delete()
|
||||
await cb.message.answer(
|
||||
"hya",
|
||||
reply_markup=render_products(
|
||||
products[:PAGE_SIZE],
|
||||
parent_id=parent_id,
|
||||
cat_id=cat_id,
|
||||
page=0,
|
||||
show_prev=products_len < 0,
|
||||
show_next=products_len > PAGE_SIZE,
|
||||
),
|
||||
)
|
||||
return
|
||||
|
||||
await cb.message.edit_text(
|
||||
"hya",
|
||||
reply_markup=render_products(
|
||||
products[:PAGE_SIZE],
|
||||
parent_id=parent_id,
|
||||
cat_id=cat_id,
|
||||
page=0,
|
||||
show_prev=products_len < 0,
|
||||
show_next=products_len > PAGE_SIZE,
|
||||
),
|
||||
)
|
||||
return
|
||||
|
||||
kb = render_category(children, parent_id)
|
||||
|
||||
await cb.message.edit_text(
|
||||
"category selection",
|
||||
reply_markup=kb,
|
||||
)
|
||||
Reference in New Issue
Block a user