A json pendant to permission_required. Will return a 401 response if
    the user is not allowed. The body of the response will be the following json
    data::

        {
            "status": 401,
            "message": "Unauthorized",
            "data": {}
        }

    Example::

        import jason

        @jason.permission_required("my_perm")
        def my_view(request):
            ...

def permission_required(perm):
    """
    A json pendant to permission_required. Will return a 401 response if
    the user is not allowed. The body of the response will be the following json
    data::

        {
            "status": 401,
            "message": "Unauthorized",
            "data": {}
        }

    Example::

        import jason

        @jason.permission_required("my_perm")
        def my_view(request):
            ...
    """
    def _(f):
        def __(request, *args, **kwargs):
            if request.user.has_perm(perm):
                return f(request, *args, **kwargs)
            else:
                return response({}, 401, 'Unauthorized')
        return __
    return _