Permalink
Browse files

Fixed #12066 - Moved auth context processor from core to the auth app…

…. Thanks, Rob Hudson.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12466 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
jezdez committed Feb 21, 2010
1 parent 1559f64 commit 67d4289c2ef56022d14a038a644d020997e2d9c9
@@ -183,7 +183,7 @@
# Each one should be a callable that takes the request object as its
# only parameter and returns a dictionary to add to the context.
TEMPLATE_CONTEXT_PROCESSORS = (
- 'django.core.context_processors.auth',
+ 'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
@@ -155,11 +155,15 @@ def check_dependencies(self):
from django.contrib.contenttypes.models import ContentType
if not LogEntry._meta.installed:
- raise ImproperlyConfigured("Put 'django.contrib.admin' in your INSTALLED_APPS setting in order to use the admin application.")
+ raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
+ "INSTALLED_APPS setting in order to use the admin application.")
if not ContentType._meta.installed:
- raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in your INSTALLED_APPS setting in order to use the admin application.")
- if 'django.core.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS:
- raise ImproperlyConfigured("Put 'django.core.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")
+ raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
+ "your INSTALLED_APPS setting in order to use the admin application.")
+ if not ('django.contrib.auth.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS or
+ 'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS):
+ raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' "
+ "in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")
def admin_view(self, view, cacheable=False):
"""
@@ -0,0 +1,33 @@
+from django.core.context_processors import PermWrapper
+from django.utils.functional import lazy, memoize, SimpleLazyObject
+from django.contrib import messages
+
+def auth(request):
+ """
+ Returns context variables required by apps that use Django's authentication
+ system.
+
+ 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()
+
+ return {
+ 'user': SimpleLazyObject(get_user),
+ 'messages': messages.get_messages(request),
+ 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
+ }
@@ -9,38 +9,25 @@
from django.conf import settings
from django.middleware.csrf import get_token
-from django.utils.functional import lazy, memoize, SimpleLazyObject
-from django.contrib import messages
+from django.utils.functional import lazy
def auth(request):
"""
- Returns context variables required by apps that use Django's authentication
- system.
+ DEPRECATED. This context processor is the old location, and has been moved
+ to `django.contrib.auth.context_processors`.
- If there is no 'user' attribute in the request, uses AnonymousUser (from
- django.contrib.auth).
+ This function still exists for backwards-compatibility; it will be removed
+ in Django 1.4.
"""
- # 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()
-
- return {
- 'user': SimpleLazyObject(get_user),
- 'messages': messages.get_messages(request),
- 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
- }
+ import warnings
+ warnings.warn(
+ "The context processor at `django.core.context_processors.auth` is " \
+ "deprecated; use the path `django.contrib.auth.context_processors.auth` " \
+ "instead.",
+ PendingDeprecationWarning
+ )
+ from django.contrib.auth.context_processors import auth as auth_context_processor
+ return auth_context_processor(request)
def csrf(request):
"""
@@ -86,6 +86,11 @@ their deprecation, as per the :ref:`Django deprecation policy
``django.contrib.syndication`` have been deprecated since the 1.2
release. The class-based view ``views.Feed`` should be used instead.
+ * ``django.core.context_processors.auth``. This release will
+ remove the old method in favor of the new method in
+ ``django.contrib.auth.context_processors.auth``. This has been
+ deprecated since the 1.2 release.
+
* 2.0
* ``django.views.defaults.shortcut()``. This function has been moved
to ``django.contrib.contenttypes.views.shortcut()`` as part of the
View
@@ -1367,7 +1367,7 @@ TEMPLATE_CONTEXT_PROCESSORS
Default::
- ("django.core.context_processors.auth",
+ ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
@@ -308,7 +308,7 @@ called **context processors** -- that take a request object as their argument
and return a dictionary of items to be merged into the context. By default,
:setting:`TEMPLATE_CONTEXT_PROCESSORS` is set to::
- ("django.core.context_processors.auth",
+ ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
@@ -360,8 +360,8 @@ optional, third positional argument, ``processors``. In this example, the
Here's what each of the default processors does:
-django.core.context_processors.auth
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+django.contrib.auth.context_processors.auth
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every
``RequestContext`` will contain these three variables:
View
@@ -1236,8 +1236,8 @@ The currently logged-in user and his/her permissions are made available in the
Technically, these variables are only made available in the template context
if you use :class:`~django.template.context.RequestContext` *and* your
:setting:`TEMPLATE_CONTEXT_PROCESSORS` setting contains
- ``"django.core.context_processors.auth"``, which is default. For more, see
- the :ref:`RequestContext docs <subclassing-context-requestcontext>`.
+ ``"django.contrib.auth.context_processors.auth"``, which is default. For
+ more, see the :ref:`RequestContext docs <subclassing-context-requestcontext>`.
Users
-----
@@ -41,7 +41,7 @@ def test_request_attributes(self):
class AuthContextProcessorTests(TestCase):
"""
- Tests for the ``django.core.context_processors.auth`` processor
+ Tests for the ``django.contrib.auth.context_processors.auth`` processor
"""
urls = 'regressiontests.context_processors.urls'
fixtures = ['context-processors-users.xml']

0 comments on commit 67d4289

Please sign in to comment.