Ticket #16319: ticket_16319.patch

File ticket_16319.patch, 5.8 KB (added by Roman Gladkov, 12 years ago)
  • new file django/contrib/messages/views.py

    diff --git a/django/contrib/messages/views.py b/django/contrib/messages/views.py
    new file mode 100644
    index 0000000..219c524
    - +  
     1from django.views.generic.edit import FormMixin
     2from django.contrib import messages
     3
     4
     5class SuccessMessageMixin(FormMixin):
     6    """
     7    A mixin that add a success message when a form is completed
     8    """
     9    success_message = None
     10
     11    def form_valid(self, form):
     12        success_message = self.get_success_message()
     13        if success_message:
     14            messages.success(self.request, success_message)
     15        return super(SuccessMessageMixin, self).form_valid(form)
     16
     17    def get_success_message(self):
     18        if hasattr(self, 'object'):
     19            return self.success_message % self.object.__dict__
     20        return self.success_message
  • docs/ref/contrib/messages.txt

    diff --git a/docs/ref/contrib/messages.txt b/docs/ref/contrib/messages.txt
    index bc921a9..d155a81 100644
    a b example::  
    280280   use one of the ``add_message`` family of methods. It does not hide failures
    281281   that may occur for other reasons.
    282282
     283Adding a messages in CBV
     284------------------------
     285
     286If you're writing a view for processing a form and want to add message
     287about successful checking, you can use ``'django.contrib.messages.views.SuccessMessageMixin'`` for it.
     288For example::
     289
     290    # views.py
     291    from django.contrib.messages.views import SuccessMessageMixin
     292    from django.views.generic.edit import CreateView
     293    from myapp.models import Author
     294
     295
     296    class AuthorCreate(CreateView, SuccessMessageMixin):
     297        model = Author
     298        success_url = '/success/'
     299        success_message = "%(name)s was created successfully"
     300
     301.. note::
     302    ``success_message`` is supported printing only fields what is contains in ``model``.
     303
    283304Expiration of messages
    284305======================
    285306
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 16f4da8..45638d3 100644
    a b class FormMixinTests(TestCase):  
    2020         initial_2 = FormMixin().get_initial()
    2121         self.assertNotEqual(initial_1, initial_2)
    2222
     23
    2324class ModelFormMixinTests(TestCase):
    2425    def test_get_form(self):
    2526        form_class = views.AuthorGetQuerySetFormView().get_form_class()
    2627        self.assertEqual(form_class._meta.model, Author)
    2728
     29
     30class SuccessMessageMixinTests(TestCase):
     31    urls = 'regressiontests.generic_views.urls'
     32
     33    def test_set_messages_success(self):
     34        req = self.client.post('/edit/authors/create/msg/',
     35                               {'name': 'John Doe',
     36                                'slug': 'success-msg'})
     37        self.assertIn('John Doe', req.cookies['messages'].value)
     38
     39    def test_set_message_false(self):
     40        req = self.client.post('/edit/authors/create/msg/',
     41                               {'name': 'John Doe'})
     42        self.assertFalse('messages' in req.cookies)
     43
     44
    2845class CreateViewTests(TestCase):
    2946    urls = 'regressiontests.generic_views.urls'
    3047
    class DeleteViewTests(TestCase):  
    298315            self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
    299316        except ImproperlyConfigured:
    300317            pass
    301 
  • tests/regressiontests/generic_views/tests.py

    diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
    index c985ad3..0bc4233 100644
    a b  
    11from __future__ import absolute_import
    22
    33from .base import (ViewTest, TemplateViewTest, RedirectViewTest,
    4     GetContextDataTest)
     4                   GetContextDataTest)
    55from .dates import (ArchiveIndexViewTests, YearArchiveViewTests,
    6     MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
    7     DateDetailViewTests)
     6                    MonthArchiveViewTests, WeekArchiveViewTests,
     7                    DayArchiveViewTests, DateDetailViewTests)
    88from .detail import DetailViewTest
    99from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests,
    10     UpdateViewTests, DeleteViewTests)
     10                   UpdateViewTests, DeleteViewTests, SuccessMessageMixinTests)
    1111from .list import ListViewTests
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index c72bfec..300f8ec 100644
    a b urlpatterns = patterns('',  
    7272        views.AuthorCreateRestricted.as_view()),
    7373    (r'^edit/authors/create/$',
    7474        views.AuthorCreate.as_view()),
     75    (r'^edit/authors/create/msg/$',
     76        views.AuthorCreateViewWithMsg.as_view()),
    7577    (r'^edit/authors/create/special/$',
    7678        views.SpecializedAuthorCreate.as_view()),
    7779
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index f7fcf6f..7613f24 100644
    a b  
    11from __future__ import absolute_import
    22
    33from django.contrib.auth.decorators import login_required
     4from django.contrib.messages.views import SuccessMessageMixin
    45from django.core.paginator import Paginator
    56from django.core.urlresolvers import reverse
    67from django.utils.decorators import method_decorator
    class AuthorCreateRestricted(AuthorCreate):  
    102103    post = method_decorator(login_required)(AuthorCreate.post)
    103104
    104105
     106class AuthorCreateViewWithMsg(generic.CreateView, SuccessMessageMixin):
     107    model = Author
     108    success_url = '/next/'
     109    success_message = "%(name)s was created successfully"
     110
     111
    105112class ArtistUpdate(generic.UpdateView):
    106113    model = Artist
    107114
Back to Top