Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #17663 - Only remove extraneous attributes from extra if changing to a BooleanFilter #17670

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions netbox/netbox/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ def get_additional_lookups(cls, existing_filter_name, existing_filter):
# create the new filter with the same type because there is no guarantee the defined type
# is the same as the default type for the field
resolve_field(field, lookup_expr) # Will raise FieldLookupError if the lookup is invalid
for field_to_remove in ('choices', 'null_value'):
existing_filter_extra.pop(field_to_remove, None)
filter_cls = django_filters.BooleanFilter if lookup_expr == 'empty' else type(existing_filter)
filter_cls = type(existing_filter)
if lookup_expr == 'empty':
filter_cls = django_filters.BooleanFilter
for param_to_remove in ('choices', 'null_value'):
existing_filter_extra.pop(param_to_remove, None)
new_filter = filter_cls(
field_name=field_name,
lookup_expr=lookup_expr,
Expand Down
25 changes: 20 additions & 5 deletions netbox/utilities/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from dcim.choices import *
from dcim.fields import MACAddressField
from dcim.filtersets import DeviceFilterSet, SiteFilterSet
from dcim.filtersets import DeviceFilterSet, SiteFilterSet, InterfaceFilterSet
from dcim.models import (
Device, DeviceRole, DeviceType, Interface, Manufacturer, Platform, Rack, Region, Site
)
Expand All @@ -16,6 +16,7 @@
from ipam.filtersets import ASNFilterSet
from ipam.models import RIR, ASN
from netbox.filtersets import BaseFilterSet
from wireless.choices import WirelessRoleChoices
from utilities.filters import (
MultiValueCharFilter, MultiValueDateFilter, MultiValueDateTimeFilter, MultiValueMACAddressFilter,
MultiValueNumberFilter, MultiValueTimeFilter, TreeNodeMultipleChoiceFilter,
Expand Down Expand Up @@ -408,9 +409,9 @@ def setUpTestData(cls):
region.save()

sites = (
Site(name='Site 1', slug='abc-site-1', region=regions[0]),
Site(name='Site 2', slug='def-site-2', region=regions[1]),
Site(name='Site 3', slug='ghi-site-3', region=regions[2]),
Site(name='Site 1', slug='abc-site-1', region=regions[0], status=SiteStatusChoices.STATUS_ACTIVE),
Site(name='Site 2', slug='def-site-2', region=regions[1], status=SiteStatusChoices.STATUS_ACTIVE),
Site(name='Site 3', slug='ghi-site-3', region=regions[2], status=SiteStatusChoices.STATUS_PLANNED),
)
Site.objects.bulk_create(sites)

Expand Down Expand Up @@ -438,14 +439,22 @@ def setUpTestData(cls):
Interface(device=devices[1], name='Interface 3', mac_address='00-00-00-00-00-02'),
Interface(device=devices[1], name='Interface 4', mac_address='bb-00-00-00-00-02'),
Interface(device=devices[2], name='Interface 5', mac_address='00-00-00-00-00-03'),
Interface(device=devices[2], name='Interface 6', mac_address='cc-00-00-00-00-03'),
Interface(device=devices[2], name='Interface 6', mac_address='cc-00-00-00-00-03', rf_role=WirelessRoleChoices.ROLE_AP),
)
Interface.objects.bulk_create(interfaces)

def test_site_name_negation(self):
params = {'name__n': ['Site 1']}
self.assertEqual(SiteFilterSet(params, Site.objects.all()).qs.count(), 2)

def test_site_status_icontains(self):
params = {'status__ic': [SiteStatusChoices.STATUS_ACTIVE]}
self.assertEqual(SiteFilterSet(params, Site.objects.all()).qs.count(), 2)

def test_site_status_icontains_negation(self):
params = {'status__nic': [SiteStatusChoices.STATUS_ACTIVE]}
self.assertEqual(SiteFilterSet(params, Site.objects.all()).qs.count(), 1)

def test_site_slug_icontains(self):
params = {'slug__ic': ['-1']}
self.assertEqual(SiteFilterSet(params, Site.objects.all()).qs.count(), 1)
Expand Down Expand Up @@ -553,3 +562,9 @@ def test_device_mac_address_icontains(self):
def test_device_mac_address_icontains_negation(self):
params = {'mac_address__nic': ['aa:', 'bb']}
self.assertEqual(DeviceFilterSet(params, Device.objects.all()).qs.count(), 1)

def test_interface_rf_role_empty(self):
params = {'rf_role__empty': 'true'}
self.assertEqual(InterfaceFilterSet(params, Interface.objects.all()).qs.count(), 5)
params = {'rf_role__empty': 'false'}
self.assertEqual(InterfaceFilterSet(params, Interface.objects.all()).qs.count(), 1)