django_api/decorators.py
def validate_json_request(required_fields):
    """
    Return a decorator that ensures that the request passed to the view
    function/method has a valid JSON request body with the given required
    fields.  The dict parsed from the JSON is then passed as the second
    argument to the decorated function/method.  For example:

    @json_request({'name', 'date'})
    def view_func(request, request_dict):
        ...
    """
    def decorator(func):
        @wraps(func)
        def wrapped_func(request, *args, **kwargs):
            try:
                request_dict = json.loads(request.raw_post_data)
            except ValueError as e:
                return JsonResponseBadRequest('invalid POST JSON: %s' % e)

            for k in required_fields:
                if k not in request_dict:
                    return JsonResponseBadRequest(
                        'POST JSON must contain property \'%s\'' % k)

            return func(request, request_dict, *args, **kwargs)
        return wrapped_func
    return decorator
def validate_json_request(required_fields):
    """
    Return a decorator that ensures that the request passed to the view
    function/method has a valid JSON request body with the given required
    fields.  The dict parsed from the JSON is then passed as the second
    argument to the decorated function/method.  For example:

    @json_request({'name', 'date'})
    def view_func(request, request_dict):
        ...
    """
    def decorator(func):
        @wraps(func)
        def wrapped_func(request, *args, **kwargs):
            try:
                request_dict = json.loads(request.raw_post_data)
            except ValueError as e:
                return JsonResponseBadRequest('invalid POST JSON: %s' % e)

            for k in required_fields:
                if k not in request_dict:
                    return JsonResponseBadRequest(
                        'POST JSON must contain property \'%s\'' % k)

            return func(request, request_dict, *args, **kwargs)
        return wrapped_func
    return decorator
Return a decorator that ensures that the request passed to the view
    function/method has a valid JSON request body with the given required
    fields.  The dict parsed from the JSON is then passed as the second
    argument to the decorated function/method.  For example:

    @json_request({'name', 'date'})
    def view_func(request, request_dict):
        ...