Skip to content

Commit

Permalink
resolving issues with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
whitej6 committed Nov 17, 2022
1 parent d9320d7 commit 046ce80
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 28 additions & 16 deletions nautobot_firewall_models/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -48,7 +55,7 @@ class Meta:
fields = ["id", "name", "description"]


class AddressObjectFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet):
class AddressObjectFilterSet(BaseFilterSet, NautobotFilterSet):
"""Filter for AddressObject."""

class Meta:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -102,7 +109,7 @@ class Meta:
fields = ["id", "name", "username"]


class UserObjectGroupFilterSet(StatusModelFilterSetMixin, NameDescriptionSearchFilter, NautobotFilterSet):
class UserObjectGroupFilterSet(BaseFilterSet, NautobotFilterSet):
"""Filter for UserObjectGroup."""

class Meta:
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 046ce80

Please sign in to comment.