Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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')
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def delete(self, p_id):
        obj = Product.objects(id=p_id).get()
        obj.delete()

        return {p_id: 'DELETED'}
Ejemplo n.º 6
0
 def delete(self, id):
     ProductSchema().dump(Product.objects(id=id).delete())
     return jsonify(**{id: 'deleted'})
Ejemplo n.º 7
0
 def put(self, id):
     obj = Product.objects(id=id).get()
     obj.update(**request.json)
     return ProductSchema().dump(obj.reload())
Ejemplo n.º 8
0
 def get(self, id=None):
     if not id:
         return ProductSchema().dump(Product.objects, many=True)
     return ProductSchema().dump(Product.objects(id=id).get())