Ticket #17818: class-based-generic-edit-views-messages-v1.diff

File class-based-generic-edit-views-messages-v1.diff, 4.6 KB (added by charettes, 4 years ago)
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 3cade52..4ff03cc 100644
    a b  
    11from django.forms import models as model_forms
     2from django.contrib import messages
    23from django.core.exceptions import ImproperlyConfigured
    34from django.http import HttpResponseRedirect
     5from django.utils.translation import ugettext_lazy as _
    46from django.views.generic.base import TemplateResponseMixin, View
    57from django.views.generic.detail import (SingleObjectMixin,
    68                        SingleObjectTemplateResponseMixin, BaseDetailView)
    class FormView(TemplateResponseMixin, BaseFormView): 
    157159    """
    158160
    159161
    160 class BaseCreateView(ModelFormMixin, ProcessFormView):
     162class SuccessMessageMixin(object):
     163    """
     164    A mixin providing the ability to create success messages.
     165    Message creation can be disabled by setting success_message=None.
     166    """
     167    def get_success_message(self, **kwargs):
     168        return self.success_message % kwargs
     169   
     170    def create_success_message(self, request, **kwargs):
     171        try:
     172            msg = self.get_success_message(**kwargs)
     173        except AttributeError:
     174            raise ImproperlyConfigured('`SuccessMessageMixin` subclasses must define a '
     175                                       '`success_message` or override `get_success_message`')
     176        except TypeError:
     177            if self.success_message is not None:
     178                raise
     179        else:
     180            messages.success(request, msg, fail_silently=True)
     181
     182
     183class BaseCreateView(ModelFormMixin, ProcessFormView, SuccessMessageMixin):
    161184    """
    162185    Base view for creating an new object instance.
    163186
    164187    Using this base class requires subclassing to provide a response mixin.
    165188    """
     189    success_message = _("The %(verbose_name)s was created successfully.")
     190   
    166191    def get(self, request, *args, **kwargs):
    167192        self.object = None
    168193        return super(BaseCreateView, self).get(request, *args, **kwargs)
    def get(self, request, *args, **kwargs): 
    170195    def post(self, request, *args, **kwargs):
    171196        self.object = None
    172197        return super(BaseCreateView, self).post(request, *args, **kwargs)
     198   
     199    def form_valid(self, form):
     200        form_valid = super(BaseCreateView, self).form_valid(form)
     201        self.create_success_message(self.request, object=self.object,
     202                                    verbose_name=self.object._meta.verbose_name)
     203        return form_valid
    173204
    174205
    175206class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
    class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView): 
    180211    template_name_suffix = '_form'
    181212
    182213
    183 class BaseUpdateView(ModelFormMixin, ProcessFormView):
     214class BaseUpdateView(ModelFormMixin, ProcessFormView, SuccessMessageMixin):
    184215    """
    185216    Base view for updating an existing object.
    186217
    187218    Using this base class requires subclassing to provide a response mixin.
    188219    """
     220    success_message = _("The %(verbose_name)s was updated successfully.")
     221   
    189222    def get(self, request, *args, **kwargs):
    190223        self.object = self.get_object()
    191224        return super(BaseUpdateView, self).get(request, *args, **kwargs)
    def get(self, request, *args, **kwargs): 
    193226    def post(self, request, *args, **kwargs):
    194227        self.object = self.get_object()
    195228        return super(BaseUpdateView, self).post(request, *args, **kwargs)
     229   
     230    def form_valid(self, form):
     231        form_valid = super(BaseUpdateView, self).form_valid(form)
     232        self.create_success_message(self.request, object=self.object,
     233                                    verbose_name=self.object._meta.verbose_name)
     234        return form_valid
    196235
    197236
    198237class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
    def get_success_url(self): 
    226265                "No URL to redirect to. Provide a success_url.")
    227266
    228267
    229 class BaseDeleteView(DeletionMixin, BaseDetailView):
     268class BaseDeleteView(DeletionMixin, BaseDetailView, SuccessMessageMixin):
    230269    """
    231270    Base view for deleting an object.
    232271
    233272    Using this base class requires subclassing to provide a response mixin.
    234273    """
     274    success_message = _("The %(verbose_name)s was deleted.")
     275   
     276    def delete(self, request, *args, **kwargs):
     277        response = super(BaseDeleteView, self).delete(request, *args, **kwargs)
     278        self.create_success_message(request, object=self.object,
     279                                    verbose_name=self.object._meta.verbose_name)
     280        return response
    235281
    236282
    237283class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
Back to Top