Skip to content

Commit

Permalink
#334 Snooze previous actions
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed May 23, 2021
1 parent 2885365 commit 3774454
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
1 change: 1 addition & 0 deletions chcemvediet/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def init_admin_site_plus(self):
from adminplus.sites import AdminSitePlus
from django.contrib import admin
admin.site = AdminSitePlus()
admin.site.disable_action(u'delete_selected')
admin.autodiscover()

def workaround_sqlite_format_dtdelta_bug(self):
Expand Down
68 changes: 40 additions & 28 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@

class DeleteNestedInforequestEmailAdminMixin(admin.ModelAdmin):

def get_inforequest(self, obj):
def get_inforequest(self, objs):
raise NotImplementedError

def nested_inforequestemail_queryset(self, obj):
def nested_inforequestemail_queryset(self, objs):
using = router.db_for_write(self.model)
collector = NestedObjects(using)
collector.collect([obj])
collector.collect(objs)
to_delete = collector.nested()
inforequest = self.get_inforequest(obj)
inforequests = self.get_inforequest(objs)
actions = [obj for obj in self.nested_objects_traverse(to_delete)
if isinstance(obj, Action)]
emails = [action.email for action in actions if action.email]
inforequestemails_qs = InforequestEmail.objects.filter(inforequest=inforequest,
inforequestemails_qs = InforequestEmail.objects.filter(inforequest=inforequests,
email__in=emails)
outbound = inforequestemails_qs.filter(type=InforequestEmail.TYPES.APPLICANT_ACTION)
inbound = inforequestemails_qs.filter(type=InforequestEmail.TYPES.OBLIGEE_ACTION)
Expand All @@ -48,14 +48,14 @@ def nested_objects_traverse(self, to_delete):
yield to_delete

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

def delete_model(self, request, obj):
outbound, inbound = self.nested_inforequestemail_queryset(obj)
outbound, inbound = self.nested_inforequestemail_queryset([obj])
outbound.delete()
inbound.update(type=InforequestEmail.TYPES.UNDECIDED)
super(DeleteNestedInforequestEmailAdminMixin, self).delete_model(request, obj)
Expand Down Expand Up @@ -317,8 +317,8 @@ def get_queryset(self, request):
queryset = queryset.select_related(u'advanced_by')
return queryset

def get_inforequest(self, obj):
return obj.inforequest
def get_inforequest(self, objs):
return Inforequest.objects.filter(branch__in=objs)

def delete_constraints(self, obj):
if obj.is_main:
Expand Down Expand Up @@ -385,12 +385,15 @@ def get_queryset(self, request):
queryset = queryset.select_related(u'email')
return queryset

def get_inforequest(self, obj):
return obj.branch.inforequest
def get_inforequest(self, objs):
return Inforequest.objects.filter(branch__action__in=objs)

def delete_warnings(self, obj):
def delete_warnings(self, obj, to_delete=None):
if self.delete_constraints(obj):
return []
to_delete = to_delete or []
warnings = []
if not obj.is_last_action:
if not obj.is_last_action and obj.next_action not in to_delete:
warnings.append(format_html(squeeze(u"""
{} is not the last action in the branch. Deleting it may cause logical errors in
the inforequest history.
Expand All @@ -407,26 +410,32 @@ def delete_constraints(self, obj):
u'{} is the only action in the branch.'.format(admin_obj_format(obj))))
return constraints

def can_snooze(self, obj):
if (obj.type in [Action.TYPES.EXPIRATION, Action.TYPES.APPEAL_EXPIRATION]
and obj.previous_action):
return True
return False

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
obj = context[u'object']
context[u'delete_warnings'] = self.delete_warnings(obj)
context[u'delete_constraints'] = self.delete_constraints(obj)
if self.can_snooze(obj):
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):
delete_warnings = []
delete_constraints = []
outbound = InforequestEmail.objects.none()
inbound = InforequestEmail.objects.none()
snoozed_actions = []
for obj in queryset:
if obj.next_action not in queryset:
delete_warnings += self.delete_warnings(obj)
delete_warnings += self.delete_warnings(obj, queryset)
delete_constraints += self.delete_constraints(obj)
inforequestemails = self.nested_inforequestemail_queryset(obj)
outbound |= inforequestemails[0]
inbound |= inforequestemails[1]
if self.can_snooze(obj):
snoozed_actions.append(obj.previous_action)
outbound, inbound = self.nested_inforequestemail_queryset(queryset)

if request.POST.get(u'post'):
if delete_constraints:
Expand All @@ -435,25 +444,28 @@ def delete_selected(self, request, queryset):
template_response = delete_selected(self, request, queryset)

if request.POST.get(u'post'):
if request.POST.get(u'snooze'):
for action in snoozed_actions:
action.snooze = local_today() + datetime.timedelta(days=ADMIN_EXTEND_SNOOZE_BY_DAYS)
action.save(update_fields=[u'snooze'])
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_warnings': delete_warnings,
u'delete_constraints': delete_constraints,
u'snoozed_actions': [admin_obj_format(action) for action in snoozed_actions],
u'delete_warnings': delete_warnings,
u'delete_constraints': delete_constraints,
})
return template_response

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]
and obj.previous_action):
if request.POST.get(u'snooze') and self.can_snooze(obj):
previous = obj.previous_action
previous.snooze = local_today() + datetime.timedelta(days=ADMIN_EXTEND_SNOOZE_BY_DAYS)
previous.save(update_fields=[u'snooze'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% if object.type == object.TYPES.EXPIRATION or object.type == object.TYPES.APPEAL_EXPIRATION %}
{% if ADMIN_EXTEND_SNOOZE_BY_DAYS %}
{% 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 %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin/delete_selected_confirmation.html" %}
{% load amend before after set_attributes from poleno.amend %}
{% load amend prepend before after set_attributes from poleno.amend %}

{% block content %}
{% amend %}
Expand All @@ -14,6 +14,16 @@
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% if snoozed_actions %}
{% prepend path=".//form" %}
<p>
<label><input name="snooze" type="checkbox" /> Extend snooze of previous actions to the
following deleted actions by {{ ADMIN_EXTEND_SNOOZE_BY_DAYS }} days from today:
</label>
</p>
<ul>{{ snoozed_actions|unordered_list }}</ul>
{% endprepend %}
{% endif %}
{% before path=".//form" %}
{% if delete_warnings %}
<ul class="messagelist">
Expand Down
5 changes: 0 additions & 5 deletions chcemvediet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
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

0 comments on commit 3774454

Please sign in to comment.