Loading django/core/context_processors.py +2 −22 Original line number Diff line number Diff line Loading @@ -8,27 +8,7 @@ RequestContext. """ from django.conf import settings from django.utils.functional import lazy, memoize, LazyObject class ContextLazyObject(LazyObject): """ A lazy object initialised from any function, useful for lazily adding things to the Context. Designed for compound objects of unknown type. For simple objects of known type, use django.utils.functional.lazy. """ def __init__(self, func): """ Pass in a callable that returns the actual value to be used """ self.__dict__['_setupfunc'] = func # For some reason, we have to inline LazyObject.__init__ here to avoid # recursion self._wrapped = None def _setup(self): self._wrapped = self._setupfunc() from django.utils.functional import lazy, memoize, SimpleLazyObject def auth(request): """ Loading @@ -55,7 +35,7 @@ def auth(request): return AnonymousUser() return { 'user': ContextLazyObject(get_user), 'user': SimpleLazyObject(get_user), 'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(), 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(), } Loading django/utils/functional.py +25 −3 Original line number Diff line number Diff line Loading @@ -257,9 +257,8 @@ class LazyObject(object): A wrapper for another class that can be used to delay instantiation of the wrapped class. This is useful, for example, if the wrapped class needs to use Django settings at creation time: we want to permit it to be imported without accessing settings. By subclassing, you have the opportunity to intercept and alter the instantiation. If you don't need to do that, use SimpleLazyObject. """ def __init__(self): self._wrapped = None Loading Loading @@ -287,3 +286,26 @@ class LazyObject(object): """ raise NotImplementedError class SimpleLazyObject(LazyObject): """ A lazy object initialised from any function. Designed for compound objects of unknown type. For builtins or objects of known type, use django.utils.functional.lazy. """ def __init__(self, func): """ Pass in a callable that returns the object to be wrapped. """ self.__dict__['_setupfunc'] = func # For some reason, we have to inline LazyObject.__init__ here to avoid # recursion self._wrapped = None def __str__(self): if self._wrapped is None: self._setup() return str(self._wrapped) def _setup(self): self._wrapped = self._setupfunc() tests/regressiontests/context_processors/templates/context_processors/auth_attrs_user.html +1 −0 Original line number Diff line number Diff line unicode: {{ user }} id: {{ user.id }} username: {{ user.username }} url: {% url userpage user %} tests/regressiontests/context_processors/tests.py +3 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ Tests for Django's bundled context processors. from django.conf import settings from django.test import TestCase from django.template import Template class RequestContextProcessorTests(TestCase): """ Loading Loading @@ -79,3 +79,5 @@ class AuthContextProcessorTests(TestCase): self.assertContains(response, "unicode: super") self.assertContains(response, "id: 100") self.assertContains(response, "username: super") # bug #12037 is tested by the {% url %} in the template: self.assertContains(response, "url: /userpage/super/") tests/regressiontests/context_processors/urls.py +1 −0 Original line number Diff line number Diff line Loading @@ -10,4 +10,5 @@ urlpatterns = patterns('', (r'^auth_processor_user/$', views.auth_processor_user), (r'^auth_processor_perms/$', views.auth_processor_perms), (r'^auth_processor_messages/$', views.auth_processor_messages), url(r'^userpage/(.+)/$', views.userpage, name="userpage"), ) Loading
django/core/context_processors.py +2 −22 Original line number Diff line number Diff line Loading @@ -8,27 +8,7 @@ RequestContext. """ from django.conf import settings from django.utils.functional import lazy, memoize, LazyObject class ContextLazyObject(LazyObject): """ A lazy object initialised from any function, useful for lazily adding things to the Context. Designed for compound objects of unknown type. For simple objects of known type, use django.utils.functional.lazy. """ def __init__(self, func): """ Pass in a callable that returns the actual value to be used """ self.__dict__['_setupfunc'] = func # For some reason, we have to inline LazyObject.__init__ here to avoid # recursion self._wrapped = None def _setup(self): self._wrapped = self._setupfunc() from django.utils.functional import lazy, memoize, SimpleLazyObject def auth(request): """ Loading @@ -55,7 +35,7 @@ def auth(request): return AnonymousUser() return { 'user': ContextLazyObject(get_user), 'user': SimpleLazyObject(get_user), 'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(), 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(), } Loading
django/utils/functional.py +25 −3 Original line number Diff line number Diff line Loading @@ -257,9 +257,8 @@ class LazyObject(object): A wrapper for another class that can be used to delay instantiation of the wrapped class. This is useful, for example, if the wrapped class needs to use Django settings at creation time: we want to permit it to be imported without accessing settings. By subclassing, you have the opportunity to intercept and alter the instantiation. If you don't need to do that, use SimpleLazyObject. """ def __init__(self): self._wrapped = None Loading Loading @@ -287,3 +286,26 @@ class LazyObject(object): """ raise NotImplementedError class SimpleLazyObject(LazyObject): """ A lazy object initialised from any function. Designed for compound objects of unknown type. For builtins or objects of known type, use django.utils.functional.lazy. """ def __init__(self, func): """ Pass in a callable that returns the object to be wrapped. """ self.__dict__['_setupfunc'] = func # For some reason, we have to inline LazyObject.__init__ here to avoid # recursion self._wrapped = None def __str__(self): if self._wrapped is None: self._setup() return str(self._wrapped) def _setup(self): self._wrapped = self._setupfunc()
tests/regressiontests/context_processors/templates/context_processors/auth_attrs_user.html +1 −0 Original line number Diff line number Diff line unicode: {{ user }} id: {{ user.id }} username: {{ user.username }} url: {% url userpage user %}
tests/regressiontests/context_processors/tests.py +3 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ Tests for Django's bundled context processors. from django.conf import settings from django.test import TestCase from django.template import Template class RequestContextProcessorTests(TestCase): """ Loading Loading @@ -79,3 +79,5 @@ class AuthContextProcessorTests(TestCase): self.assertContains(response, "unicode: super") self.assertContains(response, "id: 100") self.assertContains(response, "username: super") # bug #12037 is tested by the {% url %} in the template: self.assertContains(response, "url: /userpage/super/")
tests/regressiontests/context_processors/urls.py +1 −0 Original line number Diff line number Diff line Loading @@ -10,4 +10,5 @@ urlpatterns = patterns('', (r'^auth_processor_user/$', views.auth_processor_user), (r'^auth_processor_perms/$', views.auth_processor_perms), (r'^auth_processor_messages/$', views.auth_processor_messages), url(r'^userpage/(.+)/$', views.userpage, name="userpage"), )