Skip to content

Commit

Permalink
Closes #485: Add Ignore Termination Types to "regular" filters (#546)
Browse files Browse the repository at this point in the history
* add ignore_cable_type filter

* deleted commented command

* corrected saved filter for ignore_cable_type
  • Loading branch information
dreng authored Aug 9, 2024
1 parent 6f43b95 commit 9072faf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion netbox_topology_views/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ 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"] != '':
try:
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']
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion netbox_topology_views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
),
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion netbox_topology_views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 7 additions & 3 deletions netbox_topology_views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -725,14 +724,15 @@ 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"] != '':
try:
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']
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand Down

0 comments on commit 9072faf

Please sign in to comment.