From f6a3973da39ea48e4769828466212c1bf849738a Mon Sep 17 00:00:00 2001 From: Viliam Balaz Date: Thu, 29 Jul 2021 17:45:25 +0200 Subject: [PATCH] #334 Allow admin bulk delete on InforequestEmail model (#414) * #334 Allow admin bulk delete on InforequestEmail model * #334 Filter related actions --- chcemvediet/apps/inforequests/admin.py | 41 +++++++++++++++++++ .../inforequestemail/delete_confirmation.html | 17 ++++++++ .../delete_selected_confirmation.html | 17 ++++++++ 3 files changed, 75 insertions(+) create mode 100644 chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html create mode 100644 chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 06a930bd..5bf9ee7d 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -253,6 +253,9 @@ class InforequestEmailAdmin(admin.ModelAdmin): ] inlines = [ ] + actions = [ + u'delete_selected' + ] def get_queryset(self, request): queryset = super(InforequestEmailAdmin, self).get_queryset(request) @@ -260,6 +263,44 @@ def get_queryset(self, request): queryset = queryset.select_related(u'email') return queryset + def delete_constraints(self, objs): + constraints = [] + for obj in objs: + actions = Action.objects.of_inforequest(obj.inforequest).filter(email=obj.email) + if actions: + constraints.append(format_html( + u'{} is used for {}.'.format( + admin_obj_format(obj), + u', '.join([admin_obj_format(action) for action in actions]), + ))) + return constraints + + def render_delete_form(self, request, context): + context[u'delete_constraints'] = self.delete_constraints([context[u'object']]) + return super(InforequestEmailAdmin, self).render_delete_form(request, context) + + @decorate(short_description=u'Delete selected inforequestemails') + @transaction.atomic + def delete_selected(self, request, 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'): + return None + + template_response.context_data.update({ + u'delete_constraints': self.delete_constraints(queryset), + }) + return template_response + + def delete_model(self, request, obj): + if self.delete_constraints([obj]): + raise PermissionDenied + return super(InforequestEmailAdmin, self).delete_model(request, obj) + @admin.register(Branch, site=admin.site) class BranchAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin): date_hierarchy = None diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html new file mode 100644 index 00000000..fbe7e99c --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html @@ -0,0 +1,17 @@ +{% extends "admin/delete_confirmation.html" %} +{% load amend before set_attributes from poleno.amend %} + +{% block content %} + {% amend %} + {{ block.super }} + {% if delete_constraints %} + {% before path=".//form" %} +
+

Delete not allowed.

+ +
+ {% endbefore %} + {% set_attributes path=".//form//input[@type='submit']" disabled=True %} + {% endif %} + {% endamend %} +{% endblock %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html new file mode 100644 index 00000000..178fe1bc --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html @@ -0,0 +1,17 @@ +{% extends "admin/delete_selected_confirmation.html" %} +{% load amend before set_attributes from poleno.amend %} + +{% block content %} + {% amend %} + {{ block.super }} + {% 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 %}