Skip to content

Commit

Permalink
#334 Refactor Action admin deleting (#356)
Browse files Browse the repository at this point in the history
* #334 Refactor Action admin deleting

* #334 Move delete_dependency to admin and review suggestions

* #334 Use set_attributes tag to disable submit button

* #334 Review

Co-authored-by: Martin Macko <[email protected]>
  • Loading branch information
viliambalaz and martinmacko47 authored Apr 16, 2021
1 parent 325ffcf commit fb9a890
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
42 changes: 16 additions & 26 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,24 @@ 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
def delete_constraints(self, obj):
dependency = []
if obj.type in [Action.TYPES.REQUEST, Action.TYPES.ADVANCED_REQUEST]:
return False
if len(obj.branch.actions) > 1:
return True
return False
dependency.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(
u'{} is the only action in the branch.'.format(admin_obj_format(obj))))
return dependency

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'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)

def delete_model(self, request, obj):
if self.delete_constraints(obj):
raise PermissionDenied
if request.POST:
if (request.POST.get(u'snooze')
and obj.type in [Action.TYPES.EXPIRATION, Action.TYPES.APPEAL_EXPIRATION]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
{% extends "admin/delete_confirmation.html" %}
{% load amend prepend from poleno.amend %}
{% load amend prepend before after set_attributes from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% after path="./ul[last()]" %}
{% 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 delete_constraints %}
{% before path=".//form" %}
<div class="errornote">
<p>Delete not allowed.</p>
<ul>{{ delete_constraints|unordered_list }}</ul>
</div>
{% endbefore %}
{% set_attributes path=".//form//input[@type='submit']" disabled=True %}
{% 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[last()]" %}
{% 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 fb9a890

Please sign in to comment.