Skip to content

Commit aa73a7a

Browse files
committed
Closes #3954: Add device_bays filter for devices and device types
1 parent a4687be commit aa73a7a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

docs/release-notes/version-2.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Enhancements
44

55
* [#3842](https://github.com/netbox-community/netbox/issues/3842) - Add 802.11ax interface type
6+
* [#3954](https://github.com/netbox-community/netbox/issues/3954) - Add `device_bays` filter for devices and device types
67

78
## Bug Fixes
89

netbox/dcim/filters.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import django_filters
22
from django.contrib.auth.models import User
3-
from django.db.models import Q
43

54
from extras.filters import CustomFieldFilterSet, LocalConfigContextFilterSet, CreatedUpdatedFilterSet
65
from tenancy.filters import TenancyFilterSet
@@ -356,6 +355,10 @@ class DeviceTypeFilterSet(CustomFieldFilterSet, CreatedUpdatedFilterSet):
356355
method='_pass_through_ports',
357356
label='Has pass-through ports',
358357
)
358+
device_bays = django_filters.BooleanFilter(
359+
method='_device_bays',
360+
label='Has device bays',
361+
)
359362
tag = TagFilter()
360363

361364
class Meta:
@@ -395,6 +398,9 @@ def _pass_through_ports(self, queryset, name, value):
395398
rearport_templates__isnull=value
396399
)
397400

401+
def _device_bays(self, queryset, name, value):
402+
return queryset.exclude(device_bay_templates__isnull=value)
403+
398404

399405
class DeviceTypeComponentFilterSet(NameSlugSearchFilterSet):
400406
devicetype_id = django_filters.ModelMultipleChoiceFilter(
@@ -623,6 +629,10 @@ class DeviceFilterSet(LocalConfigContextFilterSet, TenancyFilterSet, CustomField
623629
method='_pass_through_ports',
624630
label='Has pass-through ports',
625631
)
632+
device_bays = django_filters.BooleanFilter(
633+
method='_device_bays',
634+
label='Has device bays',
635+
)
626636
tag = TagFilter()
627637

628638
class Meta:
@@ -676,6 +686,9 @@ def _pass_through_ports(self, queryset, name, value):
676686
rearports__isnull=value
677687
)
678688

689+
def _device_bays(self, queryset, name, value):
690+
return queryset.exclude(device_bays__isnull=value)
691+
679692

680693
class DeviceComponentFilterSet(django_filters.FilterSet):
681694
q = django_filters.CharFilter(

netbox/dcim/tests/test_filters.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,11 @@ def test_pass_through_ports(self):
595595
params = {'pass_through_ports': 'false'}
596596
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
597597

598-
# TODO: Add device_bay filter
599-
# def test_device_bays(self):
600-
# params = {'device_bays': 'true'}
601-
# self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
602-
# params = {'device_bays': 'false'}
603-
# self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
598+
def test_device_bays(self):
599+
params = {'device_bays': 'true'}
600+
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
601+
params = {'device_bays': 'false'}
602+
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
604603

605604

606605
class ConsolePortTemplateTestCase(TestCase):
@@ -1322,12 +1321,11 @@ def test_pass_through_ports(self):
13221321
params = {'pass_through_ports': 'false'}
13231322
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
13241323

1325-
# TODO: Add device_bay filter
1326-
# def test_device_bays(self):
1327-
# params = {'device_bays': 'true'}
1328-
# self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
1329-
# params = {'device_bays': 'false'}
1330-
# self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
1324+
def test_device_bays(self):
1325+
params = {'device_bays': 'true'}
1326+
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
1327+
params = {'device_bays': 'false'}
1328+
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
13311329

13321330
def test_local_context_data(self):
13331331
params = {'local_context_data': 'true'}

0 commit comments

Comments
 (0)