From 8e67c548afca39b6ca9f50f2fb685b74df999abe Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 20 Jun 2024 13:59:46 -0700 Subject: [PATCH] 9627 numeric range field --- netbox/utilities/forms/fields/array.py | 31 +++++++++++++++++++++ netbox/utilities/forms/fields/expandable.py | 26 ----------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/netbox/utilities/forms/fields/array.py b/netbox/utilities/forms/fields/array.py index 14f122453..01349b9ec 100644 --- a/netbox/utilities/forms/fields/array.py +++ b/netbox/utilities/forms/fields/array.py @@ -1,11 +1,13 @@ from django import forms from django.contrib.postgres.forms import SimpleArrayField +from django.db.backends.postgresql.psycopg_any import NumericRange from django.utils.translation import gettext_lazy as _ from ..utils import parse_numeric_range __all__ = ( 'NumericArrayField', + 'NumericRangeArrayField', ) @@ -24,3 +26,32 @@ class NumericArrayField(SimpleArrayField): if isinstance(value, str): value = ','.join([str(n) for n in parse_numeric_range(value)]) return super().to_python(value) + + +class NumericRangeArrayField(forms.CharField): + """ + A field which allows for array of numeric ranges: + Example: 1-5,7-20,30-50 + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if not self.help_text: + self.help_text = _( + "Specify one or more numeric ranges separated by commas " + "Example: 1-5,20-30" + ) + + def prepare_value(self, value): + range_str = ','.join([f"{val.lower}-{val.upper}" for val in value]) + return range_str + + def to_python(self, value): + if not value: + return '' + ranges = value.split(",") + values = [] + for dash_range in value.split(','): + begin, end = dash_range.split('-') + values.append(NumericRange(begin, end)) + return values diff --git a/netbox/utilities/forms/fields/expandable.py b/netbox/utilities/forms/fields/expandable.py index 6330d9f5a..959271a85 100644 --- a/netbox/utilities/forms/fields/expandable.py +++ b/netbox/utilities/forms/fields/expandable.py @@ -1,7 +1,6 @@ import re from django import forms -from django.db.backends.postgresql.psycopg_any import NumericRange from django.utils.translation import gettext_lazy as _ from utilities.forms.constants import * @@ -10,7 +9,6 @@ from utilities.forms.utils import expand_alphanumeric_pattern, expand_ipaddress_ __all__ = ( 'ExpandableIPAddressField', 'ExpandableNameField', - 'NumericRangeArrayField', ) @@ -55,27 +53,3 @@ class ExpandableIPAddressField(forms.CharField): elif ':' in value and re.search(IP6_EXPANSION_PATTERN, value): return list(expand_ipaddress_pattern(value, 6)) return [value] - - -class NumericRangeArrayField(forms.CharField): - """ - A field which allows for array of numeric ranges: - Example: 1-5,7-20,30-50 - """ - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - if not self.help_text: - self.help_text = _( - "Specify one or more numeric ranges separated by commas " - "Example: 1-5,20-30" - ) - - def to_python(self, value): - if not value: - return '' - ranges = split(value, ",") - numeric_ranges = [] - for range in ranges: - numeric_ranges.append(NumericRange(range[0], range[1])) - return [numeric_ranges]