[PATCH] REST: filter patches by state name

Philippe Pepiot philippe.pepiot at logilab.fr
Wed May 3 21:43:04 AEST 2017


Since we display the name of the state in the serialized patch, also
filter by name instead of primary key.

Also this will be consistent with the current documentation examples
(curl 'https://patchwork.example.com/api/patches?state=under-review')

Signed-off-by: Philippe Pepiot <philippe.pepiot at logilab.fr>
---
 patchwork/api/filters.py         | 2 ++
 patchwork/tests/test_rest_api.py | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
index 1573416..784c58d 100644
--- a/patchwork/api/filters.py
+++ b/patchwork/api/filters.py
@@ -19,6 +19,7 @@
 
 from django_filters import FilterSet
 from django_filters import IsoDateTimeFilter
+from django_filters import CharFilter
 
 from patchwork.compat import LOOKUP_FIELD
 from patchwork.models import Bundle
@@ -51,6 +52,7 @@ class CoverLetterFilter(TimestampMixin, FilterSet):
 
 
 class PatchFilter(FilterSet):
+    state = CharFilter(name='state__name')
 
     class Meta:
         model = Patch
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 867602a..c63f672 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -330,6 +330,13 @@ class TestPatchAPI(APITestCase):
         self.assertNotIn('content', patch_rsp)
         self.assertNotIn('diff', patch_rsp)
 
+        # test filtering by state
+        other_state = create_state()
+        resp = self.client.get(self.api_url(), {'state': patch_obj.state.name})
+        self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
+        resp = self.client.get(self.api_url(), {'state': other_state.name})
+        self.assertEqual(0, len(resp.data))
+
         # authenticated user
         user = create_user()
         self.client.force_authenticate(user=user)
-- 
2.1.4


More information about the Patchwork mailing list