From f3fe05b476d26bd1bbbe089ee7413d6f356a40b4 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Sat, 29 Jul 2023 17:29:10 +0530 Subject: [PATCH 1/2] adds assigned bool for ip address API #13151 --- netbox/ipam/filtersets.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index d011472d932..9b57cb273cf 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -591,6 +591,10 @@ class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet): method='_assigned_to_interface', label=_('Is assigned to an interface'), ) + assigned = django_filters.BooleanFilter( + method='_assigned', + label=_('Is assigned'), + ) status = django_filters.MultipleChoiceFilter( choices=IPAddressStatusChoices, null_value=None @@ -706,6 +710,18 @@ def _assigned_to_interface(self, queryset, name, value): assigned_object_id__isnull=False ) + def _assigned(self, queryset, name, value): + if value: + return queryset.exclude( + assigned_object_type__isnull=True, + assigned_object_id__isnull=True + ) + else: + return queryset.filter( + assigned_object_type__isnull=True, + assigned_object_id__isnull=True + ) + class FHRPGroupFilterSet(NetBoxModelFilterSet): protocol = django_filters.MultipleChoiceFilter( From 3aed80b3078f3075a76dcb2fa4d9335fb023f33a Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 2 Aug 2023 15:31:56 -0400 Subject: [PATCH 2/2] Add filterset test --- netbox/ipam/tests/test_filtersets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/netbox/ipam/tests/test_filtersets.py b/netbox/ipam/tests/test_filtersets.py index 3d9a6656713..0ae7544ab9a 100644 --- a/netbox/ipam/tests/test_filtersets.py +++ b/netbox/ipam/tests/test_filtersets.py @@ -992,6 +992,12 @@ def test_fhrpgroup(self): params = {'fhrpgroup_id': [fhrp_groups[0].pk, fhrp_groups[1].pk]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_assigned(self): + params = {'assigned': 'true'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 8) + params = {'assigned': 'false'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + def test_assigned_to_interface(self): params = {'assigned_to_interface': 'true'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6)