Skip to content

Commit

Permalink
Clean up filtering of usable SavedFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Nov 2, 2022
1 parent ac58743 commit 39bb6d3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
15 changes: 0 additions & 15 deletions netbox/extras/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,6 @@ class SavedFilterViewSet(NetBoxModelViewSet):
serializer_class = serializers.SavedFilterSerializer
filterset_class = filtersets.SavedFilterFilterSet

def get_queryset(self):
"""
Return only shared SavedFilters, or those owned by the current user, unless
this is a superuser.
"""
queryset = super().get_queryset()
user = self.request.user
if user.is_superuser:
return queryset
if user.is_anonymous:
return queryset.filter(shared=True)
return queryset.filter(
Q(shared=True) | Q(user=user)
)


#
# Tags
Expand Down
16 changes: 16 additions & 0 deletions netbox/extras/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class SavedFilterFilterSet(BaseFilterSet):
to_field_name='username',
label='User (name)',
)
usable = django_filters.BooleanFilter(
method='_usable'
)

class Meta:
model = SavedFilter
Expand All @@ -171,6 +174,19 @@ def search(self, queryset, name, value):
Q(description__icontains=value)
)

def _usable(self, queryset, name, value):
"""
Return only SavedFilters that are both enabled and are shared (or belong to the current user).
"""
user = self.request.user if self.request else None
if not user or user.is_anonymous:
if value:
return queryset.filter(enabled=True, shared=True)
return queryset.filter(Q(enabled=False) | Q(shared=False))
if value:
return queryset.filter(enabled=True).filter(Q(shared=True) | Q(user=user))
return queryset.filter(Q(enabled=False) | Q(Q(shared=False) & ~Q(user=user)))


class ImageAttachmentFilterSet(BaseFilterSet):
q = django_filters.CharFilter(
Expand Down
3 changes: 2 additions & 1 deletion netbox/extras/forms/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class SavedFiltersMixin(forms.Form):
filter = DynamicModelMultipleChoiceField(
queryset=SavedFilter.objects.all(),
required=False,
label='Saved Filter',
query_params={
'enabled': True,
'usable': True,
}
)
7 changes: 7 additions & 0 deletions netbox/extras/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ def test_shared(self):
params = {'enabled': False}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_usable(self):
# Filtering for an anonymous user
params = {'usable': True}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
params = {'usable': False}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)


class ExportTemplateTestCase(TestCase, BaseFilterSetTests):
queryset = ExportTemplate.objects.all()
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/extras/savedfilter.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h5 class="card-header">Saved Filter</h5>
</tr>
<tr>
<th scope="row">Shared</th>
<td>{% checkmark object.enabled %}</td>
<td>{% checkmark object.shared %}</td>
</tr>
<tr>
<th scope="row">Weight</th>
Expand Down

0 comments on commit 39bb6d3

Please sign in to comment.