Fixes #8604: Fix tag filter on config context list filter form

This commit is contained in:
jeremystretch 2022-02-10 11:56:41 -05:00
parent 3ee3c52e14
commit 41ee4b642f
4 changed files with 20 additions and 5 deletions

View File

@ -8,6 +8,7 @@
* [#8548](https://github.com/netbox-community/netbox/issues/8548) - Fix display of VC members when position is zero * [#8548](https://github.com/netbox-community/netbox/issues/8548) - Fix display of VC members when position is zero
* [#8561](https://github.com/netbox-community/netbox/issues/8561) - Include option to connect a rear port to a console port * [#8561](https://github.com/netbox-community/netbox/issues/8561) - Include option to connect a rear port to a console port
* [#8564](https://github.com/netbox-community/netbox/issues/8564) - Fix errant table configuration key `available_columns` * [#8564](https://github.com/netbox-community/netbox/issues/8564) - Fix errant table configuration key `available_columns`
* [#8604](https://github.com/netbox-community/netbox/issues/8604) - Fix tag filter on config context list filter form
--- ---

View File

@ -317,6 +317,11 @@ class ConfigContextFilterSet(ChangeLoggedModelFilterSet):
to_field_name='slug', to_field_name='slug',
label='Tenant (slug)', label='Tenant (slug)',
) )
tag_id = django_filters.ModelMultipleChoiceFilter(
field_name='tags',
queryset=Tag.objects.all(),
label='Tag',
)
tag = django_filters.ModelMultipleChoiceFilter( tag = django_filters.ModelMultipleChoiceFilter(
field_name='tags__slug', field_name='tags__slug',
queryset=Tag.objects.all(), queryset=Tag.objects.all(),

View File

@ -155,7 +155,7 @@ class TagFilterForm(FilterForm):
class ConfigContextFilterForm(FilterForm): class ConfigContextFilterForm(FilterForm):
field_groups = [ field_groups = [
['q', 'tag'], ['q', 'tag_id'],
['region_id', 'site_group_id', 'site_id'], ['region_id', 'site_group_id', 'site_id'],
['device_type_id', 'platform_id', 'role_id'], ['device_type_id', 'platform_id', 'role_id'],
['cluster_group_id', 'cluster_id'], ['cluster_group_id', 'cluster_id'],
@ -211,9 +211,8 @@ class ConfigContextFilterForm(FilterForm):
required=False, required=False,
label=_('Tenant') label=_('Tenant')
) )
tag = DynamicModelMultipleChoiceField( tag_id = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(), queryset=Tag.objects.all(),
to_field_name='slug',
required=False, required=False,
label=_('Tags') label=_('Tags')
) )

View File

@ -12,7 +12,7 @@ from extras.filtersets import *
from extras.models import * from extras.models import *
from ipam.models import IPAddress from ipam.models import IPAddress
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from utilities.testing import BaseFilterSetTests, ChangeLoggedFilterSetTests from utilities.testing import BaseFilterSetTests, ChangeLoggedFilterSetTests, create_tags
from virtualization.models import Cluster, ClusterGroup, ClusterType from virtualization.models import Cluster, ClusterGroup, ClusterType
@ -429,6 +429,8 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests):
) )
Tenant.objects.bulk_create(tenants) Tenant.objects.bulk_create(tenants)
tags = create_tags('Alpha', 'Bravo', 'Charlie')
for i in range(0, 3): for i in range(0, 3):
is_active = bool(i % 2) is_active = bool(i % 2)
c = ConfigContext.objects.create( c = ConfigContext.objects.create(
@ -446,6 +448,7 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests):
c.clusters.set([clusters[i]]) c.clusters.set([clusters[i]])
c.tenant_groups.set([tenant_groups[i]]) c.tenant_groups.set([tenant_groups[i]])
c.tenants.set([tenants[i]]) c.tenants.set([tenants[i]])
c.tags.set([tags[i]])
def test_name(self): def test_name(self):
params = {'name': ['Config Context 1', 'Config Context 2']} params = {'name': ['Config Context 1', 'Config Context 2']}
@ -516,13 +519,20 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests):
params = {'tenant_group': [tenant_groups[0].slug, tenant_groups[1].slug]} params = {'tenant_group': [tenant_groups[0].slug, tenant_groups[1].slug]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
def test_tenant_(self): def test_tenant(self):
tenants = Tenant.objects.all()[:2] tenants = Tenant.objects.all()[:2]
params = {'tenant_id': [tenants[0].pk, tenants[1].pk]} params = {'tenant_id': [tenants[0].pk, tenants[1].pk]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
params = {'tenant': [tenants[0].slug, tenants[1].slug]} params = {'tenant': [tenants[0].slug, tenants[1].slug]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
def test_tags(self):
tags = Tag.objects.all()[:2]
params = {'tag_id': [tags[0].pk, tags[1].pk]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
params = {'tag': [tags[0].slug, tags[1].slug]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
class TagTestCase(TestCase, ChangeLoggedFilterSetTests): class TagTestCase(TestCase, ChangeLoggedFilterSetTests):
queryset = Tag.objects.all() queryset = Tag.objects.all()