editing updates, cosmetic changes
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -33,8 +33,8 @@ class NewCatalogueElement:
|
||||
|
||||
|
||||
@dataclass
|
||||
class NewCategoryContext(NewCatalogueElement): ...
|
||||
class CategoryActionContext(NewCatalogueElement): ...
|
||||
|
||||
|
||||
@dataclass
|
||||
class NewProductContext(NewCatalogueElement): ...
|
||||
class ProductActionContext(NewCatalogueElement): ...
|
||||
|
||||
@@ -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
|
||||
),
|
||||
)
|
||||
|
||||
@@ -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(
|
||||
"<b>✍️ Введите новое название категории.</b>",
|
||||
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)
|
||||
)
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -18,4 +18,5 @@ class SearchStorage(StatesGroup):
|
||||
|
||||
class AdminControlStorage(StatesGroup):
|
||||
edit_product = State()
|
||||
edit_category = State()
|
||||
new_category = State()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user