mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 16:48:16 -06:00
Misc cleanup
This commit is contained in:
parent
6171ce6038
commit
674907ce0f
@ -138,12 +138,9 @@ def ranges_to_string(ranges):
|
|||||||
"""
|
"""
|
||||||
Generate a human-friendly string from a set of ranges. Intended for use with ArrayField.
|
Generate a human-friendly string from a set of ranges. Intended for use with ArrayField.
|
||||||
For example:
|
For example:
|
||||||
[1-100, 200-300] => "1-100, 200-300"
|
[Range(1, 100), Range(200, 300)] => "1-100, 200-300"
|
||||||
"""
|
"""
|
||||||
if not ranges:
|
return ', '.join([f"{r.lower}-{r.upper}" for r in ranges])
|
||||||
return ""
|
|
||||||
|
|
||||||
return ', '.join([f"{val.lower}-{val.upper}" for val in ranges])
|
|
||||||
|
|
||||||
|
|
||||||
def string_to_range_array(value):
|
def string_to_range_array(value):
|
||||||
|
@ -34,18 +34,17 @@ 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,7-20,30-50
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, *args, help_text='', **kwargs):
|
||||||
def __init__(self, *args, **kwargs):
|
if not help_text:
|
||||||
super().__init__(*args, **kwargs)
|
help_text = mark_safe(
|
||||||
if not self.help_text:
|
|
||||||
self.help_text = mark_safe(
|
|
||||||
_("Specify one or more numeric ranges separated by commas. Example: " + "<code>1-5,20-30</code>")
|
_("Specify one or more numeric ranges separated by commas. Example: " + "<code>1-5,20-30</code>")
|
||||||
)
|
)
|
||||||
|
super().__init__(*args, help_text=help_text, **kwargs)
|
||||||
|
|
||||||
def clean(self, value):
|
def clean(self, value):
|
||||||
if value and not self.to_python(value):
|
if value and not self.to_python(value):
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
_("Invalid ranges ({value}). Must be range of number '100-200' and ranges must be in ascending order.").format(value=value)
|
_("Invalid ranges ({value}). Must be a range of integers in ascending order.").format(value=value)
|
||||||
)
|
)
|
||||||
return super().clean(value)
|
return super().clean(value)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user