Skip to content

Commit

Permalink
Fixes: #14044 - Allow regex renaming of unnamed devices
Browse files Browse the repository at this point in the history
* Allow regex renaming of unnamed devices (already allowed actually)
* Catch errors relating to unnamed devices or integrity errors as a result of the rename process
  • Loading branch information
DanSheps committed Aug 20, 2024
1 parent 96802b4 commit 75b0bea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,17 @@ class DeviceBulkRenameView(generic.BulkRenameView):
filterset = filtersets.DeviceFilterSet
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')
class DeviceContactsView(ObjectContactsView):
Expand Down
8 changes: 8 additions & 0 deletions netbox/netbox/views/generic/bulk_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,14 @@ def post(self, request):
)
return redirect(self.get_return_url(request))

except IntegrityError as e:
messages.error(self.request, ", ".join(e.args))
clear_events.send(sender=self)

except ValidationError as e:
messages.error(self.request, ", ".join(e.messages))
clear_events.send(sender=self)

except (AbortRequest, PermissionsViolation) as e:
logger.debug(e.message)
form.add_error(None, e.message)
Expand Down

0 comments on commit 75b0bea

Please sign in to comment.