import json

from django.http import HttpResponse
from django.contrib.auth.models import User
from django.conf import settings
from traceback import format_exc

class AnonymousAuthenticationMiddleware(object):
    """ This middleware class tests whether the current user is the
    anonymous user. If so, it replaces the request.user object with
    Guardian's anonymous user and monkey patchs it to behave like
    Django's anonymou user.
    """
    def process_request(self, request):
        if request.user.is_anonymous() and settings.ANONYMOUS_USER_ID:
            request.user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
            request.user.is_anonymous = lambda: True
            request.user.is_authenticated = lambda: False
        return None

class AjaxExceptionMiddleware(object):

    def process_exception(self, request, exception):
        response = {
            'error': str(exception),
            'detail': format_exc(),
        }
        if settings.DEBUG:
            import sys, traceback
            (exc_type, exc_info, tb) = sys.exc_info()
            response['type'] = exc_type.__name__
            response['info'] = str(exc_info)
            response['traceback'] = ''.join(traceback.format_tb(tb))
        return HttpResponse(json.dumps(response))

import json

from django.http import HttpResponse
from django.contrib.auth.models import User
from django.conf import settings
from traceback import format_exc

class AnonymousAuthenticationMiddleware(object):
    """ This middleware class tests whether the current user is the
    anonymous user. If so, it replaces the request.user object with
    Guardian's anonymous user and monkey patchs it to behave like
    Django's anonymou user.
    """
    def process_request(self, request):
        if request.user.is_anonymous() and settings.ANONYMOUS_USER_ID:
            request.user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
            request.user.is_anonymous = lambda: False
            request.user.is_authenticated = lambda: False
        return None

class AjaxExceptionMiddleware(object):

    def process_exception(self, request, exception):
        response = {
            'error': str(exception),
            'detail': format_exc(),
        }
        if settings.DEBUG:
            import sys, traceback
            (exc_type, exc_info, tb) = sys.exc_info()
            response['type'] = exc_type.__name__
            response['info'] = str(exc_info)
            response['traceback'] = ''.join(traceback.format_tb(tb))
        return HttpResponse(json.dumps(response))

Make anonymous user distinguishable from Django's AnonymousUser
Make anonymous user distinguishable from Django's AnonymousUser

Django uses a special object---the AnonymousUser---for requests that are not
bound to a logged in user. Since we use guardian we replace this object with an
actual user object (with the ID set in ANONYMOUS_USER_ID, usually -1) in a
middleware class. So far this user had its is_anonymous() function set to return
False. This however imposes a problem if one wants to have permissions for this
users (e.g. testing them with user.has_perm()). As it turns out, permissions are
not checked if is_anonymous() returns True. The Django documentation says this
method is only used to distinguish real user objects from the AnonymousUser
object. Our anonymous user is a real user and therefore this commit changes this
function of the anonymous user to return False.
