O
O
Oleksandr Serbinov2021-07-20 19:39:28
Python
Oleksandr Serbinov, 2021-07-20 19:39:28

How to make the loop display messages in which I entered the data. After adding two products, the loop enters 9-10 messages and the data goes scatter?

Has an admin panel

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Command

from data import config, items
from keyboards.default.admin_panel_keyboards import admin_panel_keyboard, admin_panel_keyboard_category, \
    admin_panel_confirm_keyboard
from loader import dp
from states.admin_panel_state import AdminAppend


# Показывает категории администратору
@dp.message_handler(Command('Admin'), user_id=config.admins)
async def admin_panel_category(message: types.Message):
    await message.answer('Панель администратора\n'
                         'Выберите категорию где нужно сделать измениния', reply_markup=admin_panel_keyboard_category)


# Инстаграмм
@dp.message_handler(text='Instagram', user_id=config.admins)
async def admin_panel_instagram(message: types.Message):
    await message.answer('Что нужно?', reply_markup=admin_panel_keyboard)


# Добавления товара в инстаграме
@dp.message_handler(text='Добавить товар', user_id=config.admins)
async def admin_panel_instagram_append(message: types.Message):
    await message.answer('Введите названия товара')
    await AdminAppend.one.set()


@dp.message_handler(user_id=config.admins, state=AdminAppend.one)
async def title_append(message: types.Message, state: FSMContext):
    title_edit = message.text
    async with state.proxy() as data:
        data['title1'] = title_edit
    await message.answer('Введите описания товара')
    await AdminAppend.next()


@dp.message_handler(user_id=config.admins, state=AdminAppend.two)
async def description_append(message: types.Message, state: FSMContext):
    description_edit = message.text
    async with state.proxy() as data:
        data['description1'] = description_edit
    await message.answer('Введите цену товара')
    await AdminAppend.next()


@dp.message_handler(user_id=config.admins, state=AdminAppend.three)
async def price_append(message: types.Message, state: FSMContext):
    price_edit = message.text
    price_edit = str(price_edit)
    async with state.proxy() as data:
        data['price1'] = price_edit
    await message.answer('Введите id товара\n'
                         '<i>Начинайте от первого</i>')
    await AdminAppend.next()


@dp.message_handler(user_id=config.admins, state=AdminAppend.four)
async def id_append(message: types.Message, state: FSMContext):
    await message.answer('Отправьте ссылку на товар')
    id_edit = message.text
    id_edit = str(id_edit)
    async with state.proxy() as data:
        data['id1'] = id_edit
    await AdminAppend.next()


@dp.message_handler(user_id=config.admins, state=AdminAppend.five)
async def items_link(message: types.Message, state: FSMContext):
    link = message.text
    async with state.proxy() as data:
        data['link1'] = link
    await message.answer('Подтвердить создания нового товара?', reply_markup=admin_panel_confirm_keyboard)
    await AdminAppend.next()


@dp.message_handler(user_id=config.admins, text='Подтвердить', state=AdminAppend.six)
async def confirm(message: types.Message, state: FSMContext):
    await message.answer('Товар создан - /shop')
    data = await state.get_data()
    title = data.get('title1')
    description = data.get('description1')
    price = data.get('price1')
    id_id = data.get('id1')
    link = data.get('link')
    items.title_instagram.append(title)
    items.description_instagram.append(description)
    items.price_instagram.append(price)
    items.id_instagram.append(id_id)
    items.link_instagram.append(link)
    await state.finish()


There are lists that include data entered by the administrator to create a product
title_instagram = []
description_instagram = []
price_instagram = []
id_instagram = []
link_instagram = []

items_instagram = [title_instagram, description_instagram, price_instagram, id_instagram]

There is a script that displays the product created by the admin to the user
@dp.message_handler(Command('shop'))
async def show_shop(message: types.Message):
    await message.answer('<b>Страница магазина</b>', reply_markup=shop_keyboard)


@dp.callback_query_handler(text_contains='back')
async def show_shop_but_inline(call: types.CallbackQuery):
    await call.message.edit_reply_markup()
    await call.message.answer('<b>Страница магазина</b>', reply_markup=shop_keyboard)


@dp.callback_query_handler(text_contains='Instagram')
async def instagram(call: types.CallbackQuery):
    await call.message.edit_reply_markup()
    await call.answer(cache_time=60)
    caption = """
    {title}
    <pre>{description}</pre>
    {price}
    """
    for items in items_instagram:
        for title in items:
            for description in items:
                for price in items:
                    for id_id in items:
                        await call.message.answer(caption.format(title=title, description=description,
                                                                 price=price), reply_markup=keyboard(item_id=id_id))

Admin added one item, great! Everything works, the bot displays one message, in which everything is correctly indicated!
The admin added the second product, the bot displays 9-10 messages in which the data is also scattered
60f6fc2414950859081681.png
60f6fc3052a16612766488.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question