diff --git a/emeis/core/filters.py b/emeis/core/filters.py index 559fb17..105d0f7 100644 --- a/emeis/core/filters.py +++ b/emeis/core/filters.py @@ -4,7 +4,7 @@ from django_filters.filters import CharFilter from rest_framework import filters -from emeis.core.models import User +from emeis.core.models import Scope, User class MonolingualSearchFilter(filters.SearchFilter): @@ -67,3 +67,11 @@ def get_ordering(self, request, queryset, view): if not ordering: return ordering return [self._make_ordering_field(field, view) for field in ordering] + + +class ScopeFilterset(FilterSet): + class Meta: + model = Scope + fields = { + "id": ["exact", "in"], + } diff --git a/emeis/core/tests/test_filters.py b/emeis/core/tests/test_filters.py index c62cb60..13d40a0 100644 --- a/emeis/core/tests/test_filters.py +++ b/emeis/core/tests/test_filters.py @@ -84,6 +84,19 @@ def test_declared_filters( assert ret_users == [] +def test_scope_id_filter(admin_client, scope_factory): + scope1, _, scope3 = scope_factory.create_batch(3) + + resp = admin_client.get( + reverse("scope-list"), + {"filter[id__in]": ",".join([str(scope1.pk), str(scope3.pk)])}, + ) + + ret_scopes = [us["id"] for us in resp.json()["data"]] + + assert set(ret_scopes) == set([str(scope1.pk), str(scope3.pk)]) + + @pytest.mark.parametrize("sort", ["email", "-email"]) def test_user_ordering_case_insensitive(admin_client, admin_user, user_factory, sort): emails = [ diff --git a/emeis/core/views.py b/emeis/core/views.py index 3d5abf5..f8a8e57 100644 --- a/emeis/core/views.py +++ b/emeis/core/views.py @@ -142,6 +142,7 @@ class ScopeViewSet(BaseViewset): "name", "description", ] + filterset_class = filters.ScopeFilterset class RoleViewSet(BaseViewset):