def post(self): """ Creates a Product This endpoint will create a Product based the data in the body that is posted """ try: check_content_type('application/json') product = Product(1, "", "", "", 0, "", 0, "", 0) # product = Product() # app.logger.info((api.payload)) product.deserialize(api.payload) product.save() message = product.serialize() location_url = api.url_for(ProductCollection, item_id=product.id, _external=True) # return make_response(jsonify(message), status.HTTP_201_CREATED, # { # 'Location': location_url # }) return product.serialize(), status.HTTP_201_CREATED, { 'Location': location_url } except ValidationError: return request_validation_error('Invalid Data')
def create_products(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ check_content_type('application/json') product = Product(1, "", "", "", 0, "", 0, "", 0) product.deserialize(request.get_json()) product.save() message = product.serialize() location_url = url_for('list_products_by_id', item_id=product.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def test_serialize_a_product(self): """ Test serialization of a Product """ product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) data = product.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], 1) self.assertIn('name', data) self.assertEqual(data['name'], "Couch") self.assertIn('category', data) self.assertEqual(data['category'], "Furniture") self.assertIn('description', data) self.assertEqual(data['description'], "White couch") self.assertIn('price', data) self.assertEqual(data['price'], 200) self.assertIn('condition', data) self.assertEqual(data['condition'], "Boxed") self.assertIn('inventory', data) self.assertEqual(data['inventory'], 50) self.assertIn('rating', data) self.assertEqual(data['rating'], 8)