Fixes #19379: allow standalone id in vlan-ids range list (#20024)
Some checks failed
CI / build (20.x, 3.10) (push) Waiting to run
CI / build (20.x, 3.11) (push) Waiting to run
CI / build (20.x, 3.12) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, actions) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled

* Fixes #19379: allow standalone id in vlan-ids range list

* Misc cleanup

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Jonathan Ramstedt 2025-08-07 15:56:07 +03:00 committed by GitHub
parent 65b36fd594
commit 122f612750
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View File

@ -160,9 +160,17 @@ def string_to_ranges(value):
return None return None
value.replace(' ', '') # Remove whitespace value.replace(' ', '') # Remove whitespace
values = [] values = []
for dash_range in value.split(','): for data in value.split(','):
if '-' not in dash_range: dash_range = data.strip().split('-')
if len(dash_range) == 1 and str(dash_range[0]).isdigit():
# 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 two values and both are valid integers
lower = dash_range[0]
upper = dash_range[1]
else:
return None return None
lower, upper = dash_range.split('-')
values.append(NumericRange(int(lower), int(upper), bounds='[]')) values.append(NumericRange(int(lower), int(upper), bounds='[]'))
return values return values

View File

@ -32,12 +32,14 @@ class NumericArrayField(SimpleArrayField):
class NumericRangeArrayField(forms.CharField): class NumericRangeArrayField(forms.CharField):
""" """
A field which allows for array of numeric ranges: 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): def __init__(self, *args, help_text='', **kwargs):
if not help_text: if not help_text:
help_text = mark_safe( help_text = mark_safe(
_("Specify one or more numeric ranges separated by commas. Example: " + "<code>1-5,20-30</code>") _(
"Specify one or more individual numbers or numeric ranges separated by commas. Example: {example}"
).format(example="<code>1-5,10,20-30</code>")
) )
super().__init__(*args, help_text=help_text, **kwargs) super().__init__(*args, help_text=help_text, **kwargs)

View File

@ -66,3 +66,17 @@ class RangeFunctionsTestCase(TestCase):
NumericRange(100, 199, bounds='[]'), # 100-199 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
)