def user_passes_test_with_ajax(test_func, login_url=None,
                               redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.

    Returns special response to ajax calls instead of blindly redirecting.

    To use with class methods instead of functions, use :meth:`django.utils.decorators.method_decorator`.  See
    http://docs.djangoproject.com/en/dev/releases/1.2/#user-passes-test-login-required-and-permission-required

    Usage is the same as
    :meth:`django.contrib.auth.decorators.user_passes_test`::

        @user_passes_test_with_ajax(lambda u: u.has_perm('polls.can_vote'), login_url='/loginpage/')
        def my_view(request):
            ...

    """
    if not login_url:
        from django.conf import settings
        login_url = settings.LOGIN_URL

    def decorator(view_func):
        @wraps(view_func)
        def _check_user_test(request, *args, **kwargs):
            try:
                test_result = test_func(request.user, request=request, *args, **kwargs)
            except TypeError:
                test_result = test_func(request.user)

            if test_result:
                return view_func(request, *args, **kwargs)
            path = urlquote(request.get_full_path())
            urlparts = login_url, redirect_field_name, path
            # check for ajax request
            if not request.is_ajax():
                return HttpResponseRedirect('%s?%s=%s' % urlparts)
            else:
                # In case of ajax we send 401 - unauthorized HTTP response
                return HttpResponse('%s?%s=%s' % urlparts, status=401)

        return _check_user_test
    return decorator
def user_passes_test_with_ajax ( test_func , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME ) : if not login_url : from django . conf import settings login_url = settings . LOGIN_URL def decorator ( view_func ) : @ wraps ( view_func ) def _check_user_test ( request , * args , ** kwargs ) : try : test_result = test_func ( request . user , request = request , * args , ** kwargs ) except TypeError : test_result = test_func ( request . user ) if test_result : return view_func ( request , * args , ** kwargs ) path = urlquote ( request . get_full_path ( ) ) urlparts = login_url , redirect_field_name , path if not request . is_ajax ( ) : return HttpResponseRedirect ( '%s?%s=%s' % urlparts ) else : return HttpResponse ( '%s?%s=%s' % urlparts , status = 401 ) return _check_user_test return decorator
Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.

    Returns special response to ajax calls instead of blindly redirecting.

    To use with class methods instead of functions, use :meth:`django.utils.decorators.method_decorator`.  See
    http://docs.djangoproject.com/en/dev/releases/1.2/#user-passes-test-login-required-and-permission-required

    Usage is the same as
    :meth:`django.contrib.auth.decorators.user_passes_test`::

        @user_passes_test_with_ajax(lambda u: u.has_perm('polls.can_vote'), login_url='/loginpage/')
        def my_view(request):
            ...