Skip to content

Commit

Permalink
#334 Allow admin bulk delete on InforequestEmail model
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Jul 11, 2021
1 parent bd39bc4 commit 6cc4bda
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
38 changes: 38 additions & 0 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,51 @@ class InforequestEmailAdmin(admin.ModelAdmin):
]
inlines = [
]
actions = [
u'delete_selected'
]

def get_queryset(self, request):
queryset = super(InforequestEmailAdmin, self).get_queryset(request)
queryset = queryset.select_related(u'inforequest')
queryset = queryset.select_related(u'email')
return queryset

def delete_constraints(self, objs):
constraints = []
for obj in objs:
if hasattr(obj.email, u'action'):
if obj.inforequest == obj.email.action.branch.inforequest:
constraints.append(format_html(
u'{} is used in its inforequest.'.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(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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "admin/delete_confirmation.html" %}
{% load amend before set_attributes 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 %}
{% endamend %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "admin/delete_selected_confirmation.html" %}
{% load amend before set_attributes from poleno.amend %}

{% block content %}
{% amend %}
{{ block.super }}
{% 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 %}
{% endamend %}
{% endblock %}

0 comments on commit 6cc4bda

Please sign in to comment.