Ticket #13708: admin_improve_save_model.patch

File admin_improve_save_model.patch, 5.5 KB (added by Sebastian Noack, 14 years ago)
  • django/django/contrib/admin/actions.py

    diff --git a/django/django/contrib/admin/actions.py b/django/django/contrib/admin/actions.py
    index 86edb0a..29fe28c 100644
    a b def delete_selected(modeladmin, request, queryset):  
    5151    if request.POST.get('post'):
    5252        if perms_needed:
    5353            raise PermissionDenied
    54         n = queryset.count()
    55         if n:
    56             for obj in queryset:
    57                 obj_display = force_unicode(obj)
     54
     55        n = 0
     56        for obj in queryset:
     57            obj_display = force_unicode(obj)
     58            if modeladmin.delete_model(request, obj):
    5859                modeladmin.log_deletion(request, obj, obj_display)
    59             queryset.delete()
     60                n += 1
     61
     62        if n > 0:
    6063            modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
    6164                "count": n, "items": model_ngettext(modeladmin.opts, n)
    6265            })
     66
    6367        # Return None to display the change list page again.
    6468        return None
    6569
  • django/django/contrib/admin/options.py

    diff --git a/django/django/contrib/admin/options.py b/django/django/contrib/admin/options.py
    index d83b4aa..4638f7e 100644
    a b class ModelAdmin(BaseModelAdmin):  
    554554
    555555    def save_model(self, request, obj, form, change):
    556556        """
    557         Given a model instance save it to the database.
     557        Given a model instance save it to the database. Returns True to
     558        indicate that the instance was saved successful. Derived classes can
     559        override this method an let it return False to indicate, that the
     560        instance was not saved for some reasons. You must call message_user()
     561        yourself, to notify the user about the failure and its reason, if you
     562        return False.
    558563        """
    559564        obj.save()
     565        return True
     566
     567    def delete_model(self, request, obj):
     568        """
     569        Given a model instance delete it from the database. Returns True to
     570        indicate that the instance was saved successful. Derived classes can
     571        override this method an let it return False to indicate, that the
     572        instance was not deleted for some reasons. You must call message_user()
     573        yourself, to notify the user about the failure and its reason, if you
     574        return False.
     575        """
     576        obj.delete()
     577        return True
    560578
    561579    def save_formset(self, request, form, formset, change):
    562580        """
    class ModelAdmin(BaseModelAdmin):  
    733751                                  save_as_new=request.POST.has_key("_saveasnew"),
    734752                                  prefix=prefix)
    735753                formsets.append(formset)
    736             if all_valid(formsets) and form_validated:
    737                 self.save_model(request, new_object, form, change=False)
     754            if all_valid(formsets) and form_validated and self.save_model(request, new_object, form, change=False):
    738755                form.save_m2m()
    739756                for formset in formsets:
    740757                    self.save_formset(request, form, formset, change=False)
    class ModelAdmin(BaseModelAdmin):  
    829846                                  instance=new_object, prefix=prefix)
    830847                formsets.append(formset)
    831848
    832             if all_valid(formsets) and form_validated:
    833                 self.save_model(request, new_object, form, change=True)
     849            if all_valid(formsets) and form_validated and self.save_model(request, new_object, form, change=True):
    834850                form.save_m2m()
    835851                for formset in formsets:
    836852                    self.save_formset(request, form, formset, change=True)
    class ModelAdmin(BaseModelAdmin):  
    930946                for form in formset.forms:
    931947                    if form.has_changed():
    932948                        obj = self.save_form(request, form, change=True)
    933                         self.save_model(request, obj, form, change=True)
    934                         form.save_m2m()
    935                         change_msg = self.construct_change_message(request, form, None)
    936                         self.log_change(request, obj, change_msg)
    937                         changecount += 1
     949                        if self.save_model(request, obj, form, change=True):
     950                            form.save_m2m()
     951                            change_msg = self.construct_change_message(request, form, None)
     952                            self.log_change(request, obj, change_msg)
     953                            changecount += 1
    938954
    939955                if changecount:
    940956                    if changecount == 1:
    class ModelAdmin(BaseModelAdmin):  
    10171033            if perms_needed:
    10181034                raise PermissionDenied
    10191035            obj_display = force_unicode(obj)
    1020             self.log_deletion(request, obj, obj_display)
    1021             obj.delete()
    10221036
    1023             self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj_display)})
     1037            if self.delete_model(request, obj):
     1038                self.log_deletion(request, obj, obj_display)
     1039                self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': obj_display})
     1040            elif self.has_change_permission(request, obj):
     1041                return HttpResponseRedirect("../")
    10241042
    10251043            if not self.has_change_permission(request, None):
    10261044                return HttpResponseRedirect("../../../../")
Back to Top