django_api/decorators.py
def api(accept_return_dict):
    """
    Wrapper that calls @api_accepts and @api_returns in sequence.
    For example:

    @api({
        'accepts': {
            'x': forms.IntegerField(min_value=0),
            'y': forms.IntegerField(min_value=0),
        },
        'returns': [
            200: 'Operation successful',
            403: 'User does not have persion',
            404: 'Resource not found',
            404: 'User not found',
        ]
    })
    def add(request, *args, **kwargs):
        if not request.GET['x'] == 10:
            return JsonResponseForbidden()  # 403

        return HttpResponse()  # 200
    """
    def decorator(func):
        @wraps(func)
        def wrapped_func(request, *args, **kwargs):
            @api_accepts(accept_return_dict['accepts'])
            @api_returns(accept_return_dict['returns'])
            def apid_fnc(request, *args, **kwargs):
                return func(request, *args, **kwargs)

            return apid_fnc(request, *args, **kwargs)
        return wrapped_func
    return decorator
def api(accept_return_dict):
    """
    Wrapper that calls @api_accepts and @api_returns in sequence.
    For example:

    @api({
        'accepts': {
            'x': forms.IntegerField(min_value=0),
            'y': forms.IntegerField(min_value=0),
        },
        'returns': [
            200: 'Operation successful',
            403: 'User does not have persion',
            404: 'Resource not found',
            404: 'User not found',
        ]
    })
    def add(request, *args, **kwargs):
        if not request.GET['x'] == 10:
            return JsonResponseForbidden()  # 403

        return HttpResponse()  # 200
    """
    def decorator(func):
        @wraps(func)
        def wrapped_func(request, *args, **kwargs):
            @api_accepts(accept_return_dict['accepts'])
            @api_returns(accept_return_dict['returns'])
            def apid_fnc(request, *args, **kwargs):
                return func(request, *args, **kwargs)

            return apid_fnc(request, *args, **kwargs)
        return wrapped_func
    return decorator
Wrapper that calls @api_accepts and @api_returns in sequence.
    For example:

    @api({
        'accepts': {
            'x': forms.IntegerField(min_value=0),
            'y': forms.IntegerField(min_value=0),
        },
        'returns': [
            200: 'Operation successful',
            403: 'User does not have persion',
            404: 'Resource not found',
            404: 'User not found',
        ]
    })
    def add(request, *args, **kwargs):
        if not request.GET['x'] == 10:
            return JsonResponseForbidden()  # 403

        return HttpResponse()  # 200