Exemple #1
0
def category_handler(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    msg_id = call.message.message_id
    category = Category.objects.get(id=call.data.split('_')[1])
    products = Product.objects.filter(category=category)
    childs = Category.objects.filter(parent=category)
    text = f'🛒 STORE\n\n{gen_bread_crumb(category)}'
    some_exists = True
    if products.exists() and childs.exists():
        markup = inline.get_products_categories(products,
                                                childs,
                                                prev_category=category.parent)
    elif products.exists():
        markup = inline.get_products(products, prev_category=category.parent)
    elif childs.exists():
        markup = inline.get_categories(childs, prev_category=category.parent)
    else:
        some_exists = False
        markup = inline.get_back_category(category.parent)
    if some_exists:
        try:
            bot.edit_message_text(text, user_id, msg_id, reply_markup=markup)
        except Exception:
            bot.delete_message(user_id, msg_id)
            bot.send_message(user_id, text, reply_markup=markup)
    else:
        text += '\n\n🚫 There is no products'
        try:
            bot.edit_message_text(text, user_id, msg_id, reply_markup=markup)
        except Exception:
            bot.delete_message(user_id, msg_id)
            bot.send_message(user_id, text, reply_markup=markup)
Exemple #2
0
def select_product_category(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    msg_id = call.message.message_id
    call_splited = call.data.split('-')
    category_to_chose = Category.objects.get(id=call_splited[1])

    bot.update_data({'new_product_category': category_to_chose.id}, user_id)
    # bot.edit_message_text('OK. Now send price ($) for this product.', user_id, msg_id)
    # bot.set_state(States.new_product_price.value, user_id)

    data = bot.get_data(user_id)
    # category = Category.objects.get(id=data['new_product_category'])
    product_schema = {
        'name': data['new_product_name'],
        'category': category_to_chose,
        # 'price': data['new_product_price'],
        # 'data': data['new_product_data'],
        # 'doc': data.get('new_product_file')
    }
    # if call.data == 'product_for_different_users':
    #     product_schema['for_one_user'] = False
    # elif call.data == 'product_for_one_user':
    #     product_schema['for_one_user'] = True
    Product.objects.create(**product_schema)
    bot.delete_message(user_id, call.message.message_id)
    bot.send_message(user_id, 'OK! Product successfully added.')
    bot.finish_user(user_id)
Exemple #3
0
def new_category_only_one_user(call: types.CallbackQuery):
    user_id = call.from_user.id
    data = bot.get_data(user_id)
    print("Collected data : ", data)

    if data['parent_category_id'] == None:
        parent_category = None
    else:
        parent_category = Category.objects.get(id=data['parent_category_id'])

    category_schema = {
        'name': data['new_category_name'],
        'parent': parent_category,
        'price': data['new_category_price'],
        'data': data['new_category_data'],
        'doc': data.get('new_category_file')
    }

    if call.data == 'categoryfor_different_users':
        category_schema['for_one_user'] = False
    elif call.data == 'categoryfor_one_user':
        category_schema['for_one_user'] = True

    Category.objects.create(**category_schema)
    bot.delete_message(user_id, call.message.message_id)
    bot.send_message(user_id, 'OK! Category successfully added.')
    bot.finish_user(user_id)
Exemple #4
0
def to_shop(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    msg_id = call.message.message_id
    text = '🛒 STORE'
    categories = Category.objects.filter(parent=None)
    try:
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.get_categories(
                                  categories, to_start=True))
    except Exception:
        bot.delete_message(user_id, msg_id)
        bot.send_message(user_id,
                         text,
                         reply_markup=inline.get_categories(categories,
                                                            to_start=True))
Exemple #5
0
def sure_buy_product(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    msg_id = call.message.message_id
    call_splited = call.data.split('-')
    product_to_buy = Product.objects.get(id=call_splited[1])
    user = User.objects.get(user_id=user_id)
    if call_splited[0] == 'yes_sure_product':
        new_purchase = Order.objects.create(user=user, product=product_to_buy)
        user.balance -= product_to_buy.category.price
        user.save()
        text = f'✅ You have successfully bought {product_to_buy.name}\n\n' \
               f'Product data:\n{product_to_buy.category.data}\n\nYou can view it again in the order history.'
        bot.delete_message(user_id, msg_id)
        if product_to_buy.category.doc:
            bot.send_message(user_id,
                             text,
                             reply_markup=inline.get_product_doc(
                                 product_to_buy.id))
        else:
            bot.send_message(user_id, text)