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

Closes #14631: Ensure description filters are available on all relevant models #14632

Merged
merged 2 commits into from
Dec 28, 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
4 changes: 3 additions & 1 deletion netbox/circuits/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ class ProviderFilterSet(NetBoxModelFilterSet, ContactModelFilterSet):

class Meta:
model = Provider
fields = ['id', 'name', 'slug']
fields = ['id', 'name', 'slug', 'description']

def search(self, queryset, name, value):
if not value.strip():
return queryset
return queryset.filter(
Q(name__icontains=value) |
Q(description__icontains=value) |
Q(accounts__account__icontains=value) |
Q(accounts__name__icontains=value) |
Q(comments__icontains=value)
Expand Down Expand Up @@ -101,6 +102,7 @@ def search(self, queryset, name, value):
return queryset
return queryset.filter(
Q(name__icontains=value) |
Q(description__icontains=value) |
Q(account__icontains=value) |
Q(comments__icontains=value)
).distinct()
Expand Down
32 changes: 30 additions & 2 deletions netbox/circuits/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def setUpTestData(cls):
ASN.objects.bulk_create(asns)

providers = (
Provider(name='Provider 1', slug='provider-1'),
Provider(name='Provider 2', slug='provider-2'),
Provider(name='Provider 1', slug='provider-1', description='foobar1'),
Provider(name='Provider 2', slug='provider-2', description='foobar2'),
Provider(name='Provider 3', slug='provider-3'),
Provider(name='Provider 4', slug='provider-4'),
Provider(name='Provider 5', slug='provider-5'),
Expand Down Expand Up @@ -74,6 +74,10 @@ def setUpTestData(cls):
CircuitTermination(circuit=circuits[1], site=sites[0], term_side='A'),
))

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_name(self):
params = {'name': ['Provider 1', 'Provider 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand All @@ -82,6 +86,10 @@ def test_slug(self):
params = {'slug': ['provider-1', 'provider-2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_description(self):
params = {'description': ['foobar1', 'foobar2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_asn_id(self): # ASN object assignment
asns = ASN.objects.all()[:2]
params = {'asn_id': [asns[0].pk, asns[1].pk]}
Expand Down Expand Up @@ -122,6 +130,10 @@ def setUpTestData(cls):
CircuitType(name='Circuit Type 3', slug='circuit-type-3'),
))

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_name(self):
params = {'name': ['Circuit Type 1']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
Expand Down Expand Up @@ -227,6 +239,10 @@ def setUpTestData(cls):
))
CircuitTermination.objects.bulk_create(circuit_terminations)

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_cid(self):
params = {'cid': ['Test Circuit 1', 'Test Circuit 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down Expand Up @@ -369,6 +385,10 @@ def setUpTestData(cls):

Cable(a_terminations=[circuit_terminations[0]], b_terminations=[circuit_terminations[1]]).save()

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_term_side(self):
params = {'term_side': 'A'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 7)
Expand Down Expand Up @@ -440,6 +460,10 @@ def setUpTestData(cls):
)
ProviderNetwork.objects.bulk_create(provider_networks)

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_name(self):
params = {'name': ['Provider Network 1', 'Provider Network 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down Expand Up @@ -477,6 +501,10 @@ def setUpTestData(cls):
)
ProviderAccount.objects.bulk_create(provider_accounts)

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_name(self):
params = {'name': ['Provider Account 1', 'Provider Account 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down
2 changes: 1 addition & 1 deletion netbox/core/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DataSourceFilterSet(NetBoxModelFilterSet):

class Meta:
model = DataSource
fields = ('id', 'name', 'enabled')
fields = ('id', 'name', 'enabled', 'description')

def search(self, queryset, name, value):
if not value.strip():
Expand Down
18 changes: 16 additions & 2 deletions netbox/core/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ def setUpTestData(cls):
type=DataSourceTypeChoices.LOCAL,
source_url='file:///var/tmp/source1/',
status=DataSourceStatusChoices.NEW,
enabled=True
enabled=True,
description='foobar1'
),
DataSource(
name='Data Source 2',
type=DataSourceTypeChoices.LOCAL,
source_url='file:///var/tmp/source2/',
status=DataSourceStatusChoices.SYNCING,
enabled=True
enabled=True,
description='foobar2'
),
DataSource(
name='Data Source 3',
Expand All @@ -40,10 +42,18 @@ def setUpTestData(cls):
)
DataSource.objects.bulk_create(data_sources)

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_name(self):
params = {'name': ['Data Source 1', 'Data Source 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_description(self):
params = {'description': ['foobar1', 'foobar2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_type(self):
params = {'type': [DataSourceTypeChoices.LOCAL]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down Expand Up @@ -97,6 +107,10 @@ def setUpTestData(cls):
)
DataFile.objects.bulk_create(data_files)

def test_q(self):
params = {'q': 'file1.txt'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_source(self):
sources = DataSource.objects.all()
params = {'source_id': [sources[0].pk, sources[1].pk]}
Expand Down
Loading