Skip to content

Commit

Permalink
#334 Various refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed May 23, 2021
1 parent 9d88da3 commit 2885365
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
53 changes: 24 additions & 29 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# -*- 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, transaction
Expand All @@ -13,7 +13,7 @@
from poleno.utils.date import local_today
from poleno.utils.misc import decorate, squeeze
from poleno.utils.admin import (simple_list_filter_factory, admin_obj_format,
ReadOnlyAdminInlineMixin, NoBulkDeleteAdminMixin)
ReadOnlyAdminInlineMixin)
from chcemvediet.apps.inforequests.constants import ADMIN_EXTEND_SNOOZE_BY_DAYS

from .models import Inforequest, InforequestDraft, InforequestEmail, Branch, Action
Expand Down Expand Up @@ -261,7 +261,7 @@ def get_queryset(self, request):
return queryset

@admin.register(Branch, site=admin.site)
class BranchAdmin(NoBulkDeleteAdminMixin, DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
class BranchAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
date_hierarchy = None
list_display = [
u'id',
Expand Down Expand Up @@ -388,6 +388,15 @@ def get_queryset(self, request):
def get_inforequest(self, obj):
return obj.branch.inforequest

def delete_warnings(self, obj):
warnings = []
if not obj.is_last_action:
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))))
return warnings

def delete_constraints(self, obj):
constraints = []
if obj.type in [Action.TYPES.REQUEST, Action.TYPES.ADVANCED_REQUEST]:
Expand All @@ -399,58 +408,44 @@ def delete_constraints(self, obj):
return constraints

def render_delete_form(self, request, context):
context[u'delete_warnings'] = self.delete_warnings(context[u'object'])
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)

@decorate(short_description=u'Delete selected actions')
@transaction.atomic
def delete_selected(self, request, queryset):
constraints = []
warnings = []
delete_warnings = []
delete_constraints = []
outbound = InforequestEmail.objects.none()
inbound = InforequestEmail.objects.none()
for obj in queryset:
obj_constraints = self.delete_constraints(obj)
constraints += obj_constraints
if obj.next_action not in queryset:
delete_warnings += self.delete_warnings(obj)
delete_constraints += self.delete_constraints(obj)
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:
if delete_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)
outbound.delete()
inbound.update(type=InforequestEmail.TYPES.UNDECIDED)
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,
u'delete_warnings': delete_warnings,
u'delete_constraints': delete_constraints,
})
return template_response
delete_selected.short_description = u'Delete selected actions'

def delete_model(self, request, obj):
if self.delete_constraints(obj):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,26 @@
<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" %}
{% before path=".//form" %}
{% if delete_warnings %}
<ul class="messagelist">
{% for warning in delete_warnings %}
<li class="warning">{{ warning }}</li>
{% endfor %}
</ul>
{% endif %}
{% if delete_constraints %}
<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 %}
{% set_attributes path=".//form//input[@type='submit']" disabled=True %}
{% endif %}
{% endbefore %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
{% endif %}
{% endafter %}
{% before path=".//form" %}
{% if warnings %}
{% if delete_warnings %}
<ul class="messagelist">
{% for warning in warnings %}
{% for warning in delete_warnings %}
<li class="warning">{{ warning }}</li>
{% endfor %}
</ul>
Expand Down
5 changes: 5 additions & 0 deletions chcemvediet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
url(r'', include(u'poleno.pages.urls', namespace=u'pages')),
)

try:
admin.site.disable_action(u'delete_selected')
except KeyError:
pass

if settings.DEBUG: # pragma: no cover
urlpatterns = patterns(u'',
url(r'^media/(?P<path>.*)$', u'django.views.static.serve', {u'document_root': settings.MEDIA_ROOT, u'show_indexes': True}),
Expand Down
7 changes: 0 additions & 7 deletions poleno/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,3 @@ def has_add_permission(self, request, obj=None):

def has_delete_permission(self, request, obj=None):
return False

class NoBulkDeleteAdminMixin(admin.ModelAdmin):

def get_actions(self, request):
actions = super(NoBulkDeleteAdminMixin, self).get_actions(request)
actions.pop(u'delete_selected', None)
return actions

0 comments on commit 2885365

Please sign in to comment.