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
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)
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
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)
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))
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())
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)
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)
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))
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))
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
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))
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))
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))
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())