Пример #1
0
def count_callback_query(call):
    def _total_cart_sum(cart) -> int:
        summary_dishes_sum = [
            cart_item.dish.price * cart_item.count for cart_item in cart
        ]
        total = sum(summary_dishes_sum)
        return total

    chat_id = call.message.chat.id
    user_id = call.from_user.id
    language = userservice.get_user_language(user_id)
    bot.answer_callback_query(call.id)
    bot.clear_step_handler_by_chat_id(chat_id)
    selected_number = int(call.data[6:])
    current_dish = userservice.get_current_user_dish(user_id)
    dish_to_check = Dish.query.get(current_dish.id)
    if selected_number > dish_to_check.quantity:
        not_enough_count = strings.get_string(
            'not_enough_count', language).format(dish_to_check.quantity)
        msg = bot.send_message(chat_id, text=not_enough_count)
        bot.register_next_step_handler(msg, dish_action_processor)
    else:
        userservice.add_dish_to_cart(user_id, current_dish, selected_number)
        cart = userservice.get_user_cart(user_id)
        total = _total_cart_sum(cart)
        bot.delete_message(chat_id, call.message.message_id)
        cart_contains_message = strings.from_cart_items(cart, language, total)
        continue_message = strings.get_string(
            'catalog.continue', language).format(cart_contains_message)
        back_to_the_catalog(chat_id, language, continue_message)
        catalog_message = strings.get_string('catalog.start', language)
        bot.send_message(chat_id, catalog_message, parse_mode='HTML')
Пример #2
0
def cart_processor(message: Message, callback=None):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    cart = userservice.get_user_cart(user_id)
    if len(cart) == 0:
        cart_empty_message = strings.get_string('cart.empty', language)
        bot.send_message(chat_id, cart_empty_message)
        if callback:
            bot.register_next_step_handler_by_chat_id(chat_id, callback)
        else:
            bot.register_next_step_handler_by_chat_id(chat_id,
                                                      catalog_processor)
        return
    cart_help_message = strings.get_string('cart.help', language)
    total = _total_cart_sum(cart)
    cart_contains_message = strings.from_cart_items(cart, language, total)
    cart_items_keyboard = keyboards.from_cart_items(cart, language)
    bot.send_message(chat_id, cart_help_message, parse_mode='HTML')
    bot.send_message(chat_id,
                     cart_contains_message,
                     parse_mode='HTML',
                     reply_markup=cart_items_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, cart_action_processor)
Пример #3
0
def _to_the_address(chat_id, language):
    cart = userservice.get_user_cart(chat_id)
    total = _total_cart_sum(cart)
    cart_contains_message = strings.from_cart_items(cart, language, total)
    address_message = strings.get_string('order.address', language).format(cart_contains_message)
    address_keyboard = keyboards.get_keyboard('order.address', language)
    bot.send_message(chat_id, address_message, parse_mode='HTML', reply_markup=address_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, address_processor)
Пример #4
0
def cart_action_processor(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        if message.text == '/start':
            registration.welcome(message)
            return
        error_msg = strings.get_string('cart.error', language)
        bot.send_message(chat_id, error_msg)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  cart_action_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        back_to_the_catalog(chat_id, language)
    elif strings.get_string('cart.clear', language) in message.text:
        userservice.clear_user_cart(user_id)
        back_to_the_catalog(chat_id, language)
    elif strings.get_string('catalog.make_order', language) in message.text:
        order_processor(message)
    else:
        dish_name = message.text[2:]
        removing_result = userservice.remove_dish_from_user_cart(
            user_id, dish_name, language)
        if removing_result:
            cart = userservice.get_user_cart(user_id)
            if len(cart) == 0:
                back_to_the_catalog(chat_id, language)
                return
            total = _total_cart_sum(cart)
            cart_contains_message = strings.from_cart_items(
                cart, language, total)
            cart_contains_keyboard = keyboards.from_cart_items(cart, language)
            bot.send_message(chat_id,
                             cart_contains_message,
                             parse_mode='HTML',
                             reply_markup=cart_contains_keyboard)
            bot.register_next_step_handler_by_chat_id(chat_id,
                                                      cart_action_processor)
            return
        else:
            error()
            return
Пример #5
0
def dish_action_processor(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def _total_cart_sum(cart) -> int:
        summary_dishes_sum = [
            cart_item.dish.price * cart_item.count for cart_item in cart
        ]
        total = sum(summary_dishes_sum)
        return total

    def error():
        error_message = strings.get_string('catalog.dish_action_error',
                                           language)
        bot.send_message(chat_id, error_message)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  dish_action_processor)

    if not message.text:
        error()
        return
    current_dish = userservice.get_current_user_dish(user_id)
    if strings.get_string('go_back', language) in message.text:
        dishes = dishservice.get_dishes_from_category(current_dish.category,
                                                      sort_by_number=True)
        dish_message = strings.get_string('catalog.choose_dish', language)
        dishes_keyboard = keyboards.from_dishes(dishes, language)
        bot.send_message(chat_id, dish_message, reply_markup=dishes_keyboard)
        bot.register_next_step_handler_by_chat_id(
            chat_id, choose_dish_processor, category=current_dish.category)

    elif strings.get_string('go_to_menu', language) in message.text:
        botutils.to_main_menu(chat_id, language)  ##MENU

    elif strings.get_string('catalog.cart', language) in message.text:
        user_cart.cart_processor(message, dish_action_processor)
    else:
        if not message.text.isdigit():
            error()
            return
        # Проверка на количество товара в базе.
        selected_number = int(message.text)
        dish_to_check = Dish.query.get(current_dish.id)

        if selected_number > dish_to_check.quantity:
            not_enough_count = strings.get_string(
                'not_enough_count', language).format(dish_to_check.quantity)
            msg = bot.send_message(chat_id, text=not_enough_count)
            bot.register_next_step_handler(msg, dish_action_processor)

        else:
            userservice.add_dish_to_cart(user_id, current_dish,
                                         int(message.text))
            cart = userservice.get_user_cart(user_id)
            total = _total_cart_sum(cart)
            cart_contains_message = strings.from_cart_items(
                cart, language, total)
            continue_message = strings.get_string(
                'catalog.continue', language).format(cart_contains_message)

            back_to_the_catalog(chat_id, language, continue_message)