Fixed #15757 - removed remaining instances of get_and_delete_messages

Thanks to void for the report, and julien for the bulk of the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16022 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-04-07 22:01:23 +00:00
parent b7715b4ae6
commit 8d4b414760
8 changed files with 30 additions and 117 deletions

View File

@ -1,5 +1,4 @@
from django.utils.functional import lazy, memoize, SimpleLazyObject
from django.contrib import messages
# PermWrapper and PermLookupDict proxy the permissions system into objects that
# the template system can understand.
@ -55,6 +54,5 @@ def auth(request):
return {
'user': SimpleLazyObject(get_user),
'messages': messages.get_messages(request),
'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
}

View File

@ -445,9 +445,6 @@ class AnonymousUser(object):
def has_module_perms(self, module):
return _user_has_module_perms(self, module)
def get_and_delete_messages(self):
return []
def is_anonymous(self):
return True

View File

@ -15,33 +15,24 @@ class MessageFailure(Exception):
def add_message(request, level, message, extra_tags='', fail_silently=False):
"""
Attempts to add a message to the request using the 'messages' app, falling
back to the user's message_set if MessageMiddleware hasn't been enabled.
Attempts to add a message to the request using the 'messages' app.
"""
if hasattr(request, '_messages'):
return request._messages.add(level, message, extra_tags)
if not fail_silently:
raise MessageFailure('Without the django.contrib.messages '
'middleware, messages can only be added to '
'authenticated users.')
raise MessageFailure('You cannot add messages without installing '
'django.contrib.messages.middleware.MessageMiddleware')
def get_messages(request):
"""
Returns the message storage on the request if it exists, otherwise returns
user.message_set.all() as the old auth context processor did.
an empty list.
"""
if hasattr(request, '_messages'):
return request._messages
def get_user():
if hasattr(request, 'user'):
return request.user
else:
from django.contrib.auth.models import AnonymousUser
return AnonymousUser()
return lazy(memoize(get_user().get_and_delete_messages, {}, 0), list)()
else:
return []
def get_level(request):

View File

@ -10,7 +10,6 @@ from django.contrib.messages.api import MessageFailure
from django.contrib.messages.storage import default_storage, base
from django.contrib.messages.storage.base import Message
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
def skipUnlessAuthIsInstalled(func):
@ -222,10 +221,10 @@ class BaseTest(TestCase):
for msg in data['messages']:
self.assertContains(response, msg)
def test_middleware_disabled_anon_user(self):
def test_middleware_disabled(self):
"""
Tests that, when the middleware is disabled and a user is not logged
in, an exception is raised when one attempts to store a message.
Tests that, when the middleware is disabled, an exception is raised
when one attempts to store a message.
"""
settings.MESSAGE_LEVEL = constants.DEBUG
settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)
@ -251,10 +250,10 @@ class BaseTest(TestCase):
self.assertRaises(MessageFailure, self.client.post, add_url,
data, follow=True)
def test_middleware_disabled_anon_user_fail_silently(self):
def test_middleware_disabled_fail_silently(self):
"""
Tests that, when the middleware is disabled and a user is not logged
in, an exception is not raised if 'fail_silently' = True
Tests that, when the middleware is disabled, an exception is not
raised if 'fail_silently' = True
"""
settings.MESSAGE_LEVEL = constants.DEBUG
settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)

View File

@ -52,7 +52,10 @@ their deprecation, as per the :ref:`Django deprecation policy
``user.get_and_delete_messages()``), which have
been deprecated since the 1.2 release, will be removed. The
:doc:`messages framework </ref/contrib/messages>` should be used
instead.
instead. The related ``messages`` variable returned by the
auth context processor will also be removed. Note that this
means that the admin application depends on the messages
context processor.
* Authentication backends need to support the ``obj`` parameter for
permission checking. The ``supports_object_permissions`` variable

View File

@ -14,26 +14,32 @@ Django's admin interface.
Overview
========
There are six steps in activating the Django admin site:
There are seven steps in activating the Django admin site:
1. Add ``'django.contrib.admin'`` to your :setting:`INSTALLED_APPS`
setting.
2. Admin has two dependencies - :mod:`django.contrib.auth` and
:mod:`django.contrib.contenttypes`. If these applications are not
in your :setting:`INSTALLED_APPS` list, add them.
2. Admin has three dependencies - :mod:`django.contrib.auth`,
:mod:`django.contrib.contenttypes` and :mod:`django.contrib.messages`.
If these applications are not in your :setting:`INSTALLED_APPS` list,
add them.
3. Determine which of your application's models should be editable in the
3. Add ``django.contrib.messages.context_processors.messages`` to
:setting:`TEMPLATE_CONTEXT_PROCESSORS` and
:class:`~django.contrib.messages.middleware.MessageMiddleware` to
:setting:`MIDDLEWARE_CLASSES`.
4. Determine which of your application's models should be editable in the
admin interface.
4. For each of those models, optionally create a ``ModelAdmin`` class that
5. For each of those models, optionally create a ``ModelAdmin`` class that
encapsulates the customized admin functionality and options for that
particular model.
5. Instantiate an ``AdminSite`` and tell it about each of your models and
6. Instantiate an ``AdminSite`` and tell it about each of your models and
``ModelAdmin`` classes.
6. Hook the ``AdminSite`` instance into your URLconf.
7. Hook the ``AdminSite`` instance into your URLconf.
Other topics
------------

View File

@ -419,9 +419,6 @@ If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every
logged-in user (or an ``AnonymousUser`` instance, if the client isn't
logged in).
* ``messages`` -- A list of messages (as strings) that have been set
via the :doc:`messages framework </ref/contrib/messages>`.
* ``perms`` -- An instance of
``django.contrib.auth.context_processors.PermWrapper``, representing the
permissions that the currently logged-in user has.
@ -430,11 +427,6 @@ If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every
This context processor was moved in this release from
``django.core.context_processors.auth`` to its current location.
.. versionchanged:: 1.2
Prior to version 1.2, the ``messages`` variable was a lazy accessor for
``user.get_and_delete_messages()``. It has been changed to include any
messages added via the :doc:`messages framework </ref/contrib/messages>`.
.. versionchanged:: 1.3
Prior to version 1.3, ``PermWrapper`` was located in
``django.contrib.auth.context_processors``.

View File

@ -19,10 +19,6 @@ The auth system consists of:
a certain task.
* Groups: A generic way of applying labels and permissions to more than one
user.
* Messages: A simple way to queue messages for given users.
.. deprecated:: 1.2
The Messages component of the auth system will be removed in Django 1.4.
Installation
============
@ -256,11 +252,6 @@ Methods
(the Django app label). If the user is inactive, this method will
always return ``False``.
.. method:: models.User.get_and_delete_messages()
Returns a list of :class:`~django.contrib.auth.models.Message` objects
in the user's queue and deletes the messages from the queue.
.. method:: models.User.email_user(subject, message, from_email=None)
Sends an email to the user. If
@ -1387,70 +1378,6 @@ group ``'Special users'``, and you could write code that could, say, give them
access to a members-only portion of your site, or send them members-only email
messages.
Messages
========
.. deprecated:: 1.2
This functionality will be removed in Django 1.4. You should use the
:doc:`messages framework </ref/contrib/messages>` for all new projects and
begin to update your existing code immediately.
The message system is a lightweight way to queue messages for given users.
A message is associated with a :class:`~django.contrib.auth.models.User`.
There's no concept of expiration or timestamps.
Messages are used by the Django admin after successful actions. For example,
``"The poll Foo was created successfully."`` is a message.
The API is simple:
.. method:: models.User.message_set.create(message)
To create a new message, use
``user_obj.message_set.create(message='message_text')``.
To retrieve/delete messages, use
:meth:`user_obj.get_and_delete_messages() <django.contrib.auth.models.User.get_and_delete_messages>`,
which returns a list of ``Message`` objects in the user's queue (if any)
and deletes the messages from the queue.
In this example view, the system saves a message for the user after creating
a playlist::
def create_playlist(request, songs):
# Create the playlist with the given songs.
# ...
request.user.message_set.create(message="Your playlist was added successfully.")
return render_to_response("playlists/create.html",
context_instance=RequestContext(request))
When you use :class:`~django.template.context.RequestContext`, the currently
logged-in user and his/her messages are made available in the
:doc:`template context </ref/templates/api>` as the template variable
``{{ messages }}``. Here's an example of template code that displays messages:
.. code-block:: html+django
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
.. versionchanged:: 1.2
The ``messages`` template variable uses a backwards compatible method in the
:doc:`messages framework </ref/contrib/messages>` to retrieve messages from
both the user ``Message`` model and from the new framework. Unlike in
previous revisions, the messages will not be erased unless they are actually
displayed.
Finally, note that this messages framework only works with users in the user
database. To send messages to anonymous users, use the
:doc:`messages framework </ref/contrib/messages>`.
.. _authentication-backends:
Other authentication sources