Author: Alex
Date: 2011-05-31 08:43:19 -0700 (Tue, 31 May 2011)
New Revision: 16305

Modified:
   django/trunk/django/contrib/auth/context_processors.py
   django/trunk/django/contrib/auth/middleware.py
   django/trunk/django/utils/functional.py
Log:
Cleaned up how ``request.user`` is set, this is a follow up to [16297]. Thanks 
for the review Luke.

Modified: django/trunk/django/contrib/auth/context_processors.py
===================================================================
--- django/trunk/django/contrib/auth/context_processors.py      2011-05-31 
15:19:19 UTC (rev 16304)
+++ django/trunk/django/contrib/auth/context_processors.py      2011-05-31 
15:43:19 UTC (rev 16305)
@@ -1,5 +1,3 @@
-from django.utils.functional import lazy, memoize, SimpleLazyObject
-
 # PermWrapper and PermLookupDict proxy the permissions system into objects that
 # the template system can understand.
 
@@ -36,23 +34,13 @@
     If there is no 'user' attribute in the request, uses AnonymousUser (from
     django.contrib.auth).
     """
-    # If we access request.user, request.session is accessed, which results in
-    # 'Vary: Cookie' being sent in every request that uses this context
-    # processor, which can easily be every request on a site if
-    # TEMPLATE_CONTEXT_PROCESSORS has this context processor added.  This kills
-    # the ability to cache.  So, we carefully ensure these attributes are lazy.
-    # 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()
+    if hasattr(request, 'user'):
+        user = request.user
+    else:
+        from django.contrib.auth.models import AnonymousUser
+        user = AnonymousUser()
 
     return {
-        'user': SimpleLazyObject(get_user),
-        'perms':  lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
+        'user': user,
+        'perms': PermWrapper(user),
     }

Modified: django/trunk/django/contrib/auth/middleware.py
===================================================================
--- django/trunk/django/contrib/auth/middleware.py      2011-05-31 15:19:19 UTC 
(rev 16304)
+++ django/trunk/django/contrib/auth/middleware.py      2011-05-31 15:43:19 UTC 
(rev 16305)
@@ -1,28 +1,23 @@
 from django.contrib import auth
 from django.core.exceptions import ImproperlyConfigured
+from django.utils.functional import SimpleLazyObject
 
 
-class LazyUser(object):
-    def __get__(self, request, obj_type=None):
-        if not hasattr(request, '_cached_user'):
-            from django.contrib.auth import get_user
-            request._cached_user = get_user(request)
-        return request._cached_user
+def get_user(request):
+    from django.contrib.auth import get_user
 
+    if not hasattr(request, '_cached_user'):
+        request._cached_user = get_user(request)
+    return request._cached_user
 
+
 class AuthenticationMiddleware(object):
     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'."
 
-        # We dynamically subclass request.__class__ rather than monkey patch 
the
-        # original class.
-        class RequestWithUser(request.__class__):
-            user = LazyUser()
+        request.user = SimpleLazyObject(lambda: get_user(request))
 
-        request.__class__ = RequestWithUser
-        return None
 
-
 class RemoteUserMiddleware(object):
     """
     Middleware for utilizing Web-server-provided authentication.

Modified: django/trunk/django/utils/functional.py
===================================================================
--- django/trunk/django/utils/functional.py     2011-05-31 15:19:19 UTC (rev 
16304)
+++ django/trunk/django/utils/functional.py     2011-05-31 15:43:19 UTC (rev 
16305)
@@ -208,7 +208,7 @@
     def __dir__(self):
         if self._wrapped is None:
             self._setup()
-        return  dir(self._wrapped)
+        return dir(self._wrapped)
 
 class SimpleLazyObject(LazyObject):
     """
@@ -227,9 +227,7 @@
         value.
         """
         self.__dict__['_setupfunc'] = func
-        # For some reason, we have to inline LazyObject.__init__ here to avoid
-        # recursion
-        self._wrapped = None
+        super(SimpleLazyObject, self).__init__()
 
     def __str__(self):
         if self._wrapped is None: self._setup()

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to