From 046ce80175b0b11d01a804c0a8dca29b75db68bb Mon Sep 17 00:00:00 2001 From: Jeremy White Date: Wed, 16 Nov 2022 20:08:16 -0600 Subject: [PATCH] resolving issues with filters --- CHANGELOG.md | 6 ++++ nautobot_firewall_models/filters.py | 44 ++++++++++++++++++----------- pyproject.toml | 2 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f9481c..c7ae9729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v1.1.3 - 2022-11-16 + +### Fixed + +- #114 Resolved issues with status filters & deep=True method on Policy detail API view + ## v1.1.2 - 2022-11-12 ### Added diff --git a/nautobot_firewall_models/filters.py b/nautobot_firewall_models/filters.py index 2ff6321a..bb194f54 100644 --- a/nautobot_firewall_models/filters.py +++ b/nautobot_firewall_models/filters.py @@ -2,21 +2,28 @@ import django_filters from django.db.models import Q -from django_filters import CharFilter, FilterSet +from django_filters import CharFilter +from django_filters.filterset import FilterSet from nautobot.dcim.models import Device -from nautobot.extras.filters import StatusModelFilterSetMixin, NautobotFilterSet +from nautobot.extras.filters import NautobotFilterSet +from nautobot.extras.models import Status from nautobot.utilities.filters import TagFilter from nautobot_firewall_models import models -class NameDescriptionSearchFilter(FilterSet): +class BaseFilterSet(FilterSet): """A base class for adding the search method to models which only expose the `name` and `description` fields.""" q = CharFilter( method="search", label="Search", ) + status = django_filters.ModelMultipleChoiceFilter( + field_name="status__slug", + to_field_name="slug", + queryset=Status.objects.all(), + ) def search(self, queryset, name, value): # pylint: disable=unused-argument, no-self-use """Construct Q filter for filterset.""" @@ -25,7 +32,7 @@ def search(self, queryset, name, value): # pylint: disable=unused-argument, no- return queryset.filter(Q(name__icontains=value) | Q(description__icontains=value)) -class IPRangeFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class IPRangeFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for IPRange.""" class Meta: @@ -37,7 +44,7 @@ class Meta: fields = ["id", "vrf", "size", "description"] -class FQDNFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class FQDNFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for FQDN.""" class Meta: @@ -48,7 +55,7 @@ class Meta: fields = ["id", "name", "description"] -class AddressObjectFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class AddressObjectFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for AddressObject.""" class Meta: @@ -59,7 +66,7 @@ class Meta: fields = ["id", "name", "ip_address", "prefix", "ip_range", "fqdn", "description"] -class AddressObjectGroupFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class AddressObjectGroupFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for AddressObjectGroup.""" class Meta: @@ -70,7 +77,7 @@ class Meta: fields = ["id", "name", "address_objects", "description"] -class ServiceObjectFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class ServiceObjectFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for ServiceObject.""" class Meta: @@ -81,7 +88,7 @@ class Meta: fields = ["id", "name", "ip_protocol", "port", "description"] -class ServiceObjectGroupFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class ServiceObjectGroupFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for ServiceObjectGroup.""" class Meta: @@ -92,7 +99,7 @@ class Meta: fields = ["id", "name", "service_objects", "description"] -class UserObjectFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class UserObjectFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for UserObject.""" class Meta: @@ -102,7 +109,7 @@ class Meta: fields = ["id", "name", "username"] -class UserObjectGroupFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class UserObjectGroupFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for UserObjectGroup.""" class Meta: @@ -112,7 +119,7 @@ class Meta: fields = ["id", "name", "user_objects", "description"] -class ZoneFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class ZoneFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for Zone.""" class Meta: @@ -124,7 +131,7 @@ class Meta: # TODO: Refactor to allow for better filtering, currently very limited. -class PolicyRuleFilterSet(StatusModelFilterSetMixin, NautobotFilterSet): +class PolicyRuleFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for PolicyRule.""" tag = TagFilter() @@ -150,7 +157,7 @@ class Meta: # TODO: Refactor to allow for better filtering, currently very limited. -class NATPolicyRuleFilterSet(StatusModelFilterSetMixin, NautobotFilterSet): +class NATPolicyRuleFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for NATPolicyRule.""" tag = TagFilter() @@ -175,7 +182,7 @@ class Meta: fields = ["id", "remark", "log", "request_id"] -class PolicyFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class PolicyFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for Policy.""" class Meta: @@ -184,8 +191,13 @@ class Meta: model = models.Policy fields = ["id", "name", "description", "policy_rules", "assigned_devices", "assigned_dynamic_groups"] + def __init__(self, data=None, queryset=None, *, request=None, prefix=None): + """Overload init to allow for deep=True on detail API call.""" + super().__init__(data, queryset, request=request, prefix=prefix) + self.data.pop("deep", None) + -class NATPolicyFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet): +class NATPolicyFilterSet(BaseFilterSet, NautobotFilterSet): """Filter for NATPolicy.""" class Meta: diff --git a/pyproject.toml b/pyproject.toml index 3fb67cef..d928bd88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "nautobot-firewall-models" -version = "1.1.2" +version = "1.1.3" description = "Nautobot plugin to model firewall objects." authors = ["Network to Code, LLC "] license = "Apache-2.0"