Skip to content

Commit

Permalink
Closes netbox-community#11110: Add start_address and end_address filt…
Browse files Browse the repository at this point in the history
…ers for IP ranges
  • Loading branch information
jeremystretch committed Feb 17, 2023
1 parent c36e7a1 commit c031951
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/version-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

* [#11110](https://github.com/netbox-community/netbox/issues/11110) - Add `start_address` and `end_address` filters for IP ranges
* [#11592](https://github.com/netbox-community/netbox/issues/11592) - Introduce `FILE_UPLOAD_MAX_MEMORY_SIZE` configuration parameter

### Bug Fixes
Expand Down
14 changes: 14 additions & 0 deletions netbox/ipam/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ class IPRangeFilterSet(TenancyFilterSet, NetBoxModelFilterSet):
field_name='start_address',
lookup_expr='family'
)
start_address = MultiValueCharFilter(
method='filter_address',
label=_('Address'),
)
end_address = MultiValueCharFilter(
method='filter_address',
label=_('Address'),
)
contains = django_filters.CharFilter(
method='search_contains',
label=_('Ranges which contain this prefix or IP'),
Expand Down Expand Up @@ -461,6 +469,12 @@ def search_contains(self, queryset, name, value):
except (AddrFormatError, ValueError):
return queryset.none()

def filter_address(self, queryset, name, value):
try:
return queryset.filter(**{f'{name}__net_in': value})
except ValidationError:
return queryset.none()


class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
family = django_filters.NumberFilter(
Expand Down
8 changes: 8 additions & 0 deletions netbox/ipam/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,14 @@ def test_family(self):
params = {'family': '6'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)

def test_start_address(self):
params = {'start_address': ['10.0.1.100', '10.0.2.100']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_end_address(self):
params = {'end_address': ['10.0.1.199', '10.0.2.199']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_contains(self):
params = {'contains': '10.0.1.150/24'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
Expand Down

0 comments on commit c031951

Please sign in to comment.