From b5ecae8d1910a8a1fdf51b7f7b1b8e8ac355beaf Mon Sep 17 00:00:00 2001 From: Jathn Date: Tue, 5 Aug 2025 21:14:27 +0300 Subject: [PATCH 1/2] Fixes #19379: allow standalone id in vlan-ids range list --- netbox/utilities/data.py | 16 +++++++++++++--- netbox/utilities/forms/fields/array.py | 3 ++- netbox/utilities/tests/test_data.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/netbox/utilities/data.py b/netbox/utilities/data.py index 73c8476ecdb..d2c78f011ce 100644 --- a/netbox/utilities/data.py +++ b/netbox/utilities/data.py @@ -160,9 +160,19 @@ def string_to_ranges(value): return None value.replace(' ', '') # Remove whitespace values = [] - for dash_range in value.split(','): - if '-' not in dash_range: + for data in value.split(','): + dash_range = data.strip().split('-') + lower, upper = '', '' + if len(dash_range) == 1 and str(dash_range[0]).isdigit(): + # Range is only a single value, which is a valid number + lower = dash_range[0] + upper = dash_range[0] + elif len(dash_range) == 2 and str(dash_range[0]).isdigit() and str(dash_range[1]).isdigit(): + # The range has 2 values and both are valid number + lower = dash_range[0] + upper = dash_range[1] + else: return None - lower, upper = dash_range.split('-') + values.append(NumericRange(int(lower), int(upper), bounds='[]')) return values diff --git a/netbox/utilities/forms/fields/array.py b/netbox/utilities/forms/fields/array.py index e6de2d89f8a..331f69bc7e4 100644 --- a/netbox/utilities/forms/fields/array.py +++ b/netbox/utilities/forms/fields/array.py @@ -36,8 +36,9 @@ class NumericRangeArrayField(forms.CharField): """ def __init__(self, *args, help_text='', **kwargs): if not help_text: + example = "1-5,10,20-30" help_text = mark_safe( - _("Specify one or more numeric ranges separated by commas. Example: " + "1-5,20-30") + _("Specify one or more individual values or numeric ranges separated by commas. Example: " + example) ) super().__init__(*args, help_text=help_text, **kwargs) diff --git a/netbox/utilities/tests/test_data.py b/netbox/utilities/tests/test_data.py index c83885233aa..e7d86c0fede 100644 --- a/netbox/utilities/tests/test_data.py +++ b/netbox/utilities/tests/test_data.py @@ -66,3 +66,17 @@ def test_string_to_ranges(self): NumericRange(100, 199, bounds='[]'), # 100-199 ] ) + + self.assertEqual( + string_to_ranges('1-2, 5, 10-12'), + [ + NumericRange(1, 2, bounds='[]'), # 1-2 + NumericRange(5, 5, bounds='[]'), # 5-5 + NumericRange(10, 12, bounds='[]'), # 10-12 + ] + ) + + self.assertEqual( + string_to_ranges('2-10, a-b'), + None # Fails to convert + ) From 8a6fbc6f0cc0a2d3b08e6d24adc61942cd37b71b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 7 Aug 2025 08:39:50 -0400 Subject: [PATCH 2/2] Misc cleanup --- netbox/utilities/data.py | 6 ++---- netbox/utilities/forms/fields/array.py | 7 ++++--- netbox/utilities/tests/test_data.py | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/netbox/utilities/data.py b/netbox/utilities/data.py index d2c78f011ce..7b50d26b84d 100644 --- a/netbox/utilities/data.py +++ b/netbox/utilities/data.py @@ -162,17 +162,15 @@ def string_to_ranges(value): values = [] for data in value.split(','): dash_range = data.strip().split('-') - lower, upper = '', '' if len(dash_range) == 1 and str(dash_range[0]).isdigit(): - # Range is only a single value, which is a valid number + # Single integer value; expand to a range lower = dash_range[0] upper = dash_range[0] elif len(dash_range) == 2 and str(dash_range[0]).isdigit() and str(dash_range[1]).isdigit(): - # The range has 2 values and both are valid number + # The range has two values and both are valid integers lower = dash_range[0] upper = dash_range[1] else: return None - values.append(NumericRange(int(lower), int(upper), bounds='[]')) return values diff --git a/netbox/utilities/forms/fields/array.py b/netbox/utilities/forms/fields/array.py index 331f69bc7e4..e4f6c972f87 100644 --- a/netbox/utilities/forms/fields/array.py +++ b/netbox/utilities/forms/fields/array.py @@ -32,13 +32,14 @@ def to_python(self, value): class NumericRangeArrayField(forms.CharField): """ A field which allows for array of numeric ranges: - Example: 1-5,7-20,30-50 + Example: 1-5,10,20-30 """ def __init__(self, *args, help_text='', **kwargs): if not help_text: - example = "1-5,10,20-30" help_text = mark_safe( - _("Specify one or more individual values or numeric ranges separated by commas. Example: " + example) + _( + "Specify one or more individual numbers or numeric ranges separated by commas. Example: {example}" + ).format(example="1-5,10,20-30") ) super().__init__(*args, help_text=help_text, **kwargs) diff --git a/netbox/utilities/tests/test_data.py b/netbox/utilities/tests/test_data.py index e7d86c0fede..7b313baf787 100644 --- a/netbox/utilities/tests/test_data.py +++ b/netbox/utilities/tests/test_data.py @@ -78,5 +78,5 @@ def test_string_to_ranges(self): self.assertEqual( string_to_ranges('2-10, a-b'), - None # Fails to convert + None # Fails to convert )