Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change filters for 2.x #565

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 55 additions & 95 deletions nautobot_golden_config/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nautobot.core.filters import MultiValueDateTimeFilter, TreeNodeMultipleChoiceFilter
from nautobot.dcim.models import Device, DeviceType, Manufacturer, Platform, Rack, RackGroup, Location
from nautobot.dcim.filters import DeviceFilterSet
from nautobot.extras.filters import StatusFilter
from nautobot.extras.filters import NaturalKeyOrPKMultipleChoiceFilter, StatusFilter
from nautobot.extras.filters import NautobotFilterSet
from nautobot.extras.models import Status, Role
from nautobot.tenancy.models import Tenant, TenantGroup
Expand Down Expand Up @@ -61,118 +61,90 @@ def _get_filter_lookup_dict(existing_filter):
)
tenant_group_id = TreeNodeMultipleChoiceFilter(
queryset=TenantGroup.objects.all(),
field_name="device__tenant__group",
field_name="device__tenant__tenant_group",
to_field_name="id",
label="Tenant Group (ID)",
)
tenant_group = TreeNodeMultipleChoiceFilter(
queryset=TenantGroup.objects.all(),
field_name="device__tenant__group",
to_field_name="slug",
label="Tenant Group (slug)",
field_name="device__tenant__tenant_group",
to_field_name="name",
label="Tenant Group (name)",
)
tenant_id = django_filters.ModelMultipleChoiceFilter(
tenant = NaturalKeyOrPKMultipleChoiceFilter(
queryset=Tenant.objects.all(),
field_name="device__tenant_id",
label="Tenant (ID)",
field_name="device__tenant",
to_field_name="name",
label="Tenant (name or ID)",
)
tenant = django_filters.ModelMultipleChoiceFilter(
queryset=Tenant.objects.all(),
field_name="device__tenant__slug",
to_field_name="slug",
label="Tenant (slug)",
location_id = TreeNodeMultipleChoiceFilter(
# Not limiting to content_type=dcim.device to allow parent locations to be included
# i.e. include all Sites in a Region, even though Region can't be assigned to a Device
queryset=Location.objects.all(),
field_name="device__location",
to_field_name="id",
label="Location (ID)",
)
location = TreeNodeMultipleChoiceFilter(
# Not limiting to content_type=dcim.device to allow parent locations to be included
# i.e. include all sites in a Region, even though Region can't be assigned to a Device
queryset=Location.objects.all(),
field_name="device__location__id",
to_field_name="pk",
field_name="device__location",
to_field_name="name",
label="Location (ID)",
)
rack_group_id = TreeNodeMultipleChoiceFilter(
queryset=RackGroup.objects.all(),
field_name="device__rack__group",
field_name="device__rack__rack_group",
to_field_name="id",
label="Rack group (ID)",
)
rack_group = TreeNodeMultipleChoiceFilter(
field_name="device__rack__group",
queryset=RackGroup.objects.all(),
label="Rack group (slug)",
field_name="device__rack__rack_group",
to_field_name="name",
label="Rack group (name)",
)
rack_id = django_filters.ModelMultipleChoiceFilter(
rack = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device__rack",
queryset=Rack.objects.all(),
label="Rack (ID)",
)
rack = django_filters.ModelMultipleChoiceFilter(
field_name="device__rack__name",
queryset=Rack.objects.all(),
to_field_name="name",
label="Rack (name)",
)
role_id = django_filters.ModelMultipleChoiceFilter(
field_name="device__device_role_id",
queryset=Role.objects.all(), # TODO: How does change to Role model affect this?
label="Role (ID)",
label="Rack (name or ID)",
)
role = django_filters.ModelMultipleChoiceFilter(
field_name="device__device_role__slug",
role = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device__role",
queryset=Role.objects.all(), # TODO: How does change to Role model affect this?
to_field_name="slug",
label="Role (slug)",
to_field_name="name",
label="Role (name or ID)",
)
manufacturer_id = django_filters.ModelMultipleChoiceFilter(
manufacturer = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device__device_type__manufacturer",
queryset=Manufacturer.objects.all(),
label="Manufacturer (ID)",
)
manufacturer = django_filters.ModelMultipleChoiceFilter(
field_name="device__device_type__manufacturer__slug",
queryset=Manufacturer.objects.all(),
to_field_name="slug",
label="Manufacturer (slug)",
to_field_name="name",
label="Manufacturer (name or ID)",
)
platform_id = django_filters.ModelMultipleChoiceFilter(
platform = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device__platform",
queryset=Platform.objects.all(),
label="Platform (ID)",
)
platform = django_filters.ModelMultipleChoiceFilter(
field_name="device__platform__slug",
queryset=Platform.objects.all(),
to_field_name="slug",
label="Platform (slug)",
)
device_status_id = StatusFilter(
field_name="device__status_id",
queryset=Status.objects.all(),
label="Device Status",
to_field_name="name",
label="Platform (name or ID)",
)
device_status = StatusFilter(
field_name="device__status",
queryset=Status.objects.all(),
label="Device Status",
)
device_type_id = django_filters.ModelMultipleChoiceFilter(
field_name="device__device_type_id",
device_type = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device__device_type",
queryset=DeviceType.objects.all(),
label="Device type (ID)",
to_field_name="model",
label="DeviceType (model or ID)",
)
device_type = django_filters.ModelMultipleChoiceFilter(
field_name="device__device_type__slug",
queryset=DeviceType.objects.all(),
to_field_name="slug",
label="DeviceType (slug)",
)
device_id = django_filters.ModelMultipleChoiceFilter(
queryset=Device.objects.all(),
label="Device ID",
)
device = django_filters.ModelMultipleChoiceFilter(
field_name="device__name",
device = NaturalKeyOrPKMultipleChoiceFilter(
field_name="device",
queryset=Device.objects.all(),
to_field_name="name",
label="Device Name",
label="Device (name or ID)",
)

def search(self, queryset, name, value): # pylint: disable=unused-argument
Expand Down Expand Up @@ -253,15 +225,11 @@ class ComplianceRuleFilterSet(NautobotFilterSet):
method="search",
label="Search",
)
platform_id = django_filters.ModelMultipleChoiceFilter(
queryset=Platform.objects.all(),
label="Platform (ID)",
)
platform = django_filters.ModelMultipleChoiceFilter(
field_name="platform__slug",
platform = NaturalKeyOrPKMultipleChoiceFilter(
field_name="platform",
queryset=Platform.objects.all(),
to_field_name="slug",
label="Platform (slug)",
to_field_name="name",
label="Platform (name or ID)",
)

def search(self, queryset, name, value): # pylint: disable=unused-argument
Expand All @@ -285,15 +253,11 @@ class ConfigRemoveFilterSet(NautobotFilterSet):
method="search",
label="Search",
)
platform_id = django_filters.ModelMultipleChoiceFilter(
platform = NaturalKeyOrPKMultipleChoiceFilter(
field_name="platform",
queryset=Platform.objects.all(),
label="Platform (ID)",
)
platform = django_filters.ModelMultipleChoiceFilter(
field_name="platform__slug",
queryset=Platform.objects.all(),
to_field_name="slug",
label="Platform (slug)",
to_field_name="name",
label="Platform (name or ID)",
)

def search(self, queryset, name, value): # pylint: disable=unused-argument
Expand All @@ -317,15 +281,11 @@ class ConfigReplaceFilterSet(NautobotFilterSet):
method="search",
label="Search",
)
platform_id = django_filters.ModelMultipleChoiceFilter(
queryset=Platform.objects.all(),
label="Platform (ID)",
)
platform = django_filters.ModelMultipleChoiceFilter(
field_name="platform__slug",
platform = NaturalKeyOrPKMultipleChoiceFilter(
field_name="platform",
queryset=Platform.objects.all(),
to_field_name="slug",
label="Platform (slug)",
to_field_name="name",
label="Platform (name or ID)",
)

def search(self, queryset, name, value): # pylint: disable=unused-argument
Expand Down
43 changes: 32 additions & 11 deletions nautobot_golden_config/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class ConfigComplianceFilterForm(NautobotFilterForm):
"q",
"tenant_group",
"tenant",
"location_id",
"location",
"rack_group_id",
"rack_group",
"rack_id",
"role",
"manufacturer",
Expand All @@ -36,27 +38,46 @@ class ConfigComplianceFilterForm(NautobotFilterForm):
]

q = forms.CharField(required=False, label="Search")
tenant_group_id = core_forms.DynamicModelMultipleChoiceField(
queryset=TenantGroup.objects.all(), to_field_name="id", required=False, label="Tenant group ID"
)
tenant_group = core_forms.DynamicModelMultipleChoiceField(
queryset=TenantGroup.objects.all(), to_field_name="slug", required=False, null_option="None"
queryset=TenantGroup.objects.all(),
to_field_name="name",
required=False,
label="Tenant group name",
null_option="None",
)
tenant = core_forms.DynamicModelMultipleChoiceField(
queryset=Tenant.objects.all(),
to_field_name="slug",
to_field_name="name",
required=False,
null_option="None",
query_params={"group": "$tenant_group"},
)
location = core_forms.DynamicModelMultipleChoiceField(
location_id = core_forms.DynamicModelMultipleChoiceField(
# Not limiting to query_params={"content_type": "dcim.device" to allow parent locations to be included
# i.e. include all sites in a Region, even though Region can't be assigned to a Device
queryset=Location.objects.all(),
to_field_name="pk",
to_field_name="id",
required=False,
label="Location ID",
)
location = core_forms.DynamicModelMultipleChoiceField(
queryset=Location.objects.all(), to_field_name="name", required=False, label="Location name"
)
rack_group_id = core_forms.DynamicModelMultipleChoiceField(
queryset=RackGroup.objects.all(),
to_field_name="id",
required=False,
label="Rack group",
label="Rack group ID",
query_params={"location": "$location"},
)
rack_group = core_forms.DynamicModelMultipleChoiceField(
queryset=RackGroup.objects.all(),
to_field_name="name",
required=False,
label="Rack group name",
query_params={"location": "$location"},
)
rack_id = core_forms.DynamicModelMultipleChoiceField(
Expand All @@ -71,11 +92,11 @@ class ConfigComplianceFilterForm(NautobotFilterForm):
)
role = core_forms.DynamicModelMultipleChoiceField(
queryset=Role.objects.all(),
to_field_name="slug",
required=False, # TODO: Fix slug field, Test with change to Role model
to_field_name="name",
required=False, # TODO: Test with change to Role model
)
manufacturer = core_forms.DynamicModelMultipleChoiceField(
queryset=Manufacturer.objects.all(), to_field_name="slug", required=False, label="Manufacturer"
queryset=Manufacturer.objects.all(), to_field_name="name", required=False, label="Manufacturer"
)
device_type_id = core_forms.DynamicModelMultipleChoiceField(
queryset=DeviceType.objects.all(),
Expand All @@ -86,7 +107,7 @@ class ConfigComplianceFilterForm(NautobotFilterForm):
)

platform = core_forms.DynamicModelMultipleChoiceField(
queryset=Platform.objects.all(), to_field_name="slug", required=False, null_option="None"
queryset=Platform.objects.all(), to_field_name="name", required=False, null_option="None"
)
device_id = core_forms.DynamicModelMultipleChoiceField(
queryset=Device.objects.all(), required=False, null_option="None", label="Device"
Expand All @@ -101,7 +122,7 @@ def __init__(self, *args, **kwargs):
query_params={"content_types": Device._meta.label_lower},
display_field="label",
label="Device Status",
to_field_name="slug",
to_field_name="name",
)
self.order_fields(self.field_order) # Reorder fields again

Expand Down Expand Up @@ -236,7 +257,7 @@ class ConfigRemoveFilterForm(NautobotFilterForm):

model = models.ConfigRemove
platform = core_forms.DynamicModelMultipleChoiceField(
queryset=Platform.objects.all(), to_field_name="slug", required=False, null_option="None"
queryset=Platform.objects.all(), to_field_name="name", required=False, null_option="None"
)
name = core_forms.DynamicModelChoiceField(
queryset=models.ConfigRemove.objects.all(), to_field_name="name", required=False
Expand Down

This file was deleted.

Loading
Loading