From 2b4c37aa0bdec9257156ba1433a5f9ca18d06d97 Mon Sep 17 00:00:00 2001 From: Viliam Balaz Date: Tue, 27 Apr 2021 10:42:20 +0200 Subject: [PATCH] #334 Refactor Branch admin deleting (#371) --- chcemvediet/apps/inforequests/admin.py | 16 ++++++++++++---- .../inforequests/branch/delete_confirmation.html | 11 ++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 2743a718..2900a387 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -319,10 +319,18 @@ def get_queryset(self, request): def get_inforequest(self, obj): return obj.inforequest - def has_delete_permission(self, request, obj=None): - if obj is None: - return True - return not obj.is_main + def delete_constraints(self, obj): + if obj.is_main: + return [format_html(u'{} is main.'.format(admin_obj_format(obj)))] + + 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) + + def delete_model(self, request, obj): + if self.delete_constraints(obj): + raise PermissionDenied + return super(BranchAdmin, self).delete_model(request, obj) @admin.register(Action, site=admin.site) class ActionAdmin(NoBulkDeleteAdminMixin, DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin): diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html index dae382a2..e1af2ba5 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html @@ -1,5 +1,5 @@ {% extends "admin/delete_confirmation.html" %} -{% load amend after from poleno.amend %} +{% load amend before after set_attributes from poleno.amend %} {% block content %} {% amend %} @@ -14,5 +14,14 @@ {% endif %} {% endafter %} + {% if delete_constraints %} + {% before path=".//form" %} +
+

Delete not allowed.

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