diff mbox

REST: filter patches by state name

Message ID 1493811784-4360-1-git-send-email-philippe.pepiot@logilab.fr
State Accepted
Headers show

Commit Message

Philippe Pepiot May 3, 2017, 11:43 a.m. UTC
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>
---
 patchwork/api/filters.py         | 2 ++
 patchwork/tests/test_rest_api.py | 7 +++++++
 2 files changed, 9 insertions(+)

Comments

Stephen Finucane May 3, 2017, 4:25 p.m. UTC | #1
On Wed, 2017-05-03 at 13:43 +0200, Philippe Pepiot wrote:
> 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>

*Fantastic* :) This has been annoying me to no end.

Reviewed-by: Stephen Finucane <stephen@that.guru>

...and applied post-haste.

Stephen

PS: That's two of three "nice to have for 2.0" items resolved [1], the
first being the updated Ubuntu 16.04 guide [2]. Given that the
remaining one is looking less likely [3], I think tagging an RC
tomorrow should be doable.

[1] https://lists.ozlabs.org/pipermail/patchwork/2017-April/004217.html
[2] https://patchwork.ozlabs.org/patch/757353/
[3] https://lists.ozlabs.org/pipermail/patchwork/2017-May/004234.html
diff mbox

Patch

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)