mirror of
https://github.com/netbox-community/netbox.git
synced 2025-09-06 06:13:36 -06:00
Misc cleanup
This commit is contained in:
parent
b5ecae8d19
commit
8a6fbc6f0c
@ -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
|
||||
|
@ -32,13 +32,14 @@ class NumericArrayField(SimpleArrayField):
|
||||
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 = "<code>1-5,10,20-30</code>"
|
||||
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="<code>1-5,10,20-30</code>")
|
||||
)
|
||||
super().__init__(*args, help_text=help_text, **kwargs)
|
||||
|
||||
|
@ -78,5 +78,5 @@ class RangeFunctionsTestCase(TestCase):
|
||||
|
||||
self.assertEqual(
|
||||
string_to_ranges('2-10, a-b'),
|
||||
None # Fails to convert
|
||||
None # Fails to convert
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user