def create_order(): """ Creates an Order object based on the JSON posted """ check_content_type('application/json') order = Order() json_post = request.get_json() order.deserialize(json_post) order.save() message = order.serialize() current_order_id = message['id'] # Want to get the items from POST and create items associated with order items = json_post['items'] items_response = [] for item_dict in items: item = Item() item.deserialize(item_dict, current_order_id) item.save() items_response.append(item.serialize()) message['items'] = items_response # location_url = url_for('get_orders', order_id=order.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, { # 'Location': location_url })
def test_invalid_key_raises_error(self): """ Try to pass invalid key """ data = {"id": 1, "product_id": 1, "quantity": 1, "price": 10.50} order_id = 1 with self.assertRaises(DataValidationError): item = Item() item.deserialize(data, order_id)
def test_non_dict_raises_error(self): """ Pass invalid data structure deserialize """ data = [1, 2, 3] item = Item() order_id = 1 with self.assertRaises(DataValidationError): item.deserialize(data, order_id)
def add_item_to_wishlist(wishlist_id): """ Add an Item to an existing wishlist This endpoint will add a wishlist item based on the data in the body that is posted --- tags: - Wishlist consumes: application/json parameters: - name: wishlist_id in: path type: integer description: the id of the Wishlist to add an item required: true - name: body in: body required: true schema: $ref: '#/definitions/Item' responses: 201: description: Successfully added Item to wishlist 404: description: Wishlist with id not found """ check_content_type('application/json') wishlist = Wishlist.get(wishlist_id) if not wishlist: raise NotFound("Wishlist with id '{}' was not found.".format(wishlist_id)) item = Item() json_post = request.get_json() item.deserialize(json_post,wishlist_id) item.save() message = item.serialize() """ check if the item.wishlist_id equals to the wishlist.id """ check_wishlist_id = item.wishlist_id location_url = url_for('get_wishlist', wishlist_id=wishlist.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, { 'Location': location_url })
def test_deserialize_an_item(self): """ Test deserialization of an Item """ data = { "id": 1, "product_id": 1, "name": "wrench", "quantity": 1, "price": 10.50 } order_id = 1 item = Item() item.deserialize(data, order_id) self.assertNotEqual(item, None) self.assertEqual(item.id, None) self.assertEqual(item.order_id, 1) self.assertEqual(item.product_id, 1) self.assertEqual(item.name, "wrench") self.assertEqual(item.quantity, 1) self.assertEqual(item.price, 10.50)