Code

Ticket #16319: SuccessMessageMixin.diff

File SuccessMessageMixin.diff, 934 bytes (added by martinogden, 3 years ago)

Add SuccessMessageMixin to django.contrib.messages.views

Line 
1Index: django/contrib/messages/views.py
2===================================================================
3--- django/contrib/messages/views.py    (revision 0)
4+++ django/contrib/messages/views.py    (revision 0)
5@@ -0,0 +1,21 @@
6+from django.core.exceptions import ImproperlyConfigured
7+from django.views.generic import FormMixin
8+from django.contrib import messages
9+
10+
11+class SuccessMessageMixin(FormMixin):
12+    """
13+    A mixin that add a success message when a form is completed
14+    """
15+    success_message = None
16+
17+    def is_valid(self, form):
18+        success_message = self.get_success_message()
19+        if success_message:
20+            messages.success(self.request, success_message)
21+        return super(SuccessMessageMixin, self).is_valid(form)
22+
23+    def get_success_message(self):
24+        if has_attr(self, 'object'):
25+            return self.success_message % self.object.__dict__
26+        return self.success_message