Пример #1
0
    def get(self):
        apiResponse = requests.get('http://sigmatest.sigmastorage.online/')
        productDic = productSchema.load(apiResponse.json())
        inquiredProduct = Product(productDic['id'], productDic['name'],
                                  productDic['image'], productDic['price'],
                                  productDic['tax'])
        oldProduct = Product.get_by_id(inquiredProduct.id)
        if (oldProduct):
            if (not validateProductEquals(inquiredProduct, oldProduct)):
                oldProduct.name = inquiredProduct.name
                oldProduct.image = inquiredProduct.image
                oldProduct.price = inquiredProduct.price
                oldProduct.tax = inquiredProduct.tax
                oldProduct.update()
        else:
            inquiredProduct.save()

        productResponse = ProductResponse(inquiredProduct.id,
                                          inquiredProduct.name,
                                          inquiredProduct.image,
                                          inquiredProduct.price,
                                          inquiredProduct.tax)
        productResponse.tax = calculateTax(inquiredProduct)
        productResponse.discount = calculateDiscount(inquiredProduct)
        return productSchema.dump(productResponse)
    def post(self):
        data = request.get_json()
        orderDict = orderSchema.load(data)
        product = Product.get_by_id((orderDict['product']['id']))
        order = Order(orderDict['productName'], orderDict['totalProduct'],
                      orderDict['totalCompra'], product)
        totalCalculado = calculateTotalCompra(product)

        if (totalCalculado != order.totalCompra):
            print(totalCalculado)
            print('error son diferentes')
        else:
            order.save()
        resp = orderSchema.dump(order)
        return resp, 201