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
17 changes: 16 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,18 @@ 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])
deletion_objects = []
if collector.instances_with_model():
for model, instance in collector.instances_with_model():
# we could ignore the instance == obj so that the list doesnt contain itself...
deletion_objects.append({
"modelname":f"{model.__name__}",
"object": 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 +356,15 @@ def get(self, request, *args, **kwargs):
'object_type': self.queryset.model._meta.verbose_name,
'form': form,
'form_url': form_url,
'deletion_objects': deletion_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),
'deletion_objects': deletion_objects,
**self.get_extra_context(request, obj),
})

Expand Down
8 changes: 8 additions & 0 deletions netbox/templates/htmx/delete_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ <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 deletion_objects %}
<p>
This will cause deltion of the following items: <br>
{% for deletion_object in deletion_objects %}
- {{ deletion_object.modelname }} - {{ deletion_object.object | linkify }} <br>
{% endfor %}
</p>
{% endif %}
{% render_form form %}
</div>
<div class="modal-footer">
Expand Down