Example #1
0
def product_list(request):
    """
    */products/*

    Returns a list of all products in the database. The ?limit=<int> parameter
    limits the number of products returned.
    """
    error = {"status": False, "name": None, "text": None, "level": None, "debug": None}

    limit, error = get_limit(request, error)

    serializer = FreshSerializer()
    queryset = Product.objects.all()[:limit]

    if not queryset:
        error = {
            "status": True,
            "name": "No Products",
            "text": "No Products found",
            "level": "Information",
            "debug": "",
        }

    data = {"products": json.loads(serializer.serialize(queryset, use_natural_foreign_keys=True)), "error": error}

    return HttpResponse(json.dumps(data), content_type="application/json")
Example #2
0
    def test_get_limit_invalid_limit_existing_error(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': 'I like Pi'}

        original_error = {
            'debug': "This is an existing error",
            'status': True,
            'level': 'Important',
            'text': 'This error already exists',
            'name': "I've errored!"
        }

        expected_error = {
            'debug': "ValueError: invalid literal for int() "
            "with base 10: 'I like Pi'",
            'status': True,
            'level': 'Warning',
            'text': 'Invalid limit. Returning all results.',
            'name': 'Bad Limit'
        }

        expected_result = [None, expected_error]
        actual_result = get_limit(mock_request, original_error)

        self.assertEqual(expected_result, actual_result)
    def test_get_limit_invalid_limit_existing_error(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': 'I like Pi'}

        original_error = {
            'debug': "This is an existing error",
            'status': True,
            'level': 'Important',
            'text': 'This error already exists',
            'name': "I've errored!"
        }

        expected_error = {
            'debug': "ValueError: invalid literal for int() "
                     "with base 10: 'I like Pi'",
            'status': True,
            'level': 'Warning',
            'text': 'Invalid limit. Returning all results.',
            'name': 'Bad Limit'
        }

        expected_result = [None, expected_error]
        actual_result = get_limit(mock_request, original_error)

        self.assertEqual(expected_result, actual_result)
Example #4
0
    def test_get_limit_valid_limit(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': '80'}

        expected_result = [80, self.base_error]
        actual_result = get_limit(mock_request, self.base_error)

        self.assertEqual(expected_result, actual_result)
    def test_get_limit_valid_limit(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': '80'}

        expected_result = [80, self.base_error]
        actual_result = get_limit(mock_request, self.base_error)

        self.assertEqual(expected_result, actual_result)
Example #6
0
def product_vendor(request, id=None):
    """
    */products/vendors/<id>*

    List all products sold by vendor <id>. This information includes the
    details of the products, rather than only the product name/id and
    preparation name/id returned by */vendors/<id>*.
    """
    data = {}
    error = {
        'status': False,
        'name': None,
        'text': None,
        'level': None,
        'debug': None
    }
    limit, error = get_limit(request, error)

    try:
        product_list = Product.objects.filter(
            productpreparation__vendorproduct__vendor__id__exact=id)[:limit]
    except Exception as e:
        data['error'] = {
            'status': True,
            'name': 'Vendor Not Found',
            'text': 'Vendor with id %s not found!' % id,
            'level': 'Error',
            'debug': "{0}: {1}".format(type(e).__name__, str(e))
        }
        data['products'] = []
        return HttpResponse(json.dumps(data), content_type="application/json")

    serializer = FreshSerializer()

    if not product_list:
        error = {
            "status": True,
            "name": "No Products",
            "text": "No Products found",
            "level": "Information",
            "debug": ""
        }

    data = {
        "products":
        json.loads(
            serializer.serialize(product_list, use_natural_foreign_keys=True)),
        "error":
        error
    }

    return HttpResponse(json.dumps(data), content_type="application/json")
Example #7
0
    def test_get_limit_invalid_limit(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': 'I like Pi'}

        expected_error = {
            'debug': "ValueError: invalid literal for int() "
            "with base 10: 'I like Pi'",
            'status': True,
            'level': 'Warning',
            'text': 'Invalid limit. Returning all results.',
            'name': 'Bad Limit'
        }

        expected_result = [None, expected_error]
        actual_result = get_limit(mock_request, self.base_error)

        self.assertEqual(expected_result, actual_result)
    def test_get_limit_invalid_limit(self, mock_request):
        mock_request = Mock()
        mock_request.GET = {'limit': 'I like Pi'}

        expected_error = {
            'debug': "ValueError: invalid literal for int() "
                     "with base 10: 'I like Pi'",
            'status': True,
            'level': 'Warning',
            'text': 'Invalid limit. Returning all results.',
            'name': 'Bad Limit'
        }

        expected_result = [None, expected_error]
        actual_result = get_limit(mock_request, self.base_error)

        self.assertEqual(expected_result, actual_result)
Example #9
0
def product_vendor(request, id=None):
    """
    */products/vendors/<id>*

    List all products sold by vendor <id>. This information includes the
    details of the products, rather than only the product name/id and
    preparation name/id returned by */vendors/<id>*.
    """
    data = {}
    error = {"status": False, "name": None, "text": None, "level": None, "debug": None}
    limit, error = get_limit(request, error)

    try:
        product_list = Product.objects.filter(productpreparation__vendorproduct__vendor__id__exact=id)[:limit]
    except Exception as e:
        data["error"] = {
            "status": True,
            "name": "Vendor Not Found",
            "text": "Vendor with id %s not found!" % id,
            "level": "Error",
            "debug": "{0}: {1}".format(type(e).__name__, str(e)),
        }
        data["products"] = []
        return HttpResponse(json.dumps(data), content_type="application/json")

    serializer = FreshSerializer()

    if not product_list:
        error = {
            "status": True,
            "name": "No Products",
            "text": "No Products found",
            "level": "Information",
            "debug": "",
        }

    data = {"products": json.loads(serializer.serialize(product_list, use_natural_foreign_keys=True)), "error": error}

    return HttpResponse(json.dumps(data), content_type="application/json")
Example #10
0
def product_list(request):
    """
    */products/*

    Returns a list of all products in the database. The ?limit=<int> parameter
    limits the number of products returned.
    """
    error = {
        'status': False,
        'name': None,
        'text': None,
        'level': None,
        'debug': None
    }

    limit, error = get_limit(request, error)

    serializer = FreshSerializer()
    queryset = Product.objects.all()[:limit]

    if not queryset:
        error = {
            "status": True,
            "name": "No Products",
            "text": "No Products found",
            "level": "Information",
            "debug": ""
        }

    data = {
        "products":
        json.loads(
            serializer.serialize(queryset, use_natural_foreign_keys=True)),
        "error":
        error
    }

    return HttpResponse(json.dumps(data), content_type="application/json")