def create_wishlists(): """ Creates a Wishlist This endpoint will create a Wishlist based the data in the body that is posted """ app.logger.info('Request to Create a Wishlist...') data = {} # Check for form submission data if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded': app.logger.info('Getting data from form submit') data = { 'name': request.form['name'], 'customer_id': request.form['customer_id'] } else: check_content_type('application/json') app.logger.info('Getting json data from API call') data = request.get_json() app.logger.info(data) wishlist = Wishlist() wishlist.deserialize(data) wishlist.save() app.logger.info('Wishlist with new id [%s] saved!', wishlist.id) message = wishlist.serialize() location_url = url_for('get_wishlists', wishlist_id=wishlist.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def test_serialize_a_wishlist(self): """ Serialize a Wishlist """ wishlist = Wishlist("fido", "1") data = wishlist.serialize() self.assertNotEqual(data, None) self.assertNotIn('_id', data) self.assertIn('name', data) self.assertEqual(data['name'], "fido") self.assertIn('customer_id', data) self.assertEqual(data['customer_id'], "1")
def test_serialize_a_wishlist(self): """ Test serialization of a Wishlist """ wishlist = Wishlist("mike's wishlist", "mike", [Wishlist_entry(0, "car")]) data = wishlist.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('name', data) self.assertEqual(data['name'], "mike's wishlist") self.assertIn('user', data) self.assertEqual(data['user'], "mike")
def post(self): """ Creates a Wishlist This endpoint will create a Wishlist based the data in the body that is posted """ app.logger.info('Request to Create a wishlist') check_content_type('application/json') wishlist = Wishlist() app.logger.info('Payload = %s', api.payload) wishlist.deserialize(api.payload) wishlist.save() app.logger.info('Wishlist with new id [%s] saved', wishlist.id) location_url = api.url_for(WishlistResource, wishlist_id=wishlist.id, _external=True) return wishlist.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }