From 8f8ba2c86c94b105b8930febd91cd96fee6812fb Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 20 Jun 2024 15:44:48 -0700 Subject: [PATCH] 9627 numeric range field --- netbox/ipam/filtersets.py | 3 ++- netbox/ipam/forms/bulk_edit.py | 3 ++- netbox/ipam/forms/filtersets.py | 26 +++++++++---------- netbox/ipam/forms/model_forms.py | 8 ++---- .../0070_vlangroup_vlan_id_ranges.py | 21 +++++++++++++++ netbox/ipam/models/vlans.py | 19 -------------- netbox/ipam/querysets.py | 2 +- netbox/ipam/search.py | 3 ++- netbox/ipam/tables/vlans.py | 2 +- netbox/utilities/forms/fields/array.py | 4 +-- 10 files changed, 46 insertions(+), 45 deletions(-) diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index 5cdfac34e..32395d22b 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -914,7 +914,8 @@ class VLANGroupFilterSet(OrganizationalModelFilterSet): class Meta: model = VLANGroup - fields = ('id', 'name', 'slug', 'min_vid', 'max_vid', 'description', 'scope_id') + fields = ('id', 'name', 'slug', 'description', 'scope_id') + # fields = ('id', 'name', 'slug', 'min_vid', 'max_vid', 'description', 'scope_id') def search(self, queryset, name, value): if not value.strip(): diff --git a/netbox/ipam/forms/bulk_edit.py b/netbox/ipam/forms/bulk_edit.py index c7f64ab1d..da752a8db 100644 --- a/netbox/ipam/forms/bulk_edit.py +++ b/netbox/ipam/forms/bulk_edit.py @@ -486,7 +486,8 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm): model = VLANGroup fieldsets = ( - FieldSet('site', 'min_vid', 'max_vid', 'description'), + # FieldSet('site', 'min_vid', 'max_vid', 'description'), + FieldSet('site', 'description'), FieldSet( 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', 'clustergroup', 'cluster', name=_('Scope') ), diff --git a/netbox/ipam/forms/filtersets.py b/netbox/ipam/forms/filtersets.py index 80fb04226..a79707cf7 100644 --- a/netbox/ipam/forms/filtersets.py +++ b/netbox/ipam/forms/filtersets.py @@ -413,7 +413,7 @@ class VLANGroupFilterForm(NetBoxModelFilterSetForm): FieldSet('q', 'filter_id', 'tag'), FieldSet('region', 'sitegroup', 'site', 'location', 'rack', name=_('Location')), FieldSet('cluster_group', 'cluster', name=_('Cluster')), - FieldSet('min_vid', 'max_vid', name=_('VLAN ID')), + # FieldSet('min_vid', 'max_vid', name=_('VLAN ID')), ) model = VLANGroup region = DynamicModelMultipleChoiceField( @@ -441,18 +441,18 @@ class VLANGroupFilterForm(NetBoxModelFilterSetForm): required=False, label=_('Rack') ) - min_vid = forms.IntegerField( - required=False, - min_value=VLAN_VID_MIN, - max_value=VLAN_VID_MAX, - label=_('Minimum VID') - ) - max_vid = forms.IntegerField( - required=False, - min_value=VLAN_VID_MIN, - max_value=VLAN_VID_MAX, - label=_('Maximum VID') - ) + # min_vid = forms.IntegerField( + # required=False, + # min_value=VLAN_VID_MIN, + # max_value=VLAN_VID_MAX, + # label=_('Minimum VID') + # ) + # max_vid = forms.IntegerField( + # required=False, + # min_value=VLAN_VID_MIN, + # max_value=VLAN_VID_MAX, + # label=_('Maximum VID') + # ) cluster = DynamicModelMultipleChoiceField( queryset=Cluster.objects.all(), required=False, diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index f06de2312..be300a27d 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -633,15 +633,11 @@ class VLANGroupForm(NetBoxModelForm): } ) slug = SlugField() - # vlan_id_ranges = SimpleArrayField( - # IntegerRangeField(), - # delimiter="|" - # ) vlan_id_ranges = NumericRangeArrayField() fieldsets = ( FieldSet('name', 'slug', 'description', 'tags', name=_('VLAN Group')), - FieldSet('min_vid', 'max_vid', 'vlan_id_ranges', name=_('Child VLANs')), + FieldSet('vlan_id_ranges', name=_('Child VLANs')), FieldSet( 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', 'clustergroup', 'cluster', name=_('Scope') @@ -652,7 +648,7 @@ class VLANGroupForm(NetBoxModelForm): model = VLANGroup fields = [ 'name', 'slug', 'description', 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', - 'clustergroup', 'cluster', 'min_vid', 'max_vid', 'vlan_id_ranges', 'tags', + 'clustergroup', 'cluster', 'vlan_id_ranges', 'tags', ] def __init__(self, *args, **kwargs): diff --git a/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py b/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py index 880a868b9..6dffe9446 100644 --- a/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py +++ b/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py @@ -3,6 +3,15 @@ import django.contrib.postgres.fields import django.contrib.postgres.fields.ranges from django.db import migrations +from django.db.backends.postgresql.psycopg_any import NumericRange + + +def move_min_max(apps, schema_editor): + VLANGroup = apps.get_model('ipam', 'VLANGroup') + for group in VLANGroup.objects.all(): + if group.min_vid or group.max_vid: + group.vlan_id_ranges = [NumericRange(group.min_vid, group.max_vid)] + group.save() class Migration(migrations.Migration): @@ -17,4 +26,16 @@ class Migration(migrations.Migration): name='vlan_id_ranges', field=django.contrib.postgres.fields.ArrayField(base_field=django.contrib.postgres.fields.ranges.BigIntegerRangeField(), blank=True, null=True, size=None), ), + migrations.RunPython( + code=move_min_max, + reverse_code=migrations.RunPython.noop + ), + migrations.RemoveField( + model_name='vlangroup', + name='max_vid', + ), + migrations.RemoveField( + model_name='vlangroup', + name='min_vid', + ), ] diff --git a/netbox/ipam/models/vlans.py b/netbox/ipam/models/vlans.py index 861b3fe3b..bfd505fef 100644 --- a/netbox/ipam/models/vlans.py +++ b/netbox/ipam/models/vlans.py @@ -46,24 +46,6 @@ class VLANGroup(OrganizationalModel): ct_field='scope_type', fk_field='scope_id' ) - min_vid = models.PositiveSmallIntegerField( - verbose_name=_('minimum VLAN ID'), - default=VLAN_VID_MIN, - validators=( - MinValueValidator(VLAN_VID_MIN), - MaxValueValidator(VLAN_VID_MAX) - ), - help_text=_('Lowest permissible ID of a child VLAN') - ) - max_vid = models.PositiveSmallIntegerField( - verbose_name=_('maximum VLAN ID'), - default=VLAN_VID_MAX, - validators=( - MinValueValidator(VLAN_VID_MIN), - MaxValueValidator(VLAN_VID_MAX) - ), - help_text=_('Highest permissible ID of a child VLAN') - ) vlan_id_ranges = ArrayField( BigIntegerRangeField(), verbose_name=_('min/max VLAN IDs'), @@ -71,7 +53,6 @@ class VLANGroup(OrganizationalModel): blank=True, null=True ) - # vlan_id_ranges = BigIntegerRangeField(null=True, blank=True) objects = VLANGroupQuerySet.as_manager() diff --git a/netbox/ipam/querysets.py b/netbox/ipam/querysets.py index a3f37fe3c..bb3aba433 100644 --- a/netbox/ipam/querysets.py +++ b/netbox/ipam/querysets.py @@ -63,7 +63,7 @@ class VLANGroupQuerySet(RestrictedQuerySet): return self.annotate( vlan_count=count_related(VLAN, 'group'), - utilization=Round(F('vlan_count') / (F('max_vid') - F('min_vid') + 1.0) * 100, 2) + # utilization=Round(F('vlan_count') / (F('max_vid') - F('min_vid') + 1.0) * 100, 2) ) diff --git a/netbox/ipam/search.py b/netbox/ipam/search.py index a1cddbb1a..298289b5b 100644 --- a/netbox/ipam/search.py +++ b/netbox/ipam/search.py @@ -156,7 +156,8 @@ class VLANGroupIndex(SearchIndex): ('description', 500), ('max_vid', 2000), ) - display_attrs = ('scope_type', 'min_vid', 'max_vid', 'description') + # display_attrs = ('scope_type', 'min_vid', 'max_vid', 'description') + display_attrs = ('scope_type', 'description') @register_search diff --git a/netbox/ipam/tables/vlans.py b/netbox/ipam/tables/vlans.py index 11de0381c..613089cdf 100644 --- a/netbox/ipam/tables/vlans.py +++ b/netbox/ipam/tables/vlans.py @@ -91,7 +91,7 @@ class VLANGroupTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = VLANGroup fields = ( - 'pk', 'id', 'name', 'scope_type', 'scope', 'min_vid', 'max_vid', 'vlan_count', 'slug', 'description', + 'pk', 'id', 'name', 'scope_type', 'scope', 'vlan_id_ranges', 'vlan_count', 'slug', 'description', 'tags', 'created', 'last_updated', 'actions', 'utilization', ) default_columns = ('pk', 'name', 'scope_type', 'scope', 'vlan_count', 'utilization', 'description') diff --git a/netbox/utilities/forms/fields/array.py b/netbox/utilities/forms/fields/array.py index 01349b9ec..1bd002004 100644 --- a/netbox/utilities/forms/fields/array.py +++ b/netbox/utilities/forms/fields/array.py @@ -52,6 +52,6 @@ class NumericRangeArrayField(forms.CharField): ranges = value.split(",") values = [] for dash_range in value.split(','): - begin, end = dash_range.split('-') - values.append(NumericRange(begin, end)) + lower, upper = dash_range.split('-') + values.append(NumericRange(lower, upper)) return values