diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 8366908ff..c5eaca7df 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -4,12 +4,13 @@ from django.contrib import admin from django.contrib.admin.utils import NestedObjects +from django.core.exceptions import PermissionDenied from django.db import router 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, squeeze +from poleno.utils.misc import decorate 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 @@ -46,18 +47,9 @@ def nested_objects_traverse(self, to_delete): yield to_delete def render_delete_form(self, request, context): - obj = context[u'object'] - outbound, inbound = self.nested_inforequestemail_queryset(obj) - if outbound: - context[u'deleted_objects'].extend([ - u'Outbound messages will be deleted:', - [admin_obj_format(inforequestemail) for inforequestemail in outbound] - ]) - if inbound: - context[u'deleted_objects'].extend([ - u'Inbound messages will be marked undecided:', - [admin_obj_format(inforequestemail) for inforequestemail in inbound] - ]) + outbound, inbound = self.nested_inforequestemail_queryset(context[u'object']) + context[u'outbound'] = [admin_obj_format(inforequestemail) for inforequestemail in outbound] + context[u'inbound'] = [admin_obj_format(inforequestemail) for inforequestemail in inbound] return super(DeleteNestedInforequestEmailAdminMixin, self).render_delete_form(request, context) @@ -384,26 +376,13 @@ def get_queryset(self, request): def get_inforequest(self, obj): return obj.branch.inforequest - def has_delete_permission(self, request, obj=None): - if obj is None: - return True - if obj.type in [Action.TYPES.REQUEST, Action.TYPES.ADVANCED_REQUEST]: - return False - if len(obj.branch.actions) > 1: - return True - return False - def render_delete_form(self, request, context): - action = context[u'object'] - if not action.is_last_action: - context[u'deleted_objects'].insert(0, format_html(squeeze(u""" - Warning: The deleted action is not the last action in the branch. Deleting it - may cause logical errors in the inforequest history. - """))) context[u'ADMIN_EXTEND_SNOOZE_BY_DAYS'] = ADMIN_EXTEND_SNOOZE_BY_DAYS return super(ActionAdmin, self).render_delete_form(request, context) def delete_model(self, request, obj): + if obj.delete_dependency: + raise PermissionDenied if request.POST: if (request.POST.get(u'snooze') and obj.type in [Action.TYPES.EXPIRATION, Action.TYPES.APPEAL_EXPIRATION] diff --git a/chcemvediet/apps/inforequests/models/action.py b/chcemvediet/apps/inforequests/models/action.py index 51c813158..892ab3cd3 100644 --- a/chcemvediet/apps/inforequests/models/action.py +++ b/chcemvediet/apps/inforequests/models/action.py @@ -8,11 +8,13 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.functional import cached_property from django.contrib.contenttypes import generic +from django.utils.html import format_html from aggregate_if import Count from multiselectfield import MultiSelectField from poleno import datacheck from poleno.attachments.models import Attachment +from poleno.utils.admin import admin_obj_format from poleno.workdays import workdays from poleno.utils.models import FieldChoices, QuerySet, join_lookup, after_saved from poleno.utils.date import utc_now, local_today @@ -450,6 +452,17 @@ def deadline(self): raise ValueError(u'Invalid action type: {}'.format(self.type)) + @cached_property + def delete_dependency(self): + dependency = [] + if self.type in [Action.TYPES.REQUEST, Action.TYPES.ADVANCED_REQUEST]: + dependency.append(format_html( + u'{} is type {}.'.format(admin_obj_format(self), self.get_type_display()))) + if len(self.branch.actions) == 1: + dependency.append(format_html( + u'{} is the only action in the branch.'.format(admin_obj_format(self)))) + return dependency + @classmethod def create(cls, *args, **kwargs): advanced_to = kwargs.pop(u'advanced_to', None) or [] diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html index bb31540a7..198082eeb 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html @@ -1,13 +1,36 @@ {% extends "admin/delete_confirmation.html" %} -{% load amend prepend from poleno.amend %} +{% load amend prepend before after delete from poleno.amend %} {% block content %} {% amend %} {{ block.super }} + {% after path="./ul[1]" %} + {% if outbound %} +

Outbound messages will be deleted:

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

Inbound messages will be marked undecided:

+ + {% endif %} + {% endafter %} + {% if not object.is_last_action %} + {% before path=".//form" %} +

+ Warning: The deleted action is not the last action in the branch. Deleting it may + cause logical errors in the inforequest history. +

+ {% endbefore %} + {% endif %} {% if object.type == object.TYPES.EXPIRATION or object.type == object.TYPES.APPEAL_EXPIRATION %} {% prepend path=".//form" %}

{% endprepend %} {% endif %} + {% if object.delete_dependency %} +

Info: Delete not allowed.

+ + {% delete path=".//form" %} + {% endif %} {% 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 new file mode 100644 index 000000000..becfc1617 --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html @@ -0,0 +1,18 @@ +{% extends "admin/delete_confirmation.html" %} +{% load amend after from poleno.amend %} + +{% block content %} + {% amend %} + {{ block.super }} + {% after path="./ul[1]" %} + {% if outbound %} +

Outbound messages will be deleted:

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

Inbound messages will be marked undecided:

+ + {% endif %} + {% endafter %} + {% endamend %} +{% endblock %}