Exemple #1
0
def go_back(call):
    obj_id = call.data.split('_')[1]
    category = models.Category.objects(id=obj_id).get()

    if category.is_root:
        kb = keyboards.InlineKB(key='root',
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()

    else:
        kb = keyboards.InlineKB(
            iterable=category.parent.subcategory,
            lookup_field='id',
            named_arg='category',
        )
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<< {category.parent.title}',
                                 callback_data=f'back_{category.parent.id}'))

    text = 'Категории' if not category.parent else category.parent.title
    bot.edit_message_text(text=text,
                          chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          reply_markup=kb)
Exemple #2
0
def go_back(call):
    '''

    :param call:
    :return: go back
    '''
    obj_id = call.data.split('_')[1]
    category = Category.objects(id=obj_id).get()
    if category.is_root:

        kb = keyboards.InlineKB(key='root',
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()
        kb.add(InlineKeyboardButton(text='<<', callback_data='home_/start'))
    else:

        kb = keyboards.InlineKB(iterable=category.parent.subcategory,
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<<',
                                 callback_data=f'back_{category.parent.id}'))
    text = 'Выберите категорию' if not category.parent else category.parent.title
    try:
        bot.edit_message_text(text=text,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
    except:
        bot.delete_message(chat_id=call.message.chat.id,
                           message_id=call.message.message_id)
Exemple #3
0
def show_products_or_sub_category(call):
    '''

    :param call:
    :return: listed subcategory || listed products
    '''
    obj_id = call.data.split('_')[1]
    category = Category.objects(id=obj_id).get()
    if category.is_parent:
        kb = keyboards.InlineKB(iterable=category.subcategory,
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text='<<',
                                 callback_data=f'back_{category.id}'))
        bot.edit_message_text(text=category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
    else:
        product = Product.objects(category=obj_id, is_discount=False).all()
        kb = keyboards.InlineKB(iterable=product,
                                lookup_field='id',
                                named_arg='product')
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<<',
                                 callback_data=f'back_{category.id}'))
        bot.edit_message_text(text=category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
Exemple #4
0
def show_categoryies(message):
    kb = keyboards.InlineKB(key='root',
                            lookup_fields='id',
                            named_arg='category')
    bot.send_message(message.chat.id,
                     message.text,
                     reply_markup=kb.generate_kb())
Exemple #5
0
def show_products_or_sub_category(call):
    """
    :param call:
    :return: listed subcategories || listed products
    """
    obj_id = call.data.split('_')[1]
    category = models.Category.objects(id=obj_id).get()

    if category.is_parent:
        kb = keyboards.InlineKB(iterable=category.subcategory,
                                lookup_field='id',
                                named_arg='category')

        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'back',
                                 callback_data=f'back_{category.id}'))

        bot.edit_message_text(text=category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
    # bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=kb.generate_kb())

    else:
        print("NON PARENT")
Exemple #6
0
def show_products_or_sub_category(call):
    """
    :param call
    :return listed
    """

    obj_id = call.data.split('_')[1]
    category = models.Category.objects(id=obj_id).get()

    if category.is_parent:

        kb = keyboards.InlineKB(iterable=category.subcategory,
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<< {category.title}',
                                 callback_data=f'back_{category.id}'))

        bot.delete_message(chat_id=call.message.chat.id,
                           message_id=call.message.message_id)
        bot.send_message(text=category.title,
                         chat_id=call.message.chat.id,
                         reply_markup=kb)
        # bot.edit_message_text(text=category.title, chat_id=call.message.chat.id,
        #                     message_id=call.message.message_id,
        #                     reply_markup=kb)

    else:
        print('NON PARANT')
        if call.data.split('_')[1]:
            products = models.Product.objects(category=call.data.split('_')[1])
            keyboard = InlineKeyboardMarkup(row_width=1)
            prods = [
                InlineKeyboardButton(
                    text=i.title,
                    callback_data=f'show product_{i.id}_{category.id}')
                for i in products
            ]
            back = InlineKeyboardButton(text=f'<< back',
                                        callback_data=f'back_{category.id}')
            if len(prods) == 0:
                text = 'Empty'
            else:
                text = category.title
            keyboard.add(*prods)
            keyboard.add(back)

            bot.delete_message(chat_id=call.message.chat.id,
                               message_id=call.message.message_id)

            bot.send_message(text=text,
                             chat_id=call.message.chat.id,
                             reply_markup=keyboard)
Exemple #7
0
def show_categories(message):
    """
    :param message:
    :return: listed root categories
    """
    kb = keyboards.InlineKB(key='root',
                            lookup_field='id',
                            named_arg='category')
    bot.send_message(message.chat.id,
                     "Choose category",
                     reply_markup=kb.generate_kb())
Exemple #8
0
def show_categories(call):
    '''

    :param message:
    :return: listed root category
    '''
    kb = keyboards.InlineKB(key='root',
                            lookup_field='id',
                            named_arg='category')
    kb.generate_kb()
    kb.add(InlineKeyboardButton(text='<<', callback_data='home_/start'))
    bot.edit_message_text(text='Выберите категорию',
                          chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          reply_markup=kb)
Exemple #9
0
def show_products_or_sub_category(call):
    """

    :param call:
    :return: listed subcategories || listed products
    """
    obj_id = call.data.split('_')[1]
    category = models.Category.objects(id=obj_id).get()

    if category.is_parent:

        kb = keyboards.InlineKB(iterable=category.subcategory,
                                lookup_field='id',
                                named_arg='category')
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<< {category.title}',
                                 callback_data=f'back_{category.id}'))

        bot.edit_message_text(text=category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)

    else:
        products = category.get_products()
        for product in products:
            kb = InlineKeyboardMarkup()
            kb.add(
                telebot.types.InlineKeyboardButton(text='Добавить в корзину',
                                                   callback_data='cart_' +
                                                   str(product.id)))

            bot.send_message(call.message.chat.id,
                             parse_mode='HTML',
                             text=f'Название товара: {product.title}')
            bot.send_photo(
                call.message.chat.id,
                product.p_img.read(),
                parse_mode='HTML',
                caption=
                f'Описание: {product.description}\nЦена: {product.get_price}',
                reply_markup=kb)
Exemple #10
0
def show_sales_product(call):
    '''

    :param call:
    :return: discount products
    '''
    product = Product.objects(is_discount=True).all()
    kb = keyboards.InlineKB(iterable=product,
                            lookup_field='id',
                            named_arg='product')
    kb.generate_kb()
    kb.add(InlineKeyboardButton(text=f'<<', callback_data=f'home_0'))
    try:
        bot.edit_message_text(text='Товары со скидкой',
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
    except:
        bot.delete_message(chat_id=call.message.chat.id,
                           message_id=call.message.message_id)
        bot.send_message(text='Товары со скидкой',
                         chat_id=call.message.chat.id,
                         reply_markup=kb)
Exemple #11
0
def show_product_or_sub_category(call):
    obj_id = call.data.split('_')[1]
    category = models.Category.objects(id=obj_id).get()
    if category.is_parent:
        kb = keyboards.InlineKB(
            iterable=category.subcategory,
            lookup_fields='id',
            named_arg='category',
        )
        kb.generate_kb()
        kb.add(
            InlineKeyboardButton(text=f'<<',
                                 callback_data=f'back_{category.id}'))
        bot.edit_message_text(text=category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)
    else:
        products = Product.objects(category=obj_id)
        for product in products:
            photo = product.photo_product.read()
            keyboard = InlineKeyboardMarkup(row_width=2)
            buttons = [
                InlineKeyboardButton(f'{i}',
                                     callback_data=f'{key}_{product.id}')
                for key, i in keyboards.product_kb.items()
            ]
            keyboard.add(*buttons)
            bot.send_photo(
                chat_id=call.message.chat.id,
                photo=photo,
                caption=
                f'<b>{product.title}</b>  Цена: <i> {product.get_price} 💵 </i> '
                f' <code> {product.description} </code>',
                reply_markup=keyboard,
                parse_mode='HTML')