9627 numeric range field

This commit is contained in:
Arthur Hanson 2024-06-20 13:31:00 -07:00
parent 9e99703d37
commit 2f2945b825
4 changed files with 49 additions and 2 deletions

View File

@ -15,7 +15,7 @@ from utilities.exceptions import PermissionsViolation
from utilities.forms import add_blank_choice from utilities.forms import add_blank_choice
from utilities.forms.fields import ( from utilities.forms.fields import (
CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, NumericArrayField, CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, NumericArrayField,
SlugField, SlugField, NumericRangeArrayField
) )
from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, TabbedGroups from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, TabbedGroups
from utilities.forms.widgets import DatePicker from utilities.forms.widgets import DatePicker
@ -637,7 +637,7 @@ class VLANGroupForm(NetBoxModelForm):
# IntegerRangeField(), # IntegerRangeField(),
# delimiter="|" # delimiter="|"
# ) # )
vlan_id_ranges = IntegerRangeField() vlan_id_ranges = NumericRangeArrayField()
fieldsets = ( fieldsets = (
FieldSet('name', 'slug', 'description', 'tags', name=_('VLAN Group')), FieldSet('name', 'slug', 'description', 'tags', name=_('VLAN Group')),

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.11 on 2024-06-20 19:28
import django.contrib.postgres.fields
import django.contrib.postgres.fields.ranges
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('ipam', '0069_gfk_indexes'),
]
operations = [
migrations.AddField(
model_name='vlangroup',
name='vlan_id_ranges',
field=django.contrib.postgres.fields.ArrayField(base_field=django.contrib.postgres.fields.ranges.BigIntegerRangeField(), blank=True, null=True, size=None),
),
]

View File

@ -71,6 +71,7 @@ class VLANGroup(OrganizationalModel):
blank=True, blank=True,
null=True null=True
) )
# vlan_id_ranges = BigIntegerRangeField(null=True, blank=True)
objects = VLANGroupQuerySet.as_manager() objects = VLANGroupQuerySet.as_manager()

View File

@ -1,6 +1,7 @@
import re import re
from django import forms from django import forms
from django.db.backends.postgresql.psycopg_any import NumericRange
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from utilities.forms.constants import * from utilities.forms.constants import *
@ -9,6 +10,7 @@ from utilities.forms.utils import expand_alphanumeric_pattern, expand_ipaddress_
__all__ = ( __all__ = (
'ExpandableIPAddressField', 'ExpandableIPAddressField',
'ExpandableNameField', 'ExpandableNameField',
'NumericRangeArrayField',
) )
@ -53,3 +55,27 @@ class ExpandableIPAddressField(forms.CharField):
elif ':' in value and re.search(IP6_EXPANSION_PATTERN, value): elif ':' in value and re.search(IP6_EXPANSION_PATTERN, value):
return list(expand_ipaddress_pattern(value, 6)) return list(expand_ipaddress_pattern(value, 6))
return [value] 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: <code>1-5,20-30</code>"
)
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]