def put(self, p_id): obj = Product.objects(id=p_id).get() if not request.json: if request.files: obj.update(**{'img': request.files['img'].read()}) data = json.loads(request.form['request']) if data.get('category'): category = data.pop('category') data['category'] = Category.objects(id=(category['id'])).get() obj.update(**data) else: if request.json.get('category'): category = request.json.pop('category') request.json['category'] = Category.objects( id=(category['id'])).get() obj.update(**request.json) return ProductScheme().dump(obj.reload())
def inline(message): print('sales') products = Product.objects(is_discount=True) for product in products: keyboard = InlineKeyboardMarkup(row_width=2) buttons = [ InlineKeyboardButton(f'{i}', callback_data=f'{key}_{product.id}') for key, i in keyboards.product_kb.items() ] keyboard.add(*buttons) bot.send_message(message.chat.id, text=f'{product.title} Цена: {product.get_price} 💵', reply_markup=keyboard)
def show_product_or_sub_category(call): obj_id = call.data.split('_')[1] category = models.Category.objects(id=obj_id).get() if category.is_parent: kb = keyboards.InlineKB( iterable=category.subcategory, lookup_fields='id', named_arg='category', ) kb.generate_kb() kb.add( InlineKeyboardButton(text=f'<<', callback_data=f'back_{category.id}')) bot.edit_message_text(text=category.title, chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=kb) else: products = Product.objects(category=obj_id) for product in products: photo = product.photo_product.read() keyboard = InlineKeyboardMarkup(row_width=2) buttons = [ InlineKeyboardButton(f'{i}', callback_data=f'{key}_{product.id}') for key, i in keyboards.product_kb.items() ] keyboard.add(*buttons) bot.send_photo( chat_id=call.message.chat.id, photo=photo, caption= f'<b>{product.title}</b> Цена: <i> {product.get_price} 💵 </i> ' f' <code> {product.description} </code>', reply_markup=keyboard, parse_mode='HTML')
def get(self, p_id=None): if p_id: return ProductScheme().dump(Product.objects(id=p_id).get()) return ProductScheme().dump(Product.objects, many=True)
def delete(self, p_id): obj = Product.objects(id=p_id).get() obj.delete() return {p_id: 'DELETED'}
def delete(self, id): ProductSchema().dump(Product.objects(id=id).delete()) return jsonify(**{id: 'deleted'})
def put(self, id): obj = Product.objects(id=id).get() obj.update(**request.json) return ProductSchema().dump(obj.reload())
def get(self, id=None): if not id: return ProductSchema().dump(Product.objects, many=True) return ProductSchema().dump(Product.objects(id=id).get())