Cleaned up how ``request.user`` is set, this is a follow up to [16297]. Thanks for the review Luke.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16305 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2011-05-31 15:43:19 +00:00
parent 4531767700
commit 1cfb00dc41
3 changed files with 17 additions and 36 deletions

View File

@ -1,5 +1,3 @@
from django.utils.functional import lazy, memoize, SimpleLazyObject
# PermWrapper and PermLookupDict proxy the permissions system into objects that # PermWrapper and PermLookupDict proxy the permissions system into objects that
# the template system can understand. # the template system can understand.
@ -36,23 +34,13 @@ def auth(request):
If there is no 'user' attribute in the request, uses AnonymousUser (from If there is no 'user' attribute in the request, uses AnonymousUser (from
django.contrib.auth). django.contrib.auth).
""" """
# If we access request.user, request.session is accessed, which results in if hasattr(request, 'user'):
# 'Vary: Cookie' being sent in every request that uses this context user = request.user
# processor, which can easily be every request on a site if else:
# TEMPLATE_CONTEXT_PROCESSORS has this context processor added. This kills from django.contrib.auth.models import AnonymousUser
# the ability to cache. So, we carefully ensure these attributes are lazy. user = AnonymousUser()
# We don't use django.utils.functional.lazy() for User, because that
# requires knowing the class of the object we want to proxy, which could
# break with custom auth backends. LazyObject is a less complete but more
# flexible solution that is a good enough wrapper for 'User'.
def get_user():
if hasattr(request, 'user'):
return request.user
else:
from django.contrib.auth.models import AnonymousUser
return AnonymousUser()
return { return {
'user': SimpleLazyObject(get_user), 'user': user,
'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(), 'perms': PermWrapper(user),
} }

View File

@ -1,26 +1,21 @@
from django.contrib import auth from django.contrib import auth
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.functional import SimpleLazyObject
class LazyUser(object): def get_user(request):
def __get__(self, request, obj_type=None): from django.contrib.auth import get_user
if not hasattr(request, '_cached_user'):
from django.contrib.auth import get_user if not hasattr(request, '_cached_user'):
request._cached_user = get_user(request) request._cached_user = get_user(request)
return request._cached_user return request._cached_user
class AuthenticationMiddleware(object): class AuthenticationMiddleware(object):
def process_request(self, request): def process_request(self, request):
assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'." assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
# We dynamically subclass request.__class__ rather than monkey patch the request.user = SimpleLazyObject(lambda: get_user(request))
# original class.
class RequestWithUser(request.__class__):
user = LazyUser()
request.__class__ = RequestWithUser
return None
class RemoteUserMiddleware(object): class RemoteUserMiddleware(object):

View File

@ -208,7 +208,7 @@ class LazyObject(object):
def __dir__(self): def __dir__(self):
if self._wrapped is None: if self._wrapped is None:
self._setup() self._setup()
return dir(self._wrapped) return dir(self._wrapped)
class SimpleLazyObject(LazyObject): class SimpleLazyObject(LazyObject):
""" """
@ -227,9 +227,7 @@ class SimpleLazyObject(LazyObject):
value. value.
""" """
self.__dict__['_setupfunc'] = func self.__dict__['_setupfunc'] = func
# For some reason, we have to inline LazyObject.__init__ here to avoid super(SimpleLazyObject, self).__init__()
# recursion
self._wrapped = None
def __str__(self): def __str__(self):
if self._wrapped is None: self._setup() if self._wrapped is None: self._setup()