Skip to content

Commit

Permalink
#334 Create BulkDeleteAdminMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Dec 7, 2021
1 parent 5f4315e commit df366ce
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 179 deletions.
111 changes: 22 additions & 89 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,8 +433,6 @@ 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
Expand All @@ -499,35 +445,22 @@ def delete_selected(self, request, queryset):
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)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{% if delete_warnings %}
<ul class="messagelist">
{% for warning in delete_warnings %}
<li class="warning">{{ warning }}</li>
<li class="warning">{{ warning }}</li>
{% endfor %}
</ul>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{% if delete_warnings %}
<ul class="messagelist">
{% for warning in delete_warnings %}
<li class="warning">{{ warning }}</li>
<li class="warning">{{ warning }}</li>
{% endfor %}
</ul>
{% endif %}
Expand Down

This file was deleted.

This file was deleted.

43 changes: 43 additions & 0 deletions poleno/utils/admin.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

0 comments on commit df366ce

Please sign in to comment.