From 7a53e24f9721a8506008e8bafc25ddd04fa2f412 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 19 Feb 2020 13:53:11 -0500 Subject: [PATCH] Closes #3810: Preserve slug value when editing existing objects --- docs/release-notes/version-2.7.md | 1 + netbox/project-static/js/forms.js | 14 ++++++++++---- netbox/utilities/forms.py | 10 +++++++++- netbox/utilities/templates/widgets/sluginput.html | 8 ++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 netbox/utilities/templates/widgets/sluginput.html diff --git a/docs/release-notes/version-2.7.md b/docs/release-notes/version-2.7.md index 14fe69622..ef05bda8c 100644 --- a/docs/release-notes/version-2.7.md +++ b/docs/release-notes/version-2.7.md @@ -3,6 +3,7 @@ ## Enhancements * [#2511](https://github.com/netbox-community/netbox/issues/2511) - Compare object change to the previous change +* [#3810](https://github.com/netbox-community/netbox/issues/3810) - Preserve slug value when editing existing objects * [#3840](https://github.com/netbox-community/netbox/issues/3840) - Enhance search function when selecting VLANs for interface assignment * [#4170](https://github.com/netbox-community/netbox/issues/4170) - Improve color contrast in rack elevation drawings diff --git a/netbox/project-static/js/forms.js b/netbox/project-static/js/forms.js index 802d1b4e9..7f71d28c0 100644 --- a/netbox/project-static/js/forms.js +++ b/netbox/project-static/js/forms.js @@ -42,17 +42,23 @@ $(document).ready(function() { return s.substring(0, num_chars); // Trim to first num_chars chars } var slug_field = $('#id_slug'); - slug_field.change(function() { - $(this).attr('_changed', true); - }); if (slug_field) { var slug_source = $('#id_' + slug_field.attr('slug-source')); var slug_length = slug_field.attr('maxlength'); + if (slug_field[0].value) { + slug_field.attr('_changed', true); + } + slug_field.change(function() { + $(this).attr('_changed', true); + }); slug_source.on('keyup change', function() { if (slug_field && !slug_field.attr('_changed')) { slug_field.val(slugify($(this).val(), (slug_length ? slug_length : 50))); } - }) + }); + $('button.reslugify').click(function() { + slug_field.val(slugify(slug_source.val(), (slug_length ? slug_length : 50))); + }); } // Bulk edit nullification diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index c629d70a2..a7ee63eaa 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -132,6 +132,13 @@ class SmallTextarea(forms.Textarea): pass +class SlugWidget(forms.TextInput): + """ + Subclass TextInput and add a slug regeneration button next to the form field. + """ + template_name = 'widgets/sluginput.html' + + class ColorSelect(forms.Select): """ Extends the built-in Select widget to colorize each