def create_products(order_id): """ Create an Product on an Order This endpoint will add an product to an order """ app.logger.info("Request to add an product to an order") # pylint: disable=maybe-no-member check_content_type("application/json") order = Order.find_or_404(order_id) product = Product() product.deserialize(request.get_json()) order.products.append(product) order.save() message = product.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED)
def create_products(supplier_id): """ Create a Product on a Supplier This endpoint will add a product to a supplier """ app.logger.info("Request to add an product to an supplier") check_content_type("application/json") supplier = Supplier.find_or_404(supplier_id) product = Product() product.deserialize(request.get_json()) supplier.products.append(product) supplier.save() message = product.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED)
def test_deserialize_a_product(self): """ Test deserialization of a Product """ data = { "id": 1, "name": "iPhone X", "description": "Black iPhone", "category": "Technology", "price": 999.99 } product = Product() product.deserialize(data) self.assertNotEqual(product, None) self.assertEqual(product.id, None) self.assertEqual(product.name, "iPhone X") self.assertEqual(product.description, "Black iPhone") self.assertEqual(product.category, "Technology") self.assertEqual(product.price, 999.99)
def create_product(): """ Creates a Product This endpoint will create a Product based on the data in the body that is posted """ app.logger.info("Request to create a product") check_content_type("application/json") product = Product() product.deserialize(request.get_json()) product.create() message = product.serialize() location_url = url_for("get_products", product_id=product.id, _external=True) #location_url = "not yet implemented - story #7" return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def post(self): """ Creates a Product This endpoint will create a Product based on the data in the body that is posted """ app.logger.info("Request to create a product") check_content_type("application/json") product = Product() product.deserialize(api.payload) if product.id == "" or product.name == "" or product.description == "" or product.price == "" or product.category == "": app.logger.info("Fields cannot be empty.") return api.abort(status.HTTP_400_BAD_REQUEST, "Fields cannot be empty.") product.create() app.logger.info("Product with id [%s] created.", product.id) location_url = api.url_for(ProductResource, product_id=product.id, _external=True) return product.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }