Commit 25020ddb authored by Luke Plant's avatar Luke Plant
Browse files

Fixed #4604 - Configurable message passing system, supporting anonymous users

This deprecates User.message_set in favour of a configurable messaging
system, with backends provided for cookie storage, session storage and
backward compatibility.

Many thanks to Tobias McNulty for the bulk of the work here, with
contributions from Chris Beaven (SmileyChris) and lots of code review from
Russell Keith-Magee, and input from many others.  Also credit to the authors
of various messaging systems for Django whose ideas may have been pinched
:-)



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11804 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent eeb10d5f
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ answer newbie questions, and generally made Django that much better:
    Ned Batchelder <http://www.nedbatchelder.com/>
    batiste@dosimple.ch
    Batman
    Chris Beaven <http://smileychris.tactful.co.nz/>
    Brian Beck <http://blog.brianbeck.com/>
    Shannon -jj Behrens <http://jjinux.blogspot.com/>
    Esdras Beleza <linux@esdrasbeleza.com>
@@ -299,6 +300,7 @@ answer newbie questions, and generally made Django that much better:
    Jason McBrayer <http://www.carcosa.net/jason/>
    Kevin McConnell <kevin.mcconnell@gmail.com>
    mccutchen@gmail.com
    Tobias McNulty <http://www.caktusgroup.com/blog>
    Christian Metts
    michael.mcewan@gmail.com
    michal@plovarna.cz
@@ -391,7 +393,6 @@ answer newbie questions, and generally made Django that much better:
    Jozko Skrablin <jozko.skrablin@gmail.com>
    Ben Slavin <benjamin.slavin@gmail.com>
    sloonz <simon.lipp@insa-lyon.fr>
    SmileyChris <smileychris@gmail.com>
    Warren Smith <warren@wandrsmith.net>
    smurf@smurf.noris.de
    Vsevolod Solovyov
+12 −0
Original line number Diff line number Diff line
@@ -172,6 +172,7 @@ TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
#    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
)

# Output to use in template system for invalid (e.g. misspelled) variables.
@@ -308,6 +309,7 @@ MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
#     'django.middleware.http.ConditionalGetMiddleware',
#     'django.middleware.gzip.GZipMiddleware',
)
@@ -393,6 +395,16 @@ CSRF_FAILURE_VIEW = 'django.views.csrf.csrf_failure'
CSRF_COOKIE_NAME = 'csrftoken'
CSRF_COOKIE_DOMAIN = None

############
# MESSAGES #
############

# Class to use as messges backend
MESSAGE_STORAGE = 'django.contrib.messages.storage.user_messages.LegacyFallbackStorage'

# Default values of MESSAGE_LEVEL and MESSAGE_TAGS are defined within
# django.contrib.messages to avoid imports in this settings file.

###########
# TESTING #
###########
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = '{{ project_name }}.urls'
@@ -77,4 +78,5 @@ INSTALLED_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
)
+3 −2
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.admin import widgets
from django.contrib.admin import helpers
from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_objects, model_ngettext, model_format_dict
from django.contrib import messages
from django.views.decorators.csrf import csrf_protect
from django.core.exceptions import PermissionDenied
from django.db import models, transaction
@@ -541,9 +542,9 @@ class ModelAdmin(BaseModelAdmin):
    def message_user(self, request, message):
        """
        Send a message to the user. The default implementation
        posts a message using the auth Message object.
        posts a message using the django.contrib.messages backend.
        """
        request.user.message_set.create(message=message)
        messages.info(request, message)

    def save_form(self, request, form, change):
        """
+2 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from django.contrib.sites.models import Site
from django.conf import settings
from django.utils.importlib import import_module
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages


def template_validator(request):
@@ -23,7 +24,7 @@ def template_validator(request):
        form = TemplateValidatorForm(settings_modules, site_list,
                                     data=request.POST)
        if form.is_valid():
            request.user.message_set.create(message='The template is valid.')
            messages.info(request, 'The template is valid.')
    else:
        form = TemplateValidatorForm(settings_modules, site_list)
    return render_to_response('admin/template_validator.html', {
Loading