Move validation to ensure all renames are eligible

This commit is contained in:
Daniel Sheppard 2024-08-25 15:55:18 -05:00
parent 75b0bea22b
commit ec3a7f3bcb
2 changed files with 5 additions and 11 deletions

View File

@ -2122,17 +2122,6 @@ class DeviceBulkRenameView(generic.BulkRenameView):
filterset = filtersets.DeviceFilterSet filterset = filtersets.DeviceFilterSet
table = tables.DeviceTable table = tables.DeviceTable
def _rename_objects(self, form, selected_objects):
# Check devices for any unnamed devices and enforce requirements on the renaming of devices
for obj in selected_objects:
if not form.cleaned_data['use_regex'] and not obj.name:
from django.core.exceptions import ValidationError
raise ValidationError({
'use_regex': 'You must use regex to rename a unnamed device and must pass device uniqueness checks'
})
super()._rename_objects(form, selected_objects)
@register_model_view(Device, 'contacts') @register_model_view(Device, 'contacts')
class DeviceContactsView(ObjectContactsView): class DeviceContactsView(ObjectContactsView):

View File

@ -727,6 +727,11 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView):
renamed_pks = [] renamed_pks = []
for obj in selected_objects: for obj in selected_objects:
# Validate that the rename will be successful and not trigger an error
if not form.cleaned_data['use_regex'] and not obj.name:
raise ValidationError({
'use_regex': 'You must use regex to rename and must pass uniqueness checks'
})
# Take a snapshot of change-logged models # Take a snapshot of change-logged models
if hasattr(obj, 'snapshot'): if hasattr(obj, 'snapshot'):