Skip to content
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
2 changes: 1 addition & 1 deletion netbox/netbox/api/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def to_internal_value(self, data):
if type(data[0]) is not int or type(data[1]) is not int:
raise ValidationError(_("Range boundaries must be defined as integers."))

return NumericRange(data[0], data[1], bounds='[]')
return NumericRange(data[0], data[1] + 1, bounds='[)')

def to_representation(self, instance):
return instance.lower, instance.upper - 1
Expand Down
15 changes: 11 additions & 4 deletions netbox/utilities/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,16 @@ def ranges_to_string(ranges):

def string_to_ranges(value):
"""
Given a string in the format "1-100, 200-300" return an list of NumericRanges. Intended for use with ArrayField.
For example:
"1-99,200-299" => [NumericRange(1, 100), NumericRange(200, 300)]
Converts a string representation of numeric ranges into a list of NumericRange objects.

This function parses a string containing numeric values and ranges separated by commas (e.g.,
"1-5,8,10-12") and converts it into a list of NumericRange objects.
In the case of a single integer, it is treated as a range where the start and end
are equal. The returned ranges are represented as half-open intervals [lower, upper).
Intended for use with ArrayField.

Example:
"1-5,8,10-12" => [NumericRange(1, 6), NumericRange(8, 9), NumericRange(10, 13)]
"""
if not value:
return None
Expand All @@ -172,5 +179,5 @@ def string_to_ranges(value):
upper = dash_range[1]
else:
return None
values.append(NumericRange(int(lower), int(upper), bounds='[]'))
values.append(NumericRange(int(lower), int(upper) + 1, bounds='[)'))
return values
12 changes: 6 additions & 6 deletions netbox/utilities/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ def test_string_to_ranges(self):
self.assertEqual(
string_to_ranges('10-19, 30-39, 100-199'),
[
NumericRange(10, 19, bounds='[]'), # 10-19
NumericRange(30, 39, bounds='[]'), # 30-39
NumericRange(100, 199, bounds='[]'), # 100-199
NumericRange(10, 20, bounds='[)'), # 10-20
NumericRange(30, 40, bounds='[)'), # 30-40
NumericRange(100, 200, bounds='[)'), # 100-200
]
)

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
NumericRange(1, 3, bounds='[)'), # 1-3
NumericRange(5, 6, bounds='[)'), # 5-6
NumericRange(10, 13, bounds='[)'), # 10-13
]
)

Expand Down