From: Philippe Pepiot Date: Wed, 3 May 2017 11:43:04 +0000 (+0200) Subject: REST: filter patches by state name X-Git-Tag: v2.0.0-rc1~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6222574be4e75cdb78992239019c0ff681004085;p=thirdparty%2Fpatchwork.git REST: filter patches by state name 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 Reviewed-by: Stephen Finucane --- diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py index 15734166..41adcbdd 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 @@ -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', diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py index 867602a8..c63f6728 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)