diff --git a/chcemvediet/apps/inforequests/admin.py b/chcemvediet/apps/inforequests/admin.py index 5bf9ee7d..bb40c40b 100644 --- a/chcemvediet/apps/inforequests/admin.py +++ b/chcemvediet/apps/inforequests/admin.py @@ -3,9 +3,7 @@ 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 @@ -13,13 +11,13 @@ 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) @@ -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): @@ -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', @@ -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) @@ -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 @@ -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) @@ -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' @@ -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) @@ -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) diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html index 10fd2ac0..bae49a9c 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_confirmation.html @@ -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 %} -

The following outbound messages will be deleted:

- - {% endif %} - {% if inbound %} -

The following inbound messages will be marked undecided:

- - {% endif %} - {% endafter %} - {% if snoozed_actions %} - {% prepend path=".//form" %} -

- -

- - {% endprepend %} - {% endif %} - {% before path=".//form//input[@type='submit']" %} - {% if delete_warnings %} - - {% endif %} - {% if delete_constraints %} -
-

Delete not allowed.

- -
- {% 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 %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html index 8be8a9cc..197d1832 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/action/delete_selected_confirmation.html @@ -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 %} -

The following outbound messages will be deleted:

- - {% endif %} - {% if inbound %} -

The following inbound messages will be marked undecided:

- - {% endif %} - {% endafter %} - {% if snoozed_actions %} - {% prepend path=".//form" %} -

- -

- - {% endprepend %} - {% endif %} - {% before path=".//form//input[@type='submit']" %} - {% if delete_warnings %} - - {% endif %} - {% if delete_constraints %} -
-

Delete not allowed.

- -
- {% 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 %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html index 48cb5a58..0912153a 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_confirmation.html @@ -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 %} -

The following outbound messages will be deleted:

- - {% endif %} - {% if inbound %} -

The following inbound messages will be marked undecided:

- - {% endif %} - {% endafter %} - {% if delete_constraints %} - {% before path=".//form" %} -
-

Delete not allowed.

- -
- {% 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 %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html index 1997ccfb..49c189db 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/branch/delete_selected_confirmation.html @@ -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 %} -

The following outbound messages will be deleted:

- - {% endif %} - {% if inbound %} -

The following inbound messages will be marked undecided:

- - {% endif %} - {% endafter %} - {% before path=".//form//input[@type='submit']" %} - {% if delete_constraints %} -
-

Delete not allowed.

- -
- {% 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 %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html index fbe7e99c..014b6fa6 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_confirmation.html @@ -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" %} -
-

Delete not allowed.

- -
- {% endbefore %} - {% set_attributes path=".//form//input[@type='submit']" disabled=True %} - {% endif %} + {% include "utils/admin/bulk_delete_mixin.html" %} {% endamend %} {% endblock %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html index 178fe1bc..4ea4c16e 100644 --- a/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/inforequestemail/delete_selected_confirmation.html @@ -1,17 +1,9 @@ {% extends "admin/delete_selected_confirmation.html" %} -{% load amend before set_attributes from poleno.amend %} +{% load amend from poleno.amend %} {% block content %} {% amend %} {{ block.super }} - {% before path=".//form//input[@type='submit']" %} - {% if delete_constraints %} -
-

Delete not allowed.

- -
- {% set_attributes path=".//form//input[@type='submit']" disabled=True %} - {% endif %} - {% endbefore %} + {% include "utils/admin/bulk_delete_mixin.html" %} {% endamend %} {% endblock %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html new file mode 100644 index 00000000..4fa0b290 --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/delete_nested_inforequest_email_mixin.html @@ -0,0 +1,12 @@ +{% load after from poleno.amend %} + +{% after path="./ul[last()]" %} + {% if outbound %} +

The following outbound messages will be deleted:

+ + {% endif %} + {% if inbound %} +

The following inbound messages will be marked undecided:

+ + {% endif %} +{% endafter %} diff --git a/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/snoozed_actions_mixin.html b/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/snoozed_actions_mixin.html new file mode 100644 index 00000000..e7e72b26 --- /dev/null +++ b/chcemvediet/apps/inforequests/templates/admin/inforequests/mixins/snoozed_actions_mixin.html @@ -0,0 +1,13 @@ +{% load prepend from poleno.amend %} + +{% prepend path=".//form" %} + {% if snoozed_actions %} +

+ +

+ + {% endif %} +{% endprepend %} diff --git a/poleno/utils/admin.py b/poleno/utils/admin.py index 4e5d6889..059e6cc0 100644 --- a/poleno/utils/admin.py +++ b/poleno/utils/admin.py @@ -1,6 +1,9 @@ # vim: expandtab # -*- coding: utf-8 -*- +from django.contrib.admin.actions import delete_selected +from django.core.exceptions import PermissionDenied from django.core.urlresolvers import NoReverseMatch +from django.db import transaction from django.utils.html import format_html from django.contrib import admin @@ -58,3 +61,43 @@ def has_add_permission(self, request, obj=None): def has_delete_permission(self, request, obj=None): return False + +class BulkDeleteAdminMixin(admin.ModelAdmin): + + actions = admin.ModelAdmin.actions + [ + u'delete_selected', + ] + + def delete_warnings(self, objs): + return [] + + def delete_constraints(self, objs): + return [] + + 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]) + return super(BulkDeleteAdminMixin, self).render_delete_form(request, context) + + @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_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 + return super(BulkDeleteAdminMixin, self).delete_model(request, obj) diff --git a/poleno/utils/templates/utils/admin/bulk_delete_mixin.html b/poleno/utils/templates/utils/admin/bulk_delete_mixin.html new file mode 100644 index 00000000..ca491327 --- /dev/null +++ b/poleno/utils/templates/utils/admin/bulk_delete_mixin.html @@ -0,0 +1,18 @@ +{% load before set_attributes from poleno.amend %} + +{% before path=".//form//input[@type='submit']" %} + {% if delete_warnings %} + + {% endif %} + {% if delete_constraints %} +
+

Delete not allowed.

+ +
+ {% set_attributes path=".//form//input[@type='submit']" disabled=True %} + {% endif %} +{% endbefore %}