From 68cb8b68957c5b83cec53bafa20a2a5599896f35 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 19 Dec 2018 10:02:18 -0500 Subject: [PATCH] Closes #2701: Enable filtering of prefixes by exact prefix value --- CHANGELOG.md | 4 ++++ netbox/ipam/filters.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ddf44d2129..9a93302b0ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ v2.5.2 (FUTURE) +## Enhancements + +* [#2701](https://github.com/digitalocean/netbox/issues/2701) - Enable filtering of prefixes by exact prefix value + ## Bug Fixes * [#2707](https://github.com/digitalocean/netbox/issues/2707) - Correct permission evaluation for circuit termination cabling diff --git a/netbox/ipam/filters.py b/netbox/ipam/filters.py index abef95c455e..22df1b1c922 100644 --- a/netbox/ipam/filters.py +++ b/netbox/ipam/filters.py @@ -112,6 +112,10 @@ class PrefixFilter(CustomFieldFilterSet, django_filters.FilterSet): method='search', label='Search', ) + prefix = django_filters.CharFilter( + method='filter_prefix', + label='Prefix', + ) within = django_filters.CharFilter( method='search_within', label='Within prefix', @@ -197,6 +201,15 @@ def search(self, queryset, name, value): pass return queryset.filter(qs_filter) + def filter_prefix(self, queryset, name, value): + if not value.strip(): + return queryset + try: + query = str(netaddr.IPNetwork(value).cidr) + return queryset.filter(prefix=query) + except ValidationError: + return queryset.none() + def search_within(self, queryset, name, value): value = value.strip() if not value: