Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 1cfb00dc4164f7a5342c1866a50485b1c2845cb7 1 parent 4531767
Alex Gaynor authored
26  django/contrib/auth/context_processors.py
... ...
@@ -1,5 +1,3 @@
1  
-from django.utils.functional import lazy, memoize, SimpleLazyObject
2  
-
3 1
 # PermWrapper and PermLookupDict proxy the permissions system into objects that
4 2
 # the template system can understand.
5 3
 
@@ -36,23 +34,13 @@ def auth(request):
36 34
     If there is no 'user' attribute in the request, uses AnonymousUser (from
37 35
     django.contrib.auth).
38 36
     """
39  
-    # If we access request.user, request.session is accessed, which results in
40  
-    # 'Vary: Cookie' being sent in every request that uses this context
41  
-    # processor, which can easily be every request on a site if
42  
-    # TEMPLATE_CONTEXT_PROCESSORS has this context processor added.  This kills
43  
-    # the ability to cache.  So, we carefully ensure these attributes are lazy.
44  
-    # We don't use django.utils.functional.lazy() for User, because that
45  
-    # requires knowing the class of the object we want to proxy, which could
46  
-    # break with custom auth backends.  LazyObject is a less complete but more
47  
-    # flexible solution that is a good enough wrapper for 'User'.
48  
-    def get_user():
49  
-        if hasattr(request, 'user'):
50  
-            return request.user
51  
-        else:
52  
-            from django.contrib.auth.models import AnonymousUser
53  
-            return AnonymousUser()
  37
+    if hasattr(request, 'user'):
  38
+        user = request.user
  39
+    else:
  40
+        from django.contrib.auth.models import AnonymousUser
  41
+        user = AnonymousUser()
54 42
 
55 43
     return {
56  
-        'user': SimpleLazyObject(get_user),
57  
-        'perms':  lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
  44
+        'user': user,
  45
+        'perms': PermWrapper(user),
58 46
     }
21  django/contrib/auth/middleware.py
... ...
@@ -1,26 +1,21 @@
1 1
 from django.contrib import auth
2 2
 from django.core.exceptions import ImproperlyConfigured
  3
+from django.utils.functional import SimpleLazyObject
3 4
 
4 5
 
5  
-class LazyUser(object):
6  
-    def __get__(self, request, obj_type=None):
7  
-        if not hasattr(request, '_cached_user'):
8  
-            from django.contrib.auth import get_user
9  
-            request._cached_user = get_user(request)
10  
-        return request._cached_user
  6
+def get_user(request):
  7
+    from django.contrib.auth import get_user
  8
+
  9
+    if not hasattr(request, '_cached_user'):
  10
+        request._cached_user = get_user(request)
  11
+    return request._cached_user
11 12
 
12 13
 
13 14
 class AuthenticationMiddleware(object):
14 15
     def process_request(self, request):
15 16
         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'."
16 17
 
17  
-        # We dynamically subclass request.__class__ rather than monkey patch the
18  
-        # original class.
19  
-        class RequestWithUser(request.__class__):
20  
-            user = LazyUser()
21  
-
22  
-        request.__class__ = RequestWithUser
23  
-        return None
  18
+        request.user = SimpleLazyObject(lambda: get_user(request))
24 19
 
25 20
 
26 21
 class RemoteUserMiddleware(object):
6  django/utils/functional.py
@@ -208,7 +208,7 @@ def _setup(self):
208 208
     def __dir__(self):
209 209
         if self._wrapped is None:
210 210
             self._setup()
211  
-        return  dir(self._wrapped)
  211
+        return dir(self._wrapped)
212 212
 
213 213
 class SimpleLazyObject(LazyObject):
214 214
     """
@@ -227,9 +227,7 @@ def __init__(self, func):
227 227
         value.
228 228
         """
229 229
         self.__dict__['_setupfunc'] = func
230  
-        # For some reason, we have to inline LazyObject.__init__ here to avoid
231  
-        # recursion
232  
-        self._wrapped = None
  230
+        super(SimpleLazyObject, self).__init__()
233 231
 
234 232
     def __str__(self):
235 233
         if self._wrapped is None: self._setup()

0 notes on commit 1cfb00d

Please sign in to comment.
Something went wrong with that request. Please try again.