Skip to content

Fixes: #13722 - Correct range expansion code when a numeric set is used #15301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions netbox/utilities/forms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,43 @@ def parse_alphanumeric_range(string):
'0-3,a-d' => [0, 1, 2, 3, a, b, c, d]
"""
values = []
for dash_range in string.split(','):
for value in string.split(','):
if '-' not in value:
# Item is not a range
values.append(value)
continue

# Find the range's beginning & end values
try:
begin, end = dash_range.split('-')
begin, end = value.split('-')
vals = begin + end
# Break out of loop if there's an invalid pattern to return an error
if (not (vals.isdigit() or vals.isalpha())) or (vals.isalpha() and not (vals.isupper() or vals.islower())):
return []
except ValueError:
begin, end = dash_range, dash_range
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=value))

# Numeric range
if begin.isdigit() and end.isdigit():
if int(begin) >= int(end):
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))

raise forms.ValidationError(
_('Invalid range: Ending value ({end}) must be greater than beginning value ({begin}).').format(
begin=begin, end=end
)
)
for n in list(range(int(begin), int(end) + 1)):
values.append(n)

# Alphanumeric range
else:
# Value-based
if begin == end:
values.append(begin)
# Range-based
else:
# Not a valid range (more than a single character)
if not len(begin) == len(end) == 1:
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))

if ord(begin) >= ord(end):
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))

for n in list(range(ord(begin), ord(end) + 1)):
values.append(chr(n))
# Not a valid range (more than a single character)
if not len(begin) == len(end) == 1:
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=value))
if ord(begin) >= ord(end):
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=value))
for n in list(range(ord(begin), ord(end) + 1)):
values.append(chr(n))

return values


Expand Down
11 changes: 10 additions & 1 deletion netbox/utilities/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,16 @@ def test_range_alpha(self):

self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)

def test_set(self):
def test_set_numeric(self):
input = 'r[1,2]a'
output = sorted([
'r1a',
'r2a',
])

self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)

def test_set_alpha(self):
input = '[r,t]1a'
output = sorted([
'r1a',
Expand Down