Skip to content

Commit

Permalink
#334 Create BulkDeleteAdminMixin (#422)
Browse files Browse the repository at this point in the history
* #334 Create BulkDeleteAdminMixin

* #334 Indent li

* #334 Create admin delete mixin templates

* #334 Add empty div for amend workaround

* #334 Workaround with fixed amend templatetag
  • Loading branch information
viliambalaz authored Apr 20, 2022
1 parent c91e384 commit 67d7867
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 226 deletions.
112 changes: 22 additions & 90 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
import datetime

from django.contrib import admin
from django.contrib.admin.actions import delete_selected
from django.contrib.admin.utils import NestedObjects
from django.core.exceptions import PermissionDenied
from django.db import router, transaction
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.admin import (simple_list_filter_factory, admin_obj_format,
ReadOnlyAdminInlineMixin)
ReadOnlyAdminInlineMixin, BulkDeleteAdminMixin)
from chcemvediet.apps.inforequests.constants import ADMIN_EXTEND_SNOOZE_BY_DAYS

from .models import Inforequest, InforequestDraft, InforequestEmail, Branch, Action


class DeleteNestedInforequestEmailAdminMixin(admin.ModelAdmin):
class DeleteNestedInforequestEmailAdminMixin(BulkDeleteAdminMixin, admin.ModelAdmin):

def nested_inforequestemail_queryset(self, objs):
using = router.db_for_write(self.model)
Expand Down Expand Up @@ -54,11 +52,27 @@ def render_delete_form(self, request, context):
return super(DeleteNestedInforequestEmailAdminMixin, self).render_delete_form(request,
context)

@transaction.atomic
def delete_selected(self, request, queryset):
outbound, inbound = self.nested_inforequestemail_queryset(queryset)
template_response = super(DeleteNestedInforequestEmailAdminMixin, self).delete_selected(request, queryset)

if request.POST.get(u'post'):
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],
})
return template_response

def delete_model(self, request, obj):
outbound, inbound = self.nested_inforequestemail_queryset([obj])
super(DeleteNestedInforequestEmailAdminMixin, self).delete_model(request, obj)
outbound.delete()
inbound.update(type=InforequestEmail.TYPES.UNDECIDED)
super(DeleteNestedInforequestEmailAdminMixin, self).delete_model(request, obj)

class BranchFormSet(BaseInlineFormSet):
def get_queryset(self):
Expand Down Expand Up @@ -216,7 +230,7 @@ def get_queryset(self, request):
return queryset

@admin.register(InforequestEmail, site=admin.site)
class InforequestEmailAdmin(admin.ModelAdmin):
class InforequestEmailAdmin(BulkDeleteAdminMixin, admin.ModelAdmin):
date_hierarchy = None
list_display = [
u'id',
Expand Down Expand Up @@ -253,9 +267,6 @@ class InforequestEmailAdmin(admin.ModelAdmin):
]
inlines = [
]
actions = [
u'delete_selected'
]

def get_queryset(self, request):
queryset = super(InforequestEmailAdmin, self).get_queryset(request)
Expand All @@ -275,32 +286,6 @@ def delete_constraints(self, objs):
)))
return constraints

def render_delete_form(self, request, context):
context[u'delete_constraints'] = self.delete_constraints([context[u'object']])
return super(InforequestEmailAdmin, self).render_delete_form(request, context)

@decorate(short_description=u'Delete selected inforequestemails')
@transaction.atomic
def delete_selected(self, request, queryset):
if request.POST.get(u'post'):
if self.delete_constraints(queryset):
raise PermissionDenied

template_response = delete_selected(self, request, queryset)

if request.POST.get(u'post'):
return None

template_response.context_data.update({
u'delete_constraints': self.delete_constraints(queryset),
})
return template_response

def delete_model(self, request, obj):
if self.delete_constraints([obj]):
raise PermissionDenied
return super(InforequestEmailAdmin, self).delete_model(request, obj)

@admin.register(Branch, site=admin.site)
class BranchAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
date_hierarchy = None
Expand Down Expand Up @@ -350,9 +335,6 @@ class BranchAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
inlines = [
ActionInline,
]
actions = [
u'delete_selected'
]

def get_queryset(self, request):
queryset = super(BranchAdmin, self).get_queryset(request)
Expand All @@ -368,37 +350,6 @@ def delete_constraints(self, objs):
constraints.append(format_html(u'{} is main.'.format(admin_obj_format(obj))))
return constraints

def render_delete_form(self, request, context):
context[u'delete_constraints'] = self.delete_constraints([context[u'object']])
return super(BranchAdmin, self).render_delete_form(request, context)

@decorate(short_description=u'Delete selected branches')
@transaction.atomic
def delete_selected(self, request, queryset):
outbound, inbound = self.nested_inforequestemail_queryset(queryset)
if request.POST.get(u'post'):
if self.delete_constraints(queryset):
raise PermissionDenied

template_response = delete_selected(self, request, queryset)

if request.POST.get(u'post'):
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': self.delete_constraints(queryset),
})
return template_response

def delete_model(self, request, obj):
if self.delete_constraints([obj]):
raise PermissionDenied
return super(BranchAdmin, self).delete_model(request, obj)

@admin.register(Action, site=admin.site)
class ActionAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
date_hierarchy = u'created'
Expand Down Expand Up @@ -441,9 +392,6 @@ class ActionAdmin(DeleteNestedInforequestEmailAdminMixin, admin.ModelAdmin):
inlines = [
BranchInline,
]
actions = [
u'delete_selected'
]

def get_queryset(self, request):
queryset = super(ActionAdmin, self).get_queryset(request)
Expand Down Expand Up @@ -485,49 +433,33 @@ def snooze_action(self, obj):

def render_delete_form(self, request, context):
obj = context[u'object']
context[u'delete_warnings'] = self.delete_warnings([obj])
context[u'delete_constraints'] = self.delete_constraints([obj])
if self.can_snooze_previous_action(obj):
context[u'snoozed_actions'] = [admin_obj_format(obj.previous_action)]
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):
snoozed_actions = []
for obj in queryset:
if self.can_snooze_previous_action(obj) and obj.previous_action not in queryset:
snoozed_actions.append(obj.previous_action)
outbound, inbound = self.nested_inforequestemail_queryset(queryset)

if request.POST.get(u'post'):
if self.delete_constraints(queryset):
raise PermissionDenied

template_response = delete_selected(self, request, queryset)
template_response = super(ActionAdmin, self).delete_selected(request, queryset)

if request.POST.get(u'post'):
if request.POST.get(u'snooze'):
for action in snoozed_actions:
self.snooze_action(action)
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'snoozed_actions': [admin_obj_format(action) for action in snoozed_actions],
u'ADMIN_EXTEND_SNOOZE_BY_DAYS': ADMIN_EXTEND_SNOOZE_BY_DAYS,
u'delete_warnings': self.delete_warnings(queryset),
u'delete_constraints': self.delete_constraints(queryset),
})
return template_response

def delete_model(self, request, obj):
if self.delete_constraints([obj]):
raise PermissionDenied
super(ActionAdmin, self).delete_model(request, obj)
if request.POST.get(u'snooze') and self.can_snooze_previous_action(obj):
self.snooze_action(obj.previous_action)
return super(ActionAdmin, self).delete_model(request, obj)
Original file line number Diff line number Diff line change
@@ -1,45 +1,11 @@
{% extends "admin/delete_confirmation.html" %}
{% load amend prepend before after set_attributes from poleno.amend %}
{% load amend from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% after path="./ul[last()]" %}
{% if outbound %}
<p>The following outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>The following inbound messages will be marked undecided:</p>
<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 by
{{ ADMIN_EXTEND_SNOOZE_BY_DAYS }} days from today? All of the following actions will be
snoozed:
</label>
</p>
<ul>{{ snoozed_actions|unordered_list }}</ul>
{% endprepend %}
{% endif %}
{% before path=".//form//input[@type='submit']" %}
{% 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>
{% set_attributes path=".//form//input[@type='submit']" disabled=True %}
{% endif %}
{% endbefore %}
{% include "admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html" %}
{% include "admin/inforequests/mixins/snoozed_actions_mixin.html" %}
{% include "utils/admin/bulk_delete_mixin.html" %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,45 +1,11 @@
{% extends "admin/delete_selected_confirmation.html" %}
{% load amend prepend before after set_attributes from poleno.amend %}
{% load amend from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% after path="./ul[last()]" %}
{% if outbound %}
<p>The following outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>The following inbound messages will be marked undecided:</p>
<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 by
{{ ADMIN_EXTEND_SNOOZE_BY_DAYS }} days from today? All of the following actions will be
snoozed:
</label>
</p>
<ul>{{ snoozed_actions|unordered_list }}</ul>
{% endprepend %}
{% endif %}
{% before path=".//form//input[@type='submit']" %}
{% 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>
{% set_attributes path=".//form//input[@type='submit']" disabled=True %}
{% endif %}
{% endbefore %}
{% include "admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html" %}
{% include "admin/inforequests/mixins/snoozed_actions_mixin.html" %}
{% include "utils/admin/bulk_delete_mixin.html" %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
{% extends "admin/delete_confirmation.html" %}
{% load amend before after set_attributes from poleno.amend %}
{% load amend from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% after path="./ul[last()]" %}
{% if outbound %}
<p>The following outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>The following inbound messages will be marked undecided:</p>
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% 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 %}
{% include "admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html" %}
{% include "utils/admin/bulk_delete_mixin.html" %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
{% extends "admin/delete_selected_confirmation.html" %}
{% load amend before after set_attributes from poleno.amend %}
{% load amend from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% after path="./ul[last()]" %}
{% if outbound %}
<p>The following outbound messages will be deleted:</p>
<ul>{{ outbound|unordered_list }}</ul>
{% endif %}
{% if inbound %}
<p>The following inbound messages will be marked undecided:</p>
<ul>{{ inbound|unordered_list }}</ul>
{% endif %}
{% endafter %}
{% before path=".//form//input[@type='submit']" %}
{% if delete_constraints %}
<div class="errornote">
<p>Delete not allowed.</p>
<ul>{{ delete_constraints|unordered_list }}</ul>
</div>
{% set_attributes path=".//form//input[@type='submit']" disabled=True %}
{% endif %}
{% endbefore %}
{% include "admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html" %}
{% include "utils/admin/bulk_delete_mixin.html" %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
{% extends "admin/delete_confirmation.html" %}
{% load amend before set_attributes from poleno.amend %}
{% load amend from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% 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 %}
{% include "utils/admin/bulk_delete_mixin.html" %}
{% endamend %}
{% endblock %}
Loading

0 comments on commit 67d7867

Please sign in to comment.