]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: filter patches by state name
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>
Wed, 3 May 2017 11:43:04 +0000 (13:43 +0200)
committerStephen Finucane <stephen@that.guru>
Wed, 3 May 2017 14:23:10 +0000 (15:23 +0100)
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@logilab.fr>
Reviewed-by: Stephen Finucane <stephen@that.guru>
patchwork/api/filters.py
patchwork/tests/test_rest_api.py

index 15734166b625c9c4a7fd3f741bc3594a7cb11b24..41adcbdd9e84b12ce60babce07bf7284e8ed6ba3 100644 (file)
@@ -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
@@ -52,6 +53,9 @@ class CoverLetterFilter(TimestampMixin, FilterSet):
 
 class PatchFilter(FilterSet):
 
+    # TODO(stephenfin): We should probably be using a ChoiceFilter here?
+    state = CharFilter(name='state__name')
+
     class Meta:
         model = Patch
         fields = ('project', 'series', 'submitter', 'delegate', 'state',
index 867602a878615c39afd6117773dddb2c99e9b4bf..c63f6728ee653f9ff8ec9da877e1e10ffbaacd7c 100644 (file)
@@ -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)