Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#334 Allow admin bulk delete on InforequestEmail model #414

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
constraints.append(format_html(
u'{} is used in its inforequest.'.format(admin_obj_format(obj))))
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
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 %}