diff --git a/config.py b/config.py index 17bf8fb..facaa60 100644 --- a/config.py +++ b/config.py @@ -29,6 +29,6 @@ REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/1") ### Constants ### -PAGE_SIZE = 4 +PAGE_SIZE = 8 VERIFIED_ACCOUNTS = [1026030711, 8480400744] NOTIFICATION_CHANNEL = -1003849564110 diff --git a/dto/catalogue.py b/dto/catalogue.py index 5ed188c..9748325 100644 --- a/dto/catalogue.py +++ b/dto/catalogue.py @@ -33,8 +33,8 @@ class NewCatalogueElement: @dataclass -class NewCategoryContext(NewCatalogueElement): ... +class CategoryActionContext(NewCatalogueElement): ... @dataclass -class NewProductContext(NewCatalogueElement): ... +class ProductActionContext(NewCatalogueElement): ... diff --git a/handlers/admins/creation.py b/handlers/admins/creation.py index 9a09759..5c2414f 100644 --- a/handlers/admins/creation.py +++ b/handlers/admins/creation.py @@ -3,9 +3,9 @@ from aiogram.types import CallbackQuery, Message from aiogram.fsm.context import FSMContext from sqlalchemy.ext.asyncio import AsyncSession -from dto.catalogue import NewCategoryContext +from dto.catalogue import CategoryActionContext from misc.filters import IsVerified -from misc.kb.client import get_back_to_catalogue +from misc.kb.client import created_category, get_back_to_catalogue from misc.mapper import parse_cat_id from misc.states import AdminControlStorage from misc.kb.common import return_menu @@ -36,7 +36,7 @@ async def creation_init( reply_markup=get_back_to_catalogue(cat_id), ) - ctx = NewCategoryContext(cb.message, cat_id) + ctx = CategoryActionContext(cb.message, cat_id) await state.set_state(AdminControlStorage.new_category) await state.set_data({"ctx": ctx}) @@ -54,7 +54,7 @@ async def create_new_category( await msg.delete() data = await state.get_data() - ctx: NewCategoryContext | None = data.get("ctx") + ctx: CategoryActionContext | None = data.get("ctx") if not ctx: await msg.reply("❌ Ошибка при создании категории.", reply_markup=return_menu) @@ -66,5 +66,7 @@ async def create_new_category( await ctx.msg.edit_text( f"✅ Категория {msg.text} создана успешно.", - reply_markup=get_back_to_catalogue(category.id, text="🛒 Перейти"), + reply_markup=created_category( + category.id, f"🛒 {category.name}", category.parent_id + ), ) diff --git a/handlers/admins/product_mgmt.py b/handlers/admins/product_mgmt.py index 73c9db0..e5b14c3 100644 --- a/handlers/admins/product_mgmt.py +++ b/handlers/admins/product_mgmt.py @@ -4,11 +4,15 @@ from aiogram.types import Message, CallbackQuery from aiogram.fsm.context import FSMContext from sqlalchemy.ext.asyncio import AsyncSession +from dto.catalogue import CategoryActionContext from dto.control import EditProductContext from misc.filters import IsVerified from misc.kb.admins import back_to_product_kb +from misc.kb.client import get_back_to_catalogue +from misc.mapper import parse_cat_id from misc.states import AdminControlStorage from misc.texts import product_editing_mapping +from repositories.categories import CategoriesRepository from repositories.products import ProductRepository logger = logging.getLogger(__name__) @@ -93,3 +97,52 @@ async def edit_product_query( except Exception as e: logger.exception(e) await ctx.msg.edit_text(text="‼️ Что-то пошло не так.", reply_markup=kb) + + +@router.callback_query(F.data.startswith("edit:")) +async def edit_entry(cb: CallbackQuery, state: FSMContext): + await state.clear() + + data = cb.data.split(":") + mode = data[1] + entry_id = data[2] + + if mode == "category": + await cb.message.edit_text( + "✍️ Введите новое название категории.", + reply_markup=get_back_to_catalogue(entry_id), + ) + ctx = CategoryActionContext(cb.message, entry_id) + await state.set_state(AdminControlStorage.edit_category) + await state.set_data({"ctx": ctx}) + + if mode == "product": + ... + + +@router.message(AdminControlStorage.edit_category) +async def edit_category( + msg: Message, + state: FSMContext, + session: AsyncSession, + categories_repo: CategoriesRepository, +): + await msg.delete() + data = await state.get_data() + await state.clear() + + ctx: CategoryActionContext | None = data.get("ctx") + if not ctx: + await msg.answer("❌ Ошибка при редактировании категории.") + logger.error("no context found while editing category!") + return + + await ctx.msg.edit_text("⏳") + + cat = await categories_repo.update_category_name( + session=session, category_id=parse_cat_id(ctx.parent_id), value=msg.text + ) + + await ctx.msg.edit_text( + "✅ Успешно.", reply_markup=get_back_to_catalogue(cat.parent_id) + ) diff --git a/misc/kb/client.py b/misc/kb/client.py index d2542c1..bb26616 100644 --- a/misc/kb/client.py +++ b/misc/kb/client.py @@ -66,6 +66,17 @@ def get_back_to_catalogue( return builder.as_markup() +def created_category(cat_id: int, cat_name: str, parent_id: int): + return InlineKeyboardBuilder( + [ + [ + InlineKeyboardButton(text=cat_name, callback_data=f"cat:{cat_id}"), + InlineKeyboardButton(text="⬅️ Назад", callback_data=f"cat:{parent_id}"), + ] + ] + ).as_markup() + + def main_menu_kb(cart_items: int) -> InlineKeyboardMarkup: return InlineKeyboardBuilder( [ @@ -157,6 +168,7 @@ def render_products( nav_buttons = [] if prev_cb: nav_buttons.append(InlineKeyboardButton(text="◀️", callback_data=prev_cb)) + nav_buttons.append(InlineKeyboardButton(text="🔍", callback_data="find")) if next_cb: nav_buttons.append(InlineKeyboardButton(text="▶️", callback_data=next_cb)) diff --git a/misc/states.py b/misc/states.py index 00c432d..6e52511 100644 --- a/misc/states.py +++ b/misc/states.py @@ -18,4 +18,5 @@ class SearchStorage(StatesGroup): class AdminControlStorage(StatesGroup): edit_product = State() + edit_category = State() new_category = State() diff --git a/repositories/categories.py b/repositories/categories.py index ab2047a..f012128 100644 --- a/repositories/categories.py +++ b/repositories/categories.py @@ -53,3 +53,12 @@ class CategoriesRepository: await session.commit() return category + + async def update_category_name( + self, session: AsyncSession, category_id: int, value: str + ): + category = await self.get_category_by_id(session, category_id) + category.name = value + + await session.commit() + return category