diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 59d50fbde9..8f24d2b3fa 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -183,7 +183,7 @@ TEMPLATE_LOADERS = ( # 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', diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 6989dcf55e..ac8835529e 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -155,11 +155,15 @@ class AdminSite(object): 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): """ diff --git a/django/contrib/auth/context_processors.py b/django/contrib/auth/context_processors.py new file mode 100644 index 0000000000..87d8331ee6 --- /dev/null +++ b/django/contrib/auth/context_processors.py @@ -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)(), + } diff --git a/django/core/context_processors.py b/django/core/context_processors.py index eedf601217..a5f29dfb2d 100644 --- a/django/core/context_processors.py +++ b/django/core/context_processors.py @@ -9,38 +9,25 @@ RequestContext. 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): """ diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index f2ae31d4be..a56b522278 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -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 diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 993232f1d9..8bdf395fc8 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -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", diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index 7582f8b435..97f6431ba4 100644 --- a/docs/ref/templates/api.txt +++ b/docs/ref/templates/api.txt @@ -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: diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt index 182453f089..1c52ab674e 100644 --- a/docs/topics/auth.txt +++ b/docs/topics/auth.txt @@ -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 `. + ``"django.contrib.auth.context_processors.auth"``, which is default. For + more, see the :ref:`RequestContext docs `. Users ----- diff --git a/tests/regressiontests/context_processors/tests.py b/tests/regressiontests/context_processors/tests.py index 004f90eceb..0d19bef7ff 100644 --- a/tests/regressiontests/context_processors/tests.py +++ b/tests/regressiontests/context_processors/tests.py @@ -41,7 +41,7 @@ class RequestContextProcessorTests(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' fixtures = ['context-processors-users.xml']