Skip to content

Commit

Permalink
#334 Allow admin bulk delete on InforequestEmail model (#414)
Browse files Browse the repository at this point in the history
* #334 Allow admin bulk delete on InforequestEmail model

* #334 Filter related actions
  • Loading branch information
viliambalaz authored Jul 29, 2021
1 parent f22ea51 commit f6a3973
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
41 changes: 41 additions & 0 deletions chcemvediet/apps/inforequests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,54 @@ 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:
actions = Action.objects.of_inforequest(obj.inforequest).filter(email=obj.email)
if actions:
constraints.append(format_html(
u'{} is used for {}.'.format(
admin_obj_format(obj),
u', '.join([admin_obj_format(action) for action in actions]),
)))
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 f6a3973

Please sign in to comment.