Override builting delete_selected action with one that does notifies
authorMagnus Hagander <magnus@hagander.net>
Tue, 26 Jun 2012 13:14:52 +0000 (15:14 +0200)
committerMagnus Hagander <magnus@hagander.net>
Tue, 26 Jun 2012 13:14:52 +0000 (15:14 +0200)
This should fix the problem where notifications weren't sent for objects
being deleted through the "mass deletion" function in the list of objects.

Closes #121

pgweb/util/admin.py

index b462a394aa2abe63235227b5ccb8ce23b1e45631..bb84a9872f8e28ed84d086c2fc8cc4f38ec6a6b9 100644 (file)
@@ -36,6 +36,25 @@ class PgwebAdmin(admin.ModelAdmin):
 
                return super(PgwebAdmin, self).change_view(request, object_id, extra_context)
 
+       # Remove the builtin delete_selected action, so it doesn't
+       # conflict with the custom one.
+       def get_actions(self, request):
+               actions = super(PgwebAdmin, self).get_actions(request)
+               del actions['delete_selected']
+               return actions
+
+       # Define a custom delete_selected action. This is required because the
+       # default one uses the delete functionality in QuerySet, which bypasses
+       # the delete() operation on the model, and thus won't send out our
+       # notifications. Manually calling delete() on each one will be slightly
+       # slower, but will send proper notifications - and it's not like this
+       # is something that happens often enough that we care about performance.
+       def custom_delete_selected(self, request, queryset):
+               for x in queryset:
+                       x.delete()
+       custom_delete_selected.short_description = "Delete selected items"
+       actions=['custom_delete_selected']
+
        def save_model(self, request, obj, form, change):
                if change and self.model.send_notification:
                        # We only do processing if something changed, not when adding