From 9072faf7cf62fae7757d871638ccfba50b24361a Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 9 Aug 2024 15:09:16 +0200 Subject: [PATCH] Closes #485: Add Ignore Termination Types to "regular" filters (#546) * add ignore_cable_type filter * deleted commented command * corrected saved filter for ignore_cable_type --- netbox_topology_views/api/views.py | 4 +++- netbox_topology_views/forms.py | 7 ++++++- netbox_topology_views/utils.py | 6 +++++- netbox_topology_views/views.py | 10 +++++++--- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/netbox_topology_views/api/views.py b/netbox_topology_views/api/views.py index 662ded4..564dc3d 100644 --- a/netbox_topology_views/api/views.py +++ b/netbox_topology_views/api/views.py @@ -109,7 +109,7 @@ def list(self, request): if request.GET: - filter_id, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables = get_query_settings(request) + filter_id, ignore_cable_type, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables = get_query_settings(request) # Read options from saved filters as NetBox does not handle custom plugin filters if "filter_id" in request.GET and request.GET["filter_id"] != '': @@ -117,6 +117,7 @@ def list(self, request): saved_filter = SavedFilter.objects.get(pk=filter_id) saved_filter_params = getattr(saved_filter, 'parameters') + if ignore_cable_type == () and 'ignore_cable_type' in saved_filter_params: ignore_cable_type = saved_filter_params['ignore_cable_type'] if save_coords == False and 'save_coords' in saved_filter_params: save_coords = saved_filter_params['save_coords'] if show_power == False and 'show_power' in saved_filter_params: show_power = saved_filter_params['show_power'] if show_circuit == False and 'show_circuit' in saved_filter_params: show_circuit = saved_filter_params['show_circuit'] @@ -145,6 +146,7 @@ def list(self, request): topo_data = get_topology_data( queryset=self.queryset, individualOptions=individualOptions, + ignore_cable_type=ignore_cable_type, save_coords=save_coords, show_unconnected=show_unconnected, show_cables=show_cables, diff --git a/netbox_topology_views/forms.py b/netbox_topology_views/forms.py index 2735203..0be4e13 100644 --- a/netbox_topology_views/forms.py +++ b/netbox_topology_views/forms.py @@ -34,7 +34,7 @@ class DeviceFilterForm( fieldsets = ( FieldSet('q', 'filter_id', 'tag'), FieldSet( - 'group', 'save_coords', 'show_unconnected', 'show_cables', 'show_logical_connections', + 'group', 'ignore_cable_type', 'save_coords', 'show_unconnected', 'show_cables', 'show_logical_connections', 'show_single_cable_logical_conns', 'show_neighbors', 'show_circuit', 'show_power', 'show_wireless', 'group_sites', 'group_locations', 'group_racks', 'group_virtualchassis', 'straight_cables', name=_("Options") ), @@ -229,6 +229,11 @@ class DeviceFilterForm( tag = TagFilterField(model) # options + ignore_cable_type = forms.MultipleChoiceField( + label=_('Ignore Termination Types'), + required=False, + choices=IndividualOptions.CHOICES + ) save_coords = forms.NullBooleanField( label=_('Save Coordinates'), required=False, diff --git a/netbox_topology_views/utils.py b/netbox_topology_views/utils.py index 4cc47c6..53163b4 100644 --- a/netbox_topology_views/utils.py +++ b/netbox_topology_views/utils.py @@ -123,6 +123,10 @@ def get_query_settings(request): save_coords = True # Individual options + ignore_cable_type = () + if "ignore_cable_type" in request.GET: + ignore_cable_type = request.GET.getlist('ignore_cable_type') + show_unconnected = False if "show_unconnected" in request.GET: if request.GET["show_unconnected"] == "True": @@ -192,7 +196,7 @@ def get_query_settings(request): if request.GET["straight_cables"] == "True": straight_cables = True - return filter_id, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables + return filter_id, ignore_cable_type, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables class LinePattern(): wireless = [2, 10, 2, 10] diff --git a/netbox_topology_views/views.py b/netbox_topology_views/views.py index 3ba6737..bca1615 100644 --- a/netbox_topology_views/views.py +++ b/netbox_topology_views/views.py @@ -334,6 +334,7 @@ def get_topology_data( queryset: QuerySet, individualOptions: IndividualOptions, show_unconnected: bool, + ignore_cable_type: list, save_coords: bool, show_cables: bool, show_circuit: bool, @@ -369,8 +370,6 @@ def get_topology_data( cable_ids = DefaultDict(dict) interface_ids = DefaultDict(dict) - ignore_cable_type = individualOptions.ignore_cable_type - device_ids = [d.pk for d in queryset] site_ids = [d.site_id for d in queryset] @@ -725,7 +724,7 @@ def get(self, request): if request.GET: - filter_id, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables = get_query_settings(request) + filter_id, ignore_cable_type, save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless, group_sites, group_locations, group_racks, group_virtualchassis, group, show_neighbors, straight_cables = get_query_settings(request) # Read options from saved filters as NetBox does not handle custom plugin filters if "filter_id" in request.GET and request.GET["filter_id"] != '': @@ -733,6 +732,7 @@ def get(self, request): saved_filter = SavedFilter.objects.get(pk=filter_id) saved_filter_params = getattr(saved_filter, 'parameters') + if ignore_cable_type == () and 'ignore_cable_type' in saved_filter_params: ignore_cable_type = saved_filter_params['ignore_cable_type'] if save_coords == False and 'save_coords' in saved_filter_params: save_coords = saved_filter_params['save_coords'] if show_power == False and 'show_power' in saved_filter_params: show_power = saved_filter_params['show_power'] if show_circuit == False and 'show_circuit' in saved_filter_params: show_circuit = saved_filter_params['show_circuit'] @@ -763,6 +763,7 @@ def get(self, request): topo_data = get_topology_data( queryset=self.queryset, individualOptions=individualOptions, + ignore_cable_type=ignore_cable_type, save_coords=save_coords, show_unconnected=show_unconnected, show_cables=show_cables, @@ -784,10 +785,13 @@ def get(self, request): # No GET-Request in URL. We most likely came here from the navigation menu. preselected_device_roles = IndividualOptions.objects.get(id=individualOptions.id).preselected_device_roles.all().values_list('id', flat=True) preselected_tags = IndividualOptions.objects.get(id=individualOptions.id).preselected_tags.all().values_list(Lower('name'), flat=True) + ignore_cable_type = IndividualOptions.objects.get(id=individualOptions.id).ignore_cable_type.translate({ord(i): None for i in '[]\''}).split(', ') + if ignore_cable_type == ['']: ignore_cable_type = [] q = QueryDict(mutable=True) q.setlist("role_id", list(preselected_device_roles)) q.setlist("tag", list(preselected_tags)) + q.setlist("ignore_cable_type", ignore_cable_type) if individualOptions.save_coords: q['save_coords'] = "True" if individualOptions.show_unconnected: q['show_unconnected'] = "True"