def create_product(): """ Creates a Product in the datbase from the posted database """ payload = request.get_json() product = Product() product.deserialize(payload) product.save() message = product.serialize() response = make_response(jsonify(message), HTTP_201_CREATED) response.headers['Location'] = url_for('get_products', id=product.id, _external=True) return response
def post(self): app.logger.info('Request to Create a Product') check_content_type('application/json') prod = Product() app.logger.info('Payload = %s', api.payload) mqtt.publish('myTopic', str(api.payload)) prod.deserialize(api.payload) location_url = api.url_for(ProductResource, products_id=prod.id, _external=True) return prod.serialize(), status.HTTP_201_CREATED, \ {'Location': location_url}
def put(self, products_id): """ Update a Product This endpoint will update a Product based the body that is posted """ app.logger.info('Request to Update a product with id [%s]', products_id) check_content_type('application/json') data = dict(id=products_id, payload=api.payload) mqtt.publish('myTopic', str(data)) product = Product() product.deserialize(data['payload']) product.id = products_id return product.serialize(), status.HTTP_200_OK
def post(self): """ Creates a Product This endpoint will create a Product based the data in the body that is posted """ app.logger.info('Request to Create a Product') check_content_type('application/json') prod = Product() app.logger.info('Payload = %s', api.payload) prod.deserialize(api.payload) prod.save() app.logger.info('Product with new id [%s] saved!', prod.id) location_url = api.url_for(ProductResource, products_id=prod.id, _external=True) return prod.serialize(), status.HTTP_201_CREATED, \ {'Location': location_url}
def test_deserialize_a_product(self): """ Test deserialization of a Product """ data = { "id": 1, "name": "Asus2500", "category": "Laptop", "price": "234", "description": "laptop", "color": "blue", 'count': 4 } products = Product() products.deserialize(data) self.assertNotEqual(products, None) self.assertEqual(products.id, 1) self.assertEqual(products.name, "Asus2500") self.assertEqual(products.category, "Laptop") self.assertEqual(products.price, "234") self.assertEqual(products.description, "laptop") self.assertEqual(products.color, "blue") self.assertEqual(products.count, 4)
def create(payload): app.logger.info('Payload = %s', payload) prod = Product() prod.deserialize(payload) prod.save() app.logger.info('Product with new id [%s] saved!', prod.id)