diff --git a/taiga/userstorage/api.py b/taiga/userstorage/api.py index dc994801..a7917f31 100644 --- a/taiga/userstorage/api.py +++ b/taiga/userstorage/api.py @@ -23,12 +23,14 @@ from taiga.base.api import ModelCrudViewSet from taiga.base import exceptions as exc from . import models +from . import filters from . import serializers from . import permissions class StorageEntriesViewSet(ModelCrudViewSet): model = models.StorageEntry + filter_backends = (filters.StorageEntriesFilterBackend,) serializer_class = serializers.StorageEntrySerializer permission_classes = (IsAuthenticated, permissions.StorageEntriesPermission) lookup_field = "key" diff --git a/taiga/userstorage/filters.py b/taiga/userstorage/filters.py new file mode 100644 index 00000000..265d7fb3 --- /dev/null +++ b/taiga/userstorage/filters.py @@ -0,0 +1,32 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from taiga.base import filters + +class StorageEntriesFilterBackend(filters.FilterBackend): + def filter_queryset(self, request, queryset, view): + queryset = super().filter_queryset(request, queryset, view) + + query_params = {} + + if "keys" in request.QUERY_PARAMS: + field_data = request.QUERY_PARAMS["keys"] + query_params["key__in"] = field_data.split(",") + + if query_params: + queryset = queryset.filter(**query_params) + + return queryset diff --git a/tests/integration/test_userstorage_api.py b/tests/integration/test_userstorage_api.py index 0c9a15be..f3ef0328 100644 --- a/tests/integration/test_userstorage_api.py +++ b/tests/integration/test_userstorage_api.py @@ -16,6 +16,7 @@ class TestListStorageEntries: self.user2 = factories.UserFactory() self.storage11 = factories.StorageEntryFactory(owner=self.user1) self.storage12 = factories.StorageEntryFactory(owner=self.user1) + self.storage13 = factories.StorageEntryFactory(owner=self.user1) self.storage21 = factories.StorageEntryFactory(owner=self.user2) def test_list_by_anonymous_user(self, client): @@ -29,7 +30,7 @@ class TestListStorageEntries: response = client.get(reverse("user-storage-list")) assert response.status_code == 200 entries = response.data - assert len(entries) == 2 + assert len(entries) == 3 response = client.logout() def test_list_only_user2_entriees(self, client): @@ -41,6 +42,16 @@ class TestListStorageEntries: assert len(entries) == 1 response = client.logout() + def test_list_only_user1_entriees_filter_by_keys(self, client): + self._load_initial_data() + response = client.login(username=self.user1.username, password=self.user1.username) + keys = "{},{}".format(self.storage11.key, self.storage13.key) + response = client.get("{}?keys={}".format(reverse("user-storage-list"), keys)) + assert response.status_code == 200 + entries = response.data + assert len(entries) == 2 + response = client.logout() + class TestViewStorageEntries: def _load_initial_data(self):