def category_handler(call): category_id = '_'.join(call.data.split('_')[1::]) category = Category.objects.get(id=category_id) kb = InlineKeyboardMarkup(row_width=2) if category.subcategories: buttons = [ InlineKeyboardButton(text=category.title, callback_data=f'category_{category.id}') for category in category.subcategories ] kb.add(*buttons) bot.edit_message_text(category.title, chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=kb) elif category.is_leaf: for product in category.products: button = InlineKeyboardButton( text='Добавить в корзину', callback_data=f'product_{product.id}') kb.keyboard = [[button.to_dic()]] bot.send_photo(call.message.chat.id, product.image.read(), caption=product.description, disable_notification=True, reply_markup=kb)
def view_basket(self, message, basket): amount_of_items = 0 total_price = 0 kb = InlineKeyboardMarkup(row_width=2) kb_order = InlineKeyboardMarkup(row_width=2) button_order = [ InlineKeyboardButton(text="Order", callback_data=f"order_{basket}") ] kb_order.add(*button_order) if basket.amount_items() == 0: self.send_message(message.chat.id, text="Your basket is empty") else: for cart_item in basket.cart_items: amount_of_items += cart_item.quantity total_price += cart_item.price button = InlineKeyboardButton( text="Remove", callback_data=f"remove_{cart_item.product.id}") kb.keyboard = [[button.to_dic()]] if cart_item.product.image: self.send_photo( message.chat.id, cart_item.product.image.read(), caption=f"title : {cart_item.product.title}\n" f"description : {cart_item.product.description}\n" f"price : {cart_item.product.price}\n", disable_notification=True, reply_markup=kb) else: self.send_message( message.chat.id, text=f"title : {cart_item.product.title}\n" f"description : {cart_item.product.description}\n" f"price : {cart_item.product.price}\n", reply_markup=kb) self.send_message( message.chat.id, f"Total items in your basket is: {amount_of_items}\n" f"Total price is: {total_price}\n", reply_markup=kb_order, )
def send_products(self, call_data, category): kb = InlineKeyboardMarkup(row_width=2) for product in category.products: button = InlineKeyboardButton( text="To basket", callback_data=f"product_{product.id}") kb.keyboard = [[button.to_dic()]] if product.image: self.send_photo(call_data.message.chat.id, product.image.read(), caption=f"title : {product.title}\n" f"description : {product.description}\n" f"price : {product.price}\n", disable_notification=True, reply_markup=kb) else: self.send_message(call_data.message.chat.id, text=f"title : {product.title}\n" f"description : {product.description}\n" f"price : {product.price}\n", reply_markup=kb)