def get(self):
        """ Query a wishlist by its id """
        app.logger.info('Querying Wishlist list')
        wishlist_id = request.args.get('id')
        customer_id = request.args.get('customer_id')
        name = request.args.get('name')

        wishlist = []

        if not wishlist_id and not name and not customer_id:
            wishlist = Wishlist.all()
        else:
            wishlist = Wishlist.find_by_all(wishlist_id=wishlist_id,
                                            customer_id=customer_id,
                                            name=name)

        if not wishlist:
            api.abort(404, "No wishlist found.")

        response_content = [res.serialize() for res in wishlist]

        if response_content is None or len(response_content) == 0:
            api.abort(404, "No wishlist found.")

        return response_content, status.HTTP_200_OK
    def post(self):
        """
        Create a Wishlist
        This endpoint will create a Wishlist. It expects the name and
        customer_id in the body
        """
        app.logger.info('Request to create a wishlist')
        check_content_type('application/json')
        body = request.get_json()
        app.logger.info('Body: %s', body)

        name = body.get('name', '')
        customer_id = body.get('customer_id', 0)

        if name == '':
            raise DataValidationError('Invalid request: missing name')

        if not isinstance(customer_id, int) or customer_id <= 0:
            raise DataValidationError('Invalid request: Wrong customer_id. ' \
                                      'Expected a number > 0')

        wishlist = Wishlist(name=name, customer_id=customer_id)
        wishlist.save()

        message = wishlist.serialize()

        # TO-DO: Replace with URL for GET wishlist once ready
        # location_url = api.url_for(WishlistResource, wishlist_id=wishlist.id, _external=True)
        location_url = '%s/wishlists/%s' % (request.base_url, wishlist.id)
        return message, status.HTTP_201_CREATED, {'Location': location_url}
 def test_deserialize_a_wishlist(self):
     """ Test deserialization of a Wishlist """
     data = {"id": 1, "name": "wishlist_name", "customer_id": 1234}
     wishlist = Wishlist()
     wishlist.deserialize(data)
     self.assertNotEqual(wishlist, None)
     self.assertEqual(wishlist.id, 1)
     self.assertEqual(wishlist.name, "wishlist_name")
     self.assertEqual(wishlist.customer_id, 1234)
 def test_serialize_a_wishlist(self):
     """ Test serialization of a Wishlist """
     wishlist = Wishlist(name="wishlist_name", customer_id=1234)
     data = wishlist.serialize()
     self.assertNotEqual(data, None)
     self.assertIn('id', data)
     self.assertEqual(data['id'], None)
     self.assertIn('name', data)
     self.assertEqual(data['name'], "wishlist_name")
     self.assertIn('customer_id', data)
     self.assertEqual(data['customer_id'], 1234)
Пример #5
0
    def test_delete_wishlist_product(self):
        """ Delete a Wishlist Product"""
        wishlist = Wishlist(name="wishlist_name", customer_id=1234)
        wishlist.save()
        self.assertEqual(len(wishlist.all()), 1)

        wishlist_product = WishlistProduct(wishlist_id=1, product_id=1213321,
                                           product_name="Macbook Pro")
        wishlist_product.save()
        self.assertTrue(wishlist_product is not None)
        wishlist_product.delete()
        self.assertEqual(len(WishlistProduct.all()), 0)
 def test_delete_wishlist(self):
     """ Delete a Wishlist """
     wishlist = Wishlist(name="wishlist_name", customer_id=1234)
     wishlist.save()
     self.assertEqual(len(Wishlist.all()), 1)
     # delete the wishlist and make sure it isn't in the database
     wishlist.delete()
     self.assertEqual(len(Wishlist.all()), 0)
    def put(self, wishlist_id, product_id):
        """
        Update a Wishlist Product
        This endpoint will update a Product in a Wishlist
        """
        app.logger.info(
            'Request to update a product with id: %s in wishlist: %s',
            product_id, wishlist_id)
        check_content_type('application/json')

        wishlist = Wishlist.find(wishlist_id)
        wishlist_product = WishlistProduct.find(wishlist_id, product_id)

        if not wishlist:
            api.abort(status.HTTP_404_NOT_FOUND, "Wishlist with id '{}' not found"\
                       .format(wishlist_id))
        elif not wishlist_product:
            api.abort(
                status.HTTP_404_NOT_FOUND, "Product with id '{}' not found in\
                      wishlist with id '{}'.".format(product_id, wishlist_id))

        body = request.get_json()
        app.logger.info('Body: %s', body)

        product_name = body.get('product_name', '')

        if product_name == '':
            api.abort(status.HTTP_400_BAD_REQUEST,
                      "Product needs a non-empty name.")

        wishlist_product.product_name = product_name
        wishlist_product.save()

        return wishlist_product.serialize(), status.HTTP_200_OK
    def put(self, wishlist_id):
        """
        Rename a Wishlist
        This endpoint will return a Wishlist based on it's id
        """
        app.logger.info('Request to rename a wishlist with id: %s',
                        wishlist_id)
        check_content_type('application/json')
        body = request.get_json()
        app.logger.info('Body: %s', body)

        name = body.get('name', '')

        if name == '':
            api.abort(400, "Invalid request: missing name")

        wishlist = Wishlist.find(wishlist_id)

        if not wishlist:
            api.abort(404, "No wishlist found.")

        wishlist.name = name
        wishlist.save()

        return wishlist.serialize(), status.HTTP_200_OK
    def delete(self, wishlist_id):
        """
        Delete a Wishlist

        This endpoint will delete a Wishlist based the id specified in the path
        """
        app.logger.info('Request to delete wishlist with id: %s', wishlist_id)
        wishlist = Wishlist.find(wishlist_id)
        if wishlist:
            wishlist.delete()
        return '', status.HTTP_204_NO_CONTENT
    def get(self, wishlist_id):
        """
        Retrieve a single Wishlist

        This endpoint will return a Wishlist based on it's id
        """
        app.logger.info("Request to Retrieve a wishlist with id [%s]",
                        wishlist_id)
        wishlist = Wishlist.find(wishlist_id)
        if not wishlist:
            api.abort(
                status.HTTP_404_NOT_FOUND,
                "Wishlist with id '{}' was not found.".format(wishlist_id))
        return wishlist.serialize(), status.HTTP_200_OK
    def post(self, wishlist_id):
        """
        This endpoint adds an item to a Wishlist. It expects the
        wishlist_id and product_id.
        """
        app.logger.info('Request to add item into wishlist')
        check_content_type('application/json')

        # checking if the wishlist exists:
        wishlist = Wishlist.find(wishlist_id)
        if not wishlist:
            api.abort(status.HTTP_404_NOT_FOUND,
                      "Wishlist with id '%s' was not found." % wishlist_id)
        wishlist_product = WishlistProduct()
        wishlist_product.wishlist_id = wishlist_id

        body = request.get_json()
        app.logger.info('Body: %s', body)

        product_name = body.get('product_name', '')
        product_id = body.get('product_id', 0)

        if product_name == '':
            raise DataValidationError('Invalid request: missing name')

        wishlist_product.product_name = product_name

        if product_id == 0:
            raise DataValidationError('Invalid request: missing product id')

        wishlist_product.product_id = product_id

        app.logger.info('Request to add %s item to wishlist %s' %
                        (wishlist_product.product_id, wishlist_id))

        wishlist_product.save()
        message = wishlist_product.serialize()

        location_url = api.url_for(ProductResource,
                                   wishlist_id=wishlist.id,
                                   product_id=wishlist_product.product_id,
                                   _external=True)

        return message, status.HTTP_201_CREATED, {'Location': location_url}
    def put(self, wishlist_id, product_id):
        """
            Move item from Wishlist to cart
            This endpoint will request to move and item in wishlist to cart
        """
        app.logger.info('Request to move item %s in wishlist %s to cart',
                        product_id, wishlist_id)
        wishlist = Wishlist.find(wishlist_id)
        if not wishlist:
            raise NotFound(
                "Wishlist with id '{}' was not found.".format(wishlist_id))

        wishlist_product = WishlistProduct.find(wishlist_id, product_id)
        if ((not wishlist_product)
                or (wishlist_product.wishlist_id != wishlist_id)):
            raise NotFound(
                "Wishlist Product with id '{}' was not found in Wishlist \
                            with id '{}'.".format(product_id, wishlist_id))

        wishlist_product.add_to_cart(wishlist.customer_id)
        wishlist_product.delete()

        return '', status.HTTP_204_NO_CONTENT
 def test_repr(self):
     """ Create a wishlist and assert that it exists """
     wishlist = Wishlist(name="ShoppingList", customer_id=1234)
     self.assertTrue(wishlist is not None)
     self.assertEqual(repr(wishlist), "<Wishlist 'ShoppingList'>")
     self.assertEqual(wishlist.customer_id, 1234)
 def test_deserialize_missing_data(self):
     """ Test deserialization of missing data """
     data = {"id": 1, "customer_id": 1234}
     wishlist = Wishlist()
     self.assertRaises(DataValidationError, wishlist.deserialize, data)
 def test_deserialize_bad_data(self):
     """ Test deserialization of bad data """
     data = "this is not a dictionary"
     wishlist = Wishlist()
     self.assertRaises(DataValidationError, wishlist.deserialize, data)