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
This commit is contained in:
Jannis Leidel 2010-02-21 23:40:47 +00:00
parent 1559f64549
commit 67d4289c2e
9 changed files with 68 additions and 39 deletions

View File

@ -183,7 +183,7 @@ TEMPLATE_LOADERS = (
# Each one should be a callable that takes the request object as its # Each one should be a callable that takes the request object as its
# only parameter and returns a dictionary to add to the context. # only parameter and returns a dictionary to add to the context.
TEMPLATE_CONTEXT_PROCESSORS = ( TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug', 'django.core.context_processors.debug',
'django.core.context_processors.i18n', 'django.core.context_processors.i18n',
'django.core.context_processors.media', 'django.core.context_processors.media',

View File

@ -155,11 +155,15 @@ class AdminSite(object):
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
if not LogEntry._meta.installed: 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: if not ContentType._meta.installed:
raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in your INSTALLED_APPS setting in order to use the admin application.") raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
if 'django.core.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS: "your INSTALLED_APPS setting in order to use the admin application.")
raise ImproperlyConfigured("Put 'django.core.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS 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): def admin_view(self, view, cacheable=False):
""" """

View File

@ -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)(),
}

View File

@ -9,38 +9,25 @@ RequestContext.
from django.conf import settings from django.conf import settings
from django.middleware.csrf import get_token from django.middleware.csrf import get_token
from django.utils.functional import lazy, memoize, SimpleLazyObject from django.utils.functional import lazy
from django.contrib import messages
def auth(request): def auth(request):
""" """
Returns context variables required by apps that use Django's authentication DEPRECATED. This context processor is the old location, and has been moved
system. to `django.contrib.auth.context_processors`.
If there is no 'user' attribute in the request, uses AnonymousUser (from This function still exists for backwards-compatibility; it will be removed
django.contrib.auth). in Django 1.4.
""" """
# If we access request.user, request.session is accessed, which results in import warnings
# 'Vary: Cookie' being sent in every request that uses this context warnings.warn(
# processor, which can easily be every request on a site if "The context processor at `django.core.context_processors.auth` is " \
# TEMPLATE_CONTEXT_PROCESSORS has this context processor added. This kills "deprecated; use the path `django.contrib.auth.context_processors.auth` " \
# the ability to cache. So, we carefully ensure these attributes are lazy. "instead.",
# We don't use django.utils.functional.lazy() for User, because that PendingDeprecationWarning
# 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 from django.contrib.auth.context_processors import auth as auth_context_processor
# flexible solution that is a good enough wrapper for 'User'. return auth_context_processor(request)
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)(),
}
def csrf(request): def csrf(request):
""" """

View File

@ -86,6 +86,11 @@ their deprecation, as per the :ref:`Django deprecation policy
``django.contrib.syndication`` have been deprecated since the 1.2 ``django.contrib.syndication`` have been deprecated since the 1.2
release. The class-based view ``views.Feed`` should be used instead. 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 * 2.0
* ``django.views.defaults.shortcut()``. This function has been moved * ``django.views.defaults.shortcut()``. This function has been moved
to ``django.contrib.contenttypes.views.shortcut()`` as part of the to ``django.contrib.contenttypes.views.shortcut()`` as part of the

View File

@ -1367,7 +1367,7 @@ TEMPLATE_CONTEXT_PROCESSORS
Default:: Default::
("django.core.context_processors.auth", ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug", "django.core.context_processors.debug",
"django.core.context_processors.i18n", "django.core.context_processors.i18n",
"django.core.context_processors.media", "django.core.context_processors.media",

View File

@ -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, and return a dictionary of items to be merged into the context. By default,
:setting:`TEMPLATE_CONTEXT_PROCESSORS` is set to:: :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.debug",
"django.core.context_processors.i18n", "django.core.context_processors.i18n",
"django.core.context_processors.media", "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: 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 If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every
``RequestContext`` will contain these three variables: ``RequestContext`` will contain these three variables:

View File

@ -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 Technically, these variables are only made available in the template context
if you use :class:`~django.template.context.RequestContext` *and* your if you use :class:`~django.template.context.RequestContext` *and* your
:setting:`TEMPLATE_CONTEXT_PROCESSORS` setting contains :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting contains
``"django.core.context_processors.auth"``, which is default. For more, see ``"django.contrib.auth.context_processors.auth"``, which is default. For
the :ref:`RequestContext docs <subclassing-context-requestcontext>`. more, see the :ref:`RequestContext docs <subclassing-context-requestcontext>`.
Users Users
----- -----

View File

@ -41,7 +41,7 @@ class RequestContextProcessorTests(TestCase):
class AuthContextProcessorTests(TestCase): 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' urls = 'regressiontests.context_processors.urls'
fixtures = ['context-processors-users.xml'] fixtures = ['context-processors-users.xml']