Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed a regression on Python 2.6 caused by r11623

This might fix #12037, but I cannot reproduce that bug.
Refs #12037



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11625 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a2d8acbacda353248fd191b97155cd4cf27dcd66 1 parent c161bf2
Luke Plant authored October 15, 2009
24  django/core/context_processors.py
@@ -8,27 +8,7 @@
8 8
 """
9 9
 
10 10
 from django.conf import settings
11  
-from django.utils.functional import lazy, memoize, LazyObject
12  
-
13  
-class ContextLazyObject(LazyObject):
14  
-    """
15  
-    A lazy object initialised from any function, useful for lazily
16  
-    adding things to the Context.
17  
-
18  
-    Designed for compound objects of unknown type. For simple objects of known
19  
-    type, use django.utils.functional.lazy.
20  
-    """
21  
-    def __init__(self, func):
22  
-        """
23  
-        Pass in a callable that returns the actual value to be used
24  
-        """
25  
-        self.__dict__['_setupfunc'] = func
26  
-        # For some reason, we have to inline LazyObject.__init__ here to avoid
27  
-        # recursion
28  
-        self._wrapped = None
29  
-
30  
-    def _setup(self):
31  
-        self._wrapped = self._setupfunc()
  11
+from django.utils.functional import lazy, memoize, SimpleLazyObject
32 12
 
33 13
 def auth(request):
34 14
     """
@@ -55,7 +35,7 @@ def get_user():
55 35
             return AnonymousUser()
56 36
 
57 37
     return {
58  
-        'user': ContextLazyObject(get_user),
  38
+        'user': SimpleLazyObject(get_user),
59 39
         'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(),
60 40
         'perms':  lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
61 41
     }
28  django/utils/functional.py
@@ -257,9 +257,8 @@ class LazyObject(object):
257 257
     A wrapper for another class that can be used to delay instantiation of the
258 258
     wrapped class.
259 259
 
260  
-    This is useful, for example, if the wrapped class needs to use Django
261  
-    settings at creation time: we want to permit it to be imported without
262  
-    accessing settings.
  260
+    By subclassing, you have the opportunity to intercept and alter the
  261
+    instantiation. If you don't need to do that, use SimpleLazyObject.
263 262
     """
264 263
     def __init__(self):
265 264
         self._wrapped = None
@@ -287,3 +286,26 @@ def _setup(self):
287 286
         """
288 287
         raise NotImplementedError
289 288
 
  289
+
  290
+class SimpleLazyObject(LazyObject):
  291
+    """
  292
+    A lazy object initialised from any function.
  293
+
  294
+    Designed for compound objects of unknown type. For builtins or objects of
  295
+    known type, use django.utils.functional.lazy.
  296
+    """
  297
+    def __init__(self, func):
  298
+        """
  299
+        Pass in a callable that returns the object to be wrapped.
  300
+        """
  301
+        self.__dict__['_setupfunc'] = func
  302
+        # For some reason, we have to inline LazyObject.__init__ here to avoid
  303
+        # recursion
  304
+        self._wrapped = None
  305
+
  306
+    def __str__(self):
  307
+        if self._wrapped is None: self._setup()
  308
+        return str(self._wrapped)
  309
+
  310
+    def _setup(self):
  311
+        self._wrapped = self._setupfunc()
1  tests/regressiontests/context_processors/templates/context_processors/auth_attrs_user.html
... ...
@@ -1,3 +1,4 @@
1 1
 unicode: {{ user }}
2 2
 id: {{ user.id }}
3 3
 username: {{ user.username }}
  4
+url: {% url userpage user %}
4  tests/regressiontests/context_processors/tests.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django.conf import settings
6 6
 from django.test import TestCase
7  
-
  7
+from django.template import Template
8 8
 
9 9
 class RequestContextProcessorTests(TestCase):
10 10
     """
@@ -79,3 +79,5 @@ def test_user_attrs(self):
79 79
         self.assertContains(response, "unicode: super")
80 80
         self.assertContains(response, "id: 100")
81 81
         self.assertContains(response, "username: super")
  82
+        # bug #12037 is tested by the {% url %} in the template:
  83
+        self.assertContains(response, "url: /userpage/super/")
1  tests/regressiontests/context_processors/urls.py
@@ -10,4 +10,5 @@
10 10
     (r'^auth_processor_user/$', views.auth_processor_user),
11 11
     (r'^auth_processor_perms/$', views.auth_processor_perms),
12 12
     (r'^auth_processor_messages/$', views.auth_processor_messages),
  13
+    url(r'^userpage/(.+)/$', views.userpage, name="userpage"),
13 14
 )
3  tests/regressiontests/context_processors/views.py
@@ -32,3 +32,6 @@ def auth_processor_messages(request):
32 32
     request.user.message_set.create(message="Message 1")
33 33
     return render_to_response('context_processors/auth_attrs_messages.html',
34 34
          RequestContext(request, {}, processors=[context_processors.auth]))
  35
+
  36
+def userpage(request):
  37
+    pass

0 notes on commit a2d8acb

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