예제 #1
0
    def post(self):
        category = request.json.pop('category')
        request.json['category'] = Category.objects(id=(category['id'])).get()
        obj = Product(**request.json)
        obj.save()

        return ProductScheme().dump(obj)
예제 #2
0
    def get(self):

        pipeline = [{
            '$group': {
                '_id': '$title',
                'TotalSum': {
                    '$sum': {
                        '$multiply': ['$price', '$amount']
                    }
                }
            }
        }]

        data = Product.objects().aggregate(*pipeline)

        all_sum = {'total_sum': 0}

        data_to_json = list(data)

        for i in data_to_json:
            for v in i.values():

                if type(v) == int:
                    all_sum['total_sum'] += v

        return jsonify(data_to_json + [all_sum])
예제 #3
0
    def get(self, c_id=None):
        if c_id:
            category = Category.objects(id=c_id).get()
            obj = Product.objects(category=category)
            return ProductScheme().dump(obj, many=True)

        return CategoryScheme().dump(Category.objects, many=True)
예제 #4
0
def seeder(amount):

    data = {
        'food': ['Apple', 'Meat', 'IceCream'],
        'drinks': ['Cola', 'Fanta', 'Sprite'],
        'clothes': ['Backpack', 'Jacket', 'Shoes'],
    }

    if len(Category.objects.to_json()) == 2:

        for i in data.keys():

            category_dict = {'title': i, 'description': lorem.sentence()}

            Category(**category_dict).save()

    for _ in range(amount):

        title = choice(list(data.keys()))
        amount = randint(0, 100)

        product_dict = {
            'title': choice(data[title]),
            'price': randint(1, 100),
            'accessibility': bool(amount),
            'amount': amount,
            'category': Category.objects(title=title).get()
        }

        Product(**product_dict).save()
예제 #5
0
    def get(self, p_id=None):
        if p_id:
            Product.objects(id=p_id).update(inc__views=1)
            return ProductScheme().dump(Product.objects(id=p_id).get())

        return ProductScheme().dump(Product.objects, many=True)
예제 #6
0
    def delete(self, p_id):
        obj = Product.objects(id=p_id).get()
        obj.delete()

        return {p_id: 'DELETED'}
예제 #7
0
    def put(self, p_id):
        obj = Product.objects(id=p_id).get()
        obj.update(**request.json)

        return ProductScheme().dump(obj.reload())