示例#1
0
def create_items():
    """
    Creates a Item
    This endpoint will create a Item based the data in the body that is posted
    """
    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'],
            'price': request.form['price'],
            'available': True
        }
    else:
        app.logger.info('Getting data from API call')
        data = request.get_json()
    app.logger.info(data)
    item = Item()
    item.deserialize(data)
    item.save()
    message = item.serialize()
    location_url = url_for('get_items', item_id=item.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)
示例#4
0
 def test_deserialize_a_item(self):
     """ Deserialize a Item """
     data = {"id": 1, "name": "kitty", "price": "cat", "available": True}
     item = Item(data['id'])
     item.deserialize(data)
     self.assertNotEqual(item, None)
     self.assertEqual(item.id, 1)
     self.assertEqual(item.name, "kitty")
     self.assertEqual(item.price, "cat")
    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)
示例#6
0
 def test_deserialize_an_item(self):
     """ Test deserialization of a Item """
     data = {
         "id": 1,
         "sku": "ID111",
         "count": 3,
         "price": 2.00,
         "name": "test_item",
         "link": "test.com",
         "brand_name": "gucci",
         "is_available": True
     }
     item = Item()
     item.deserialize(data)
     self.assertNotEqual(item, None)
     self.assertEqual(item.id, None)
     self.assertEqual(item.sku, "ID111")
     self.assertEqual(item.count, 3)
     self.assertEqual(item.price, 2.00)
     self.assertEqual(item.name, "test_item")
     self.assertEqual(item.link, "test.com")
     self.assertEqual(item.brand_name, "gucci")
     self.assertEqual(item.is_available, True)
def create_order():
    """
    Creates an Order object based on the JSON posted
    This endpoint will create an Order based the data in the body that is posted
    ---
    tags:
      - Orders
    consumes:
      - application/json
    produces:
      - application/json
    parameters:
      - in: body
        name: body
        required: true
        schema:
          id: data
          required:
            - customer_id
            - status
            - date
          properties:
            customer_id:
              type: integer
              description: the customer id for the order
            status:
              type: string
              description: the order status
            date:
              type: string
              description: the date of the order
            items:
              type: array
              items:
                properties:
                    name:
                        type: string
                        description: the item name
                    product_id:
                        type: integer
                        description: the product_id of the item
                    quantity:
                        type: integer
                        description: the quantity of the item
                    price:
                        type: number
                        description: the price of the item
    responses:
      201:
        description: Order created
        schema:
          $ref: '#/definitions/Order'
      400:
        description: Bad Request (the posted data was not valid)
    """

    check_content_type('application/json')
    order = Order()
    json_post = request.get_json()

    order.deserialize(json_post)
    order.save()
    message = order.serialize()
    """
    Want to get the items from POST and create items associated
    with order
    """
    current_order_id = message['id']
    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())
    """
    The individual responses during the loop were added to a list
    so that the responses can be added to the POST response
    """
    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})