From fd5df3b35037939db3fb8ecd25f9dc324222c251 Mon Sep 17 00:00:00 2001 From: Viliam Balaz Date: Tue, 29 Jun 2021 13:14:41 +0200 Subject: [PATCH] #334 Allow admin bulk delete on Branch model (#411) --- chcemvediet/apps/inforequests/admin.py | 25 +++++++++++++++++ .../branch/delete_selected_confirmation.html | 27 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 0a30c0cc..06a930bd 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -309,6 +309,9 @@ class BranchAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin): inlines = [ ActionInline, ] + actions = [ + u'delete_selected' + ] def get_queryset(self, request): queryset = super(BranchAdmin, self).get_queryset(request) @@ -328,6 +331,28 @@ def render_delete_form(self, request, context): context[u'delete_constraints'] = self.delete_constraints([context[u'object']]) return super(BranchAdmin, self).render_delete_form(request, context) + @decorate(short_description=u'Delete selected branches') + @transaction.atomic + def delete_selected(self, request, queryset): + outbound, inbound = self.nested_inforequestemail_queryset(queryset) + if request.POST.get(u'post'): + if self.delete_constraints(queryset): + raise PermissionDenied + + template_response = delete_selected(self, request, queryset) + + if request.POST.get(u'post'): + outbound.delete() + inbound.update(type=InforequestEmail.TYPES.UNDECIDED) + return None + + template_response.context_data.update({ + u'outbound': [admin_obj_format(inforequestemail) for inforequestemail in outbound], + u'inbound': [admin_obj_format(inforequestemail) for inforequestemail in inbound], + u'delete_constraints': self.delete_constraints(queryset), + }) + return template_response + def delete_model(self, request, obj): if self.delete_constraints([obj]): raise PermissionDenied diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html new file mode 100644 index 00000000..1997ccfb --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html @@ -0,0 +1,27 @@ +{% extends "admin/delete_selected_confirmation.html" %} +{% load amend before after set_attributes from poleno.amend %} + +{% block content %} + {% amend %} + {{ block.super }} + {% after path="./ul[last()]" %} + {% if outbound %} +

The following outbound messages will be deleted:

+ + {% endif %} + {% if inbound %} +

The following inbound messages will be marked undecided:

+ + {% endif %} + {% endafter %} + {% before path=".//form//input[@type='submit']" %} + {% if delete_constraints %} +
+

Delete not allowed.

+ +
+ {% set_attributes path=".//form//input[@type='submit']" disabled=True %} + {% endif %} + {% endbefore %} + {% endamend %} +{% endblock %}