Misc cleanup

This commit is contained in:
Jeremy Stretch 2024-07-13 11:24:03 -04:00
parent 6171ce6038
commit 674907ce0f
2 changed files with 7 additions and 11 deletions

View File

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

View File

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