1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Jeremy Stretch 944008d475 Closes #12135: Prevent the deletion of interfaces with children (#14091)
* Closes #12135: Prevent the deletion of interfaces with children

* Change PROTECT to RESTRICT

* Extend handle_protectederror() to also handle RestrictedError

* Fix string translation

* Update migrations

* Support bulk removal of parent interfaces via UI if all children are included

* Add support for the bulk deletion of restricted objects via REST API
2023-11-01 13:47:14 -04:00

35 lines
1.3 KiB
Python

from django.contrib import messages
from django.db.models import ProtectedError, RestrictedError
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
def handle_protectederror(obj_list, request, e):
"""
Generate a user-friendly error message in response to a ProtectedError or RestrictedError exception.
"""
if type(e) is ProtectedError:
protected_objects = list(e.protected_objects)
elif type(e) is RestrictedError:
protected_objects = list(e.restricted_objects)
else:
raise e
# Formulate the error message
err_message = _("Unable to delete <strong>{objects}</strong>. {count} dependent objects were found: ").format(
objects=', '.join(str(obj) for obj in obj_list),
count=len(protected_objects) if len(protected_objects) <= 50 else _('More than 50')
)
# Append dependent objects to error message
dependent_objects = []
for dependent in protected_objects[:50]:
if hasattr(dependent, 'get_absolute_url'):
dependent_objects.append(f'<a href="{dependent.get_absolute_url()}">{escape(dependent)}</a>')
else:
dependent_objects.append(str(dependent))
err_message += ', '.join(dependent_objects)
messages.error(request, mark_safe(err_message))