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

Closes #13690: List all objects to be deleted #14089

Merged
15 changes: 14 additions & 1 deletion netbox/netbox/views/generic/object_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from copy import deepcopy

from django.contrib import messages
from django.db import transaction
from django.db import transaction, router
from django.db.models import ProtectedError
from django.db.models.deletion import Collector
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils.html import escape
Expand Down Expand Up @@ -334,6 +335,16 @@ def get(self, request, *args, **kwargs):
obj = self.get_object(**kwargs)
form = ConfirmationForm(initial=request.GET)

using = router.db_for_write(obj._meta.model)
collector = Collector(using=using)
collector.collect([obj])
related_objects = {}
for model, instance in collector.instances_with_model():
# we could ignore the instance == obj so that the list doesnt contain itself...
if model.__name__ not in related_objects:
related_objects[model.__name__] = []
related_objects[model.__name__].append(instance)

# If this is an HTMX request, return only the rendered deletion form as modal content
if is_htmx(request):
viewname = get_viewname(self.queryset.model, action='delete')
Expand All @@ -343,13 +354,15 @@ def get(self, request, *args, **kwargs):
'object_type': self.queryset.model._meta.verbose_name,
'form': form,
'form_url': form_url,
'related_objects': related_objects,
**self.get_extra_context(request, obj),
})

return render(request, self.template_name, {
'object': obj,
'form': form,
'return_url': self.get_return_url(request, obj),
'related_objects': related_objects,
**self.get_extra_context(request, obj),
})

Expand Down
12 changes: 12 additions & 0 deletions netbox/templates/htmx/delete_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ <h5 class="modal-title">{% trans "Confirm Deletion" %}</h5>
Are you sure you want to <strong class="text-danger">delete</strong> {{ object_type }} <strong>{{ object }}</strong>?
{% endblocktrans %}
</p>
{% if related_objects %}
<p>
This will cause deletion of the following items: <br>
{% for model,objects in related_objects.items %}
- {{ model }}:
{% for object in objects %}
{{ object | linkify }}{% if not forloop.last %}, {% endif %}
{% endfor %}
<br>
{% endfor %}
</p>
{% endif %}
{% render_form form %}
</div>
<div class="modal-footer">
Expand Down