1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fix filtering services by port number

This commit is contained in:
Jeremy Stretch
2020-09-21 14:36:58 -04:00
parent b85990daa6
commit 3a90366538
4 changed files with 21 additions and 8 deletions

View File

@ -8,7 +8,7 @@ from dcim.models import Device, Interface, Region, Site
from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
from tenancy.filters import TenancyFilterSet from tenancy.filters import TenancyFilterSet
from utilities.filters import ( from utilities.filters import (
BaseFilterSet, MultiValueCharFilter, MultiValueNumberFilter, NameSlugSearchFilterSet, TagFilter, BaseFilterSet, MultiValueCharFilter, MultiValueNumberFilter, NameSlugSearchFilterSet, NumericArrayFilter, TagFilter,
TreeNodeMultipleChoiceFilter, TreeNodeMultipleChoiceFilter,
) )
from virtualization.models import VirtualMachine, VMInterface from virtualization.models import VirtualMachine, VMInterface
@ -542,6 +542,10 @@ class ServiceFilterSet(BaseFilterSet, CreatedUpdatedFilterSet):
to_field_name='name', to_field_name='name',
label='Virtual machine (name)', label='Virtual machine (name)',
) )
port = NumericArrayFilter(
field_name='ports',
lookup_expr='contains'
)
tag = TagFilter() tag = TagFilter()
class Meta: class Meta:

View File

@ -623,8 +623,9 @@ class ServiceTable(BaseTable):
parent = tables.LinkColumn( parent = tables.LinkColumn(
order_by=('device', 'virtual_machine') order_by=('device', 'virtual_machine')
) )
ports = tables.Column( ports = tables.TemplateColumn(
orderable=False template_code='{{ record.port_list }}',
verbose_name='Ports'
) )
tags = TagColumn( tags = TagColumn(
url_name='ipam:service_list' url_name='ipam:service_list'

View File

@ -763,9 +763,9 @@ class ServiceTestCase(TestCase):
params = {'protocol': ServiceProtocolChoices.PROTOCOL_TCP} params = {'protocol': ServiceProtocolChoices.PROTOCOL_TCP}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
# def test_port(self): def test_port(self):
# params = {'port': ['1001', '1002', '1003']} params = {'port': '1001'}
# self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
def test_device(self): def test_device(self):
devices = Device.objects.all()[:2] devices = Device.objects.all()[:2]

View File

@ -68,7 +68,6 @@ class TreeNodeMultipleChoiceFilter(django_filters.ModelMultipleChoiceFilter):
""" """
Filters for a set of Models, including all descendant models within a Tree. Example: [<Region: R1>,<Region: R2>] Filters for a set of Models, including all descendant models within a Tree. Example: [<Region: R1>,<Region: R2>]
""" """
def get_filter_predicate(self, v): def get_filter_predicate(self, v):
# null value filtering # null value filtering
if v is None: if v is None:
@ -84,7 +83,6 @@ class NullableCharFieldFilter(django_filters.CharFilter):
""" """
Allow matching on null field values by passing a special string used to signify NULL. Allow matching on null field values by passing a special string used to signify NULL.
""" """
def filter(self, qs, value): def filter(self, qs, value):
if value != settings.FILTERS_NULL_CHOICE_VALUE: if value != settings.FILTERS_NULL_CHOICE_VALUE:
return super().filter(qs, value) return super().filter(qs, value)
@ -107,6 +105,16 @@ class TagFilter(django_filters.ModelMultipleChoiceFilter):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
class NumericArrayFilter(django_filters.NumberFilter):
"""
Filter based on the presence of an integer within an ArrayField.
"""
def filter(self, qs, value):
if value:
value = [value]
return super().filter(qs, value)
# #
# FilterSets # FilterSets
# #