From f5a5a3c2e161609c7bf401fcdeeb2d7d9f2f83b9 Mon Sep 17 00:00:00 2001 From: viliambalaz Date: Fri, 7 May 2021 22:06:05 +0200 Subject: [PATCH] #334 Allow admin bulk delete on Action model --- chcemvediet/apps/inforequests/admin.py | 69 ++++++++++++++++--- .../action/delete_selected_confirmation.html | 34 +++++++++ .../branch/delete_confirmation.html | 2 +- 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 2900a387..4cfa8944 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -2,15 +2,16 @@ # -*- coding: utf-8 -*- import datetime -from django.contrib import admin +from django.contrib.admin.actions import delete_selected +from django.contrib import admin, messages from django.contrib.admin.utils import NestedObjects from django.core.exceptions import PermissionDenied -from django.db import router +from django.db import router, transaction from django.forms.models import BaseInlineFormSet from django.utils.html import format_html from poleno.utils.date import local_today -from poleno.utils.misc import decorate +from poleno.utils.misc import decorate, squeeze from poleno.utils.admin import (simple_list_filter_factory, admin_obj_format, ReadOnlyAdminInlineMixin, NoBulkDeleteAdminMixin) from chcemvediet.apps.inforequests.constants import ADMIN_EXTEND_SNOOZE_BY_DAYS @@ -333,7 +334,7 @@ def delete_model(self, request, obj): return super(BranchAdmin, self).delete_model(request, obj) @admin.register(Action, site=admin.site) -class ActionAdmin(NoBulkDeleteAdminMixin, DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin): +class ActionAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin): date_hierarchy = u'created' list_display = [ u'id', @@ -374,6 +375,9 @@ class ActionAdmin(NoBulkDeleteAdminMixin, DeleteNestedInforequestEmailAdminMixin inlines = [ BranchInline, ] + actions = [ + u'delete_selected' + ] def get_queryset(self, request): queryset = super(ActionAdmin, self).get_queryset(request) @@ -385,20 +389,69 @@ def get_inforequest(self, obj): return obj.branch.inforequest def delete_constraints(self, obj): - dependency = [] + constraints = [] if obj.type in [Action.TYPES.REQUEST, Action.TYPES.ADVANCED_REQUEST]: - dependency.append(format_html( + constraints.append(format_html( u'{} is type {}.'.format(admin_obj_format(obj), obj.get_type_display()))) if len(obj.branch.actions) == 1: - dependency.append(format_html( + constraints.append(format_html( u'{} is the only action in the branch.'.format(admin_obj_format(obj)))) - return dependency + return constraints def render_delete_form(self, request, context): context[u'delete_constraints'] = self.delete_constraints(context[u'object']) context[u'ADMIN_EXTEND_SNOOZE_BY_DAYS'] = ADMIN_EXTEND_SNOOZE_BY_DAYS return super(ActionAdmin, self).render_delete_form(request, context) + @transaction.atomic + def delete_selected(self, request, queryset): + constraints = [] + warnings = [] + outbound = InforequestEmail.objects.none() + inbound = InforequestEmail.objects.none() + for obj in queryset: + obj_constraints = self.delete_constraints(obj) + constraints += obj_constraints + inforequestemails = self.nested_inforequestemail_queryset(obj) + outbound |= inforequestemails[0] + inbound |= inforequestemails[1] + if not obj_constraints: + if not obj.is_last_action and obj.next_action not in queryset: + warnings.append(format_html(squeeze(u""" + {} is not the last action in the branch. Deleting it may cause logical + errors in the inforequest history. + """).format(admin_obj_format(obj)))) + + if request.POST.get(u'post'): + if constraints: + raise PermissionDenied + + template_response = delete_selected(self, request, queryset) + + if request.POST.get(u'post'): + n = outbound.count() + if n: + outbound.delete() + self.message_user(request, + u'Successfully deleted {} applicant_action inforequestemails.'.format(n), + messages.SUCCESS) + m = inbound.count() + if m: + inbound.update(type=InforequestEmail.TYPES.UNDECIDED) + self.message_user(request, + u'Successfully updated {} obligee_action inforequestemails.'.format(m), + messages.SUCCESS) + 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': constraints, + u'warnings': warnings, + }) + return template_response + delete_selected.short_description = u'Delete selected actions' + def delete_model(self, request, obj): if self.delete_constraints(obj): raise PermissionDenied diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html new file mode 100644 index 00000000..fe1b6645 --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html @@ -0,0 +1,34 @@ +{% 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 %} +

Outbound messages will be deleted:

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

Inbound messages will be marked undecided:

+ + {% endif %} + {% endafter %} + {% before path=".//form" %} + {% if warnings %} + + {% endif %} + {% if delete_constraints %} +
+

Delete not allowed.

+ +
+ {% set_attributes path=".//form//input[@type='submit']" disabled=True %} + {% endif %} + {% endbefore %} + {% endamend %} +{% endblock %} 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 e1af2ba5..94983660 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html @@ -21,7 +21,7 @@ {% endbefore %} - {% set_attributes path=".//form//input[@type='submit']" disabled=False %} + {% set_attributes path=".//form//input[@type='submit']" disabled=True %} {% endif %} {% endamend %} {% endblock %}