Skip to content

Commit

Permalink
#334 Refactor Action admin deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Apr 15, 2021
1 parent 325ffcf commit b18d3f4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
35 changes: 7 additions & 28 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"""
<b>Warning:</b> 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]
Expand Down
13 changes: 13 additions & 0 deletions chcemvediet/apps/inforequests/models/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %}
<p>Outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>Inbound messages will be marked undecided:</p>
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% if not object.is_last_action %}
{% before path=".//form" %}
<p>
<b>Warning:</b> The deleted action is not the last action in the branch. Deleting it may
cause logical errors in the inforequest history.
</p>
{% endbefore %}
{% endif %}
{% if object.type == object.TYPES.EXPIRATION or object.type == object.TYPES.APPEAL_EXPIRATION %}
{% prepend path=".//form" %}
<p><label><input name="snooze" type="checkbox" /> Extend snooze of previous action by {{ ADMIN_EXTEND_SNOOZE_BY_DAYS }} days from today.</label></p>
{% endprepend %}
{% endif %}
{% if object.delete_dependency %}
<p><b>Info:</b> Delete not allowed.</p>
<ul>{{ object.delete_dependency|unordered_list }}</ul>
{% delete path=".//form" %}
{% endif %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -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 %}
<p>Outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>Inbound messages will be marked undecided:</p>
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% endamend %}
{% endblock %}

0 comments on commit b18d3f4

Please sign in to comment.