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

This commit is contained in:
Jathn 2025-08-05 21:14:27 +03:00
parent 6ce3012f93
commit b5ecae8d19
3 changed files with 29 additions and 4 deletions

View File

@ -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

View File

@ -36,8 +36,9 @@ class NumericRangeArrayField(forms.CharField):
"""
def __init__(self, *args, help_text='', **kwargs):
if not help_text:
example = "<code>1-5,10,20-30</code>"
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 values or numeric ranges separated by commas. Example: " + example)
)
super().__init__(*args, help_text=help_text, **kwargs)

View File

@ -66,3 +66,17 @@ class RangeFunctionsTestCase(TestCase):
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
)