Skip to content

Commit

Permalink
Fixed filter for device, virtual_machine, virtual_chassi, site, site_…
Browse files Browse the repository at this point in the history
…group, and region.
  • Loading branch information
Julio-Oliveira-Encora committed Jun 13, 2024
1 parent 322325f commit f47a4c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
24 changes: 22 additions & 2 deletions netbox_acls/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
when filtering the sites list by status or region, for instance.
"""
import django_filters
from dcim.models import Device, Interface, VirtualChassis
from dcim.models import Device, Interface, Region, Site, SiteGroup, VirtualChassis
from django.db.models import Q
from netbox.filtersets import NetBoxModelFilterSet
from virtualization.models import VirtualMachine, VMInterface
Expand All @@ -22,7 +22,24 @@ class AccessListFilterSet(NetBoxModelFilterSet):
"""
Define the filter set for the django model AccessList.
"""

region = django_filters.ModelMultipleChoiceFilter(
field_name="device__site__region",
queryset=Region.objects.all(),
to_field_name="id",
label="Region",
)
site_group = django_filters.ModelMultipleChoiceFilter(
field_name="device__site__group",
queryset=SiteGroup.objects.all(),
to_field_name="id",
label="Site Group",
)
site = django_filters.ModelMultipleChoiceFilter(
field_name="device__site",
queryset=Site.objects.all(),
to_field_name="id",
label="Site",
)
device = django_filters.ModelMultipleChoiceFilter(
field_name="device__name",
queryset=Device.objects.all(),
Expand Down Expand Up @@ -75,6 +92,9 @@ class Meta:
"type",
"default_action",
"comments",
"site",
"site_group",
"region",
)

def search(self, queryset, name, value):
Expand Down
17 changes: 11 additions & 6 deletions netbox_acls/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dcim.models import Device, Interface, Region, Site, SiteGroup, VirtualChassis
from django import forms
from django.utils.translation import gettext as _
from ipam.models import Prefix
from netbox.forms import NetBoxModelFilterSetForm
from utilities.forms.fields import (
Expand All @@ -14,6 +15,7 @@
from utilities.forms.utils import add_blank_choice
from virtualization.models import VirtualMachine, VMInterface


from ..choices import (
ACLActionChoices,
ACLAssignmentDirectionChoices,
Expand Down Expand Up @@ -56,22 +58,25 @@ class AccessListFilterForm(NetBoxModelFilterSetForm):
required=False,
query_params={"region_id": "$region", "group_id": "$site_group"},
)
device = DynamicModelChoiceField(
device_id = DynamicModelChoiceField(
queryset=Device.objects.all(),
query_params={
"region_id": "$region",
"group_id": "$site_group",
"site_id": "$site",
},
required=False,
label=_("Device",),
)
virtual_machine = DynamicModelChoiceField(
virtual_machine_id = DynamicModelChoiceField(
queryset=VirtualMachine.objects.all(),
required=False,
label=_("Virtual Machine",)
)
virtual_chassis = DynamicModelChoiceField(
virtual_chassis_id = DynamicModelChoiceField(
queryset=VirtualChassis.objects.all(),
required=False,
label=_("Virtual Chassis",)
)
type = forms.ChoiceField(
choices=add_blank_choice(ACLTypeChoices),
Expand All @@ -92,9 +97,9 @@ class AccessListFilterForm(NetBoxModelFilterSetForm):
"region",
"site_group",
"site",
"device",
"virtual_chassis",
"virtual_machine",
"device_id",
"virtual_chassis_id",
"virtual_machine_id",
),
),
("ACL Details", ("type", "default_action")),
Expand Down

0 comments on commit f47a4c6

Please sign in to comment.