Skip to content

Commit

Permalink
Closes netbox-community#981: Allow filtering primary objects by a giv…
Browse files Browse the repository at this point in the history
…en set of IDs
  • Loading branch information
jeremystretch committed Mar 22, 2017
1 parent ad21bc2 commit b809a10
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion netbox/circuits/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from dcim.models import Site
from extras.filters import CustomFieldFilterSet
from tenancy.models import Tenant
from utilities.filters import NullableModelMultipleChoiceFilter
from utilities.filters import NullableModelMultipleChoiceFilter, NumericInFilter

from .models import Provider, Circuit, CircuitType


class ProviderFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -42,6 +43,7 @@ def search(self, queryset, name, value):


class CircuitFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down
6 changes: 5 additions & 1 deletion netbox/dcim/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from extras.filters import CustomFieldFilterSet
from tenancy.models import Tenant
from utilities.filters import NullableModelMultipleChoiceFilter
from utilities.filters import NullableModelMultipleChoiceFilter, NumericInFilter
from .models import (
ConsolePort, ConsoleServerPort, Device, DeviceRole, DeviceType, IFACE_FF_LAG, Interface, InterfaceConnection,
Manufacturer, Platform, PowerOutlet, PowerPort, Rack, RackGroup, RackReservation, RackRole, Region, Site,
Expand All @@ -14,6 +14,7 @@


class SiteFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -81,6 +82,7 @@ class Meta:


class RackFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -157,6 +159,7 @@ class Meta:


class DeviceTypeFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -191,6 +194,7 @@ def search(self, queryset, name, value):


class DeviceFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down
8 changes: 7 additions & 1 deletion netbox/ipam/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from dcim.models import Site, Device, Interface
from extras.filters import CustomFieldFilterSet
from tenancy.models import Tenant
from utilities.filters import NullableModelMultipleChoiceFilter
from utilities.filters import NullableModelMultipleChoiceFilter, NumericInFilter

from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF


class VRFFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -44,13 +45,15 @@ class Meta:


class RIRFilter(django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')

class Meta:
model = RIR
fields = ['is_private']


class AggregateFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -84,6 +87,7 @@ def search(self, queryset, name, value):


class PrefixFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -182,6 +186,7 @@ def filter_mask_length(self, queryset, name, value):


class IPAddressFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down Expand Up @@ -283,6 +288,7 @@ class Meta:


class VLANFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down
2 changes: 2 additions & 0 deletions netbox/secrets/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

from .models import Secret, SecretRole
from dcim.models import Device
from utilities.filters import NumericInFilter


class SecretFilter(django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down
3 changes: 2 additions & 1 deletion netbox/tenancy/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.db.models import Q

from extras.filters import CustomFieldFilterSet
from utilities.filters import NullableModelMultipleChoiceFilter
from utilities.filters import NullableModelMultipleChoiceFilter, NumericInFilter
from .models import Tenant, TenantGroup


class TenantFilter(CustomFieldFilterSet, django_filters.FilterSet):
id__in = NumericInFilter(name='id', lookup_expr='in')
q = django_filters.CharFilter(
method='search',
label='Search',
Expand Down
11 changes: 11 additions & 0 deletions netbox/utilities/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
from django.utils.encoding import force_text


#
# Filters
#

class NumericInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
"""
Filters for a set of numeric values. Example: id__in=100,200,300
"""
pass


class NullableModelMultipleChoiceField(forms.ModelMultipleChoiceField):
"""
This field operates like a normal ModelMultipleChoiceField except that it allows for one additional choice which is
Expand Down

0 comments on commit b809a10

Please sign in to comment.