Exemplo n.º 1
0
def mrmnext(call: types.CallbackQuery):
    print("navigateto_adminusers_page")
    bot.answer_callback_query(call.id, '🖍')
    user_id = call.from_user.id
    msg_id = call.message.message_id
    call_splited = call.data.split('-')
    page_number = 1  # Start viewing items from page 1
    try:
        page_number_str = call_splited[1]
        page_number = int(page_number_str)
    except Exception as e:
        print("Cannot extract page number from {}. Error is {}".format(
            page_number_str, str(e)))

    print("navigateto_adminusers_page > Page number is {}".format(page_number))

    text = 'OK. Now select the user to set value of balance.\n\n'
    try:
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.manage_admins(
                                  user_id, page_number))
    except ApiException:
        pass
Exemplo n.º 2
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)
Exemplo n.º 3
0
def navigateto_banned_users_page(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('-')
    page_number = 1
    try:
        page_number_str = call_splited[1]
        page_number = int(page_number_str)
    except Exception as e:
        print("Cannot extract page number from {}. Error is {}".format(
            page_number_str, str(e)))

    print(
        "navigateto_banned_users_page > Page number is {}".format(page_number))
    # user = User.objects.get(user_id=call_splited[1])
    # if user.ban_status:
    #     user.ban_status = False
    # else:
    #     user.ban_status = True
    # user.save()
    text = 'OK.  Now select the user to ban or unban.\n\n'
    text += '❗ If user have 🖍 - user is banned ❗'
    try:
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.manage_banned_users(
                                  user_id, page_number))
    except ApiException:
        pass
Exemplo n.º 4
0
def manage_users_balance(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('-')
    user = User.objects.get(user_id=call_splited[1])
    bot.set_data({'user_to_set_balance': user.user_id}, user_id)
    bot.set_state(States.manage_users_balance.value, user_id)
    bot.edit_message_text(f'Send value to set new balance to {user.full_name}',
                          user_id, msg_id)
Exemplo n.º 5
0
def del_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 = Product.objects.get(id=call_splited[1])
    bot.edit_message_text(f'Are you sure want to delete {product.name}?',
                          user_id,
                          msg_id,
                          reply_markup=inline.sure_delete_product(product.id))
Exemplo n.º 6
0
def addcategorywithout_doc(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    bot.update_data({'new_category_file': None}, user_id)
    bot.set_state(States.new_category_only_one_user.value, user_id)
    bot.edit_message_text(
        'OK, now tell me. Can different users buy this product?',
        user_id,
        call.message.message_id,
        reply_markup=inline.category_only_one_user())
Exemplo n.º 7
0
def edit_text_shop_policy(call: types.CallbackQuery):
    bot.answer_callback_query(call.id, '⚠')
    user_id = call.from_user.id
    msg_id = call.message.message_id
    bot.edit_message_text(
        'OK. Now send new text for shop policy\n\n'
        '(After restarting the bot will be reset to default)',
        user_id,
        msg_id,
        reply_markup=inline.back_admin_edit_text())
    bot.set_state(States.edit_text_shop_policy.value, user_id)
Exemplo n.º 8
0
def select_category_parent(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('-')

    if call_splited[2] != 'None':
        bot.update_data({'parent_category_id': int(call_splited[2])}, user_id)
    else:
        bot.update_data({'parent_category_id': None}, user_id)

    bot.edit_message_text('OK. Now send price ($) for this category.', user_id,
                          msg_id)
    bot.set_state(States.new_category_price.value, user_id)
Exemplo n.º 9
0
def admin____handler(call: types.CallbackQuery):
    bot.answer_callback_query(call.id)
    user_id = call.from_user.id
    if not User.objects.get(user_id=user_id).admin:
        bot.send_message(user_id,
                         'YOU ARE NOT AN ADMIN!!!',
                         reply_markup=reply.main_menu())
    if call.data == 'admin_add_category':
        bot.set_state(States.new_category_name.value, user_id)
        bot.send_message(user_id, 'OK. Send me the name for new category.')
    if call.data == 'admin_add_product':
        bot.set_state(States.new_product_name.value, user_id)
        bot.send_message(user_id, 'OK. Send me the name for new product.')
    if call.data == 'admin_del_category':
        bot.send_message(user_id,
                         'OK. Now select the category for delete.',
                         reply_markup=inline.del_category())
    if call.data == 'admin_del_product':
        bot.send_message(user_id,
                         'OK. Now select the product for delete.',
                         reply_markup=inline.del_product())
    if call.data == 'admin_manage_admins':
        text = 'OK.  Now select the user to grant or remove administrator rights.\n\n'
        text += '❗ If user have ✅ - user is admin ❗'
        bot.send_message(user_id,
                         text,
                         reply_markup=inline.manage_admins(user_id))

    text = 'OK.  Now select the menu where you want to change the text'
    if call.data == 'admin_edit_text':
        text = 'OK.  Now select the menu where you want to change the text'
        bot.send_message(user_id, text, reply_markup=inline.edit_text())
    elif call.data == 'back_admin_edit_text':
        bot.finish_user(user_id)
        bot.edit_message_text(text,
                              user_id,
                              call.message.message_id,
                              reply_markup=inline.edit_text())

    if call.data == 'admin_manage_banned_users':
        text = 'OK.  Now select the user to ban or unban.\n\n'
        text += '❗ If user have 🖍 - user is banned ❗'
        bot.send_message(user_id,
                         text,
                         reply_markup=inline.manage_banned_users(user_id))
    if call.data == 'admin_manage_users_balance':
        text = 'OK. Now select the user to set value of balance.'
        bot.send_message(user_id,
                         text,
                         reply_markup=inline.manage_users_balance(user_id))
Exemplo n.º 10
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))
Exemplo n.º 11
0
def manage_banned_users(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('-')
    user = User.objects.get(user_id=call_splited[1])
    if user.ban_status:
        user.ban_status = False
    else:
        user.ban_status = True
    user.save()
    text = 'OK.  Now select the user to ban or unban.\n\n'
    text += '❗ If user have 🖍 - user is banned ❗'
    try:
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.manage_banned_users(user_id))
    except ApiException:
        pass
Exemplo n.º 12
0
def pagination_orders(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('-')
    page = call_splited[1]
    user = User.objects.get(user_id=user_id)
    user_orders = Order.objects.filter(user=user).order_by('id')
    paginator = Paginator(user_orders, 1)
    text = '💵 Here you can view your recent purchases:\n\n'
    page_orders = paginator.page(page)
    for order in page_orders:
        text += f'Product id: {order.product.id}\n'
        text += f'Product name: {order.product.name}\n'
        text += f'Product data:\n{order.product.category.data}\n\n'
        text += f'Page: {page} / {paginator.num_pages}'
    bot.edit_message_text(text,
                          user_id,
                          msg_id,
                          reply_markup=inline.pagination_orders(
                              paginator, page))
Exemplo n.º 13
0
def 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 user.balance < product_to_buy.category.price:
        text = '🛒 STORE\n\n🚫 You do not have enough money'
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.get_back_category(
                                  product_to_buy.category))
    elif len(
            Order.objects.filter(user=user,
                                 user__order__product=product_to_buy.id)) >= 1:
        bot.edit_message_text(
            '🛒 STORE\n\n✅ Have you already purchased this product!',
            user_id,
            msg_id,
            reply_markup=inline.get_back_category(product_to_buy.category))
    else:
        bot.edit_message_text('OK. Are you sure what want buy this product?',
                              user_id,
                              msg_id,
                              reply_markup=inline.sure_buy_product(
                                  product_to_buy.id, product_to_buy.category))
Exemplo n.º 14
0
def manage_admins(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('-')
    user = User.objects.get(user_id=call_splited[1])
    text = 'OK.  Now select the user to grant or remove administrator rights.\n\n'
    text += '❗ If user have ✅ - user is admin ❗'
    if user.admin:
        user.admin = False
        user.save()
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.manage_admins(user_id))
    else:
        user.admin = True
        user.save()
        bot.edit_message_text(text,
                              user_id,
                              msg_id,
                              reply_markup=inline.manage_admins(user_id))
Exemplo n.º 15
0
def sure_delete_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('-')
    if call_splited[0] == 'yes_delete_product':
        product_to_delete = Product.objects.get(id=call_splited[1])
        product_to_delete.delete()
        bot.edit_message_text(
            f'Product "{product_to_delete.name}" has been deleted!', user_id,
            msg_id)
    if call_splited[0] == 'no_delete_product':
        if not check_admin(user_id):
            bot.edit_message_text(user_id,
                                  '❗ YOU ARE NOT AN ADMIN ❗',
                                  user_id,
                                  msg_id,
                                  reply_markup=reply.main_menu())
        else:
            bot.edit_message_text('👨‍💻 ADMIN MENU ⚙',
                                  user_id,
                                  msg_id,
                                  reply_markup=inline.admin_menu())