aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Finucane <stephen@that.guru>2019-11-30 16:24:36 +0000
committerStephen Finucane <stephen@that.guru>2019-11-30 16:40:08 +0000
commite53d7985a15a90617d653645d1bb0c2693b73ff6 (patch)
tree3ca836fa4f3a643852926297e5878ce36c9f06bd
parentd380219e4dd5e963f7b2b3fb421cd033e70ac5a8 (diff)
downloadpatchwork-e53d7985a15a90617d653645d1bb0c2693b73ff6.tar
patchwork-e53d7985a15a90617d653645d1bb0c2693b73ff6.tar.gz
REST: Exclude filters added in later version
If a person requests API version 1.1, they should get the exact same behavior regardless of the base Patchwork version. We already do this for fields in the output, so now extend this to filters in the querystring. Signed-off-by: Stephen Finucane <stephen@that.guru> Cc: Daniel Axtens <dja@axtens.net>
-rw-r--r--patchwork/api/filters.py36
-rw-r--r--patchwork/tests/api/test_patch.py9
2 files changed, 38 insertions, 7 deletions
diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
index 4184ee8..6b4d84c 100644
--- a/patchwork/api/filters.py
+++ b/patchwork/api/filters.py
@@ -13,6 +13,7 @@ from django_filters import ModelMultipleChoiceFilter
from django.forms import ModelMultipleChoiceField as BaseMultipleChoiceField
from django.forms.widgets import MultipleHiddenInput
+from patchwork.api import utils
from patchwork.compat import NAME_FIELD
from patchwork.models import Bundle
from patchwork.models import Check
@@ -136,14 +137,32 @@ class UserFilter(ModelMultipleChoiceFilter):
# filter sets
-class TimestampMixin(FilterSet):
+
+class BaseFilterSet(FilterSet):
+
+ @property
+ def form(self):
+ form = super(BaseFilterSet, self).form
+
+ for version in getattr(self.Meta, 'versioned_fields', {}):
+ if utils.has_version(self.request, version):
+ continue
+
+ for field in self.Meta.versioned_fields[version]:
+ if field in form.fields:
+ del form.fields[field]
+
+ return form
+
+
+class TimestampMixin(BaseFilterSet):
# TODO(stephenfin): These should filter on a 'updated_at' field instead
before = IsoDateTimeFilter(lookup_expr='lt', **{NAME_FIELD: 'date'})
since = IsoDateTimeFilter(lookup_expr='gte', **{NAME_FIELD: 'date'})
-class SeriesFilterSet(TimestampMixin, FilterSet):
+class SeriesFilterSet(TimestampMixin, BaseFilterSet):
submitter = PersonFilter(queryset=Person.objects.all())
project = ProjectFilter(queryset=Project.objects.all())
@@ -153,7 +172,7 @@ class SeriesFilterSet(TimestampMixin, FilterSet):
fields = ('submitter', 'project')
-class CoverLetterFilterSet(TimestampMixin, FilterSet):
+class CoverLetterFilterSet(TimestampMixin, BaseFilterSet):
project = ProjectFilter(queryset=Project.objects.all())
# NOTE(stephenfin): We disable the select-based HTML widgets for these
@@ -167,7 +186,7 @@ class CoverLetterFilterSet(TimestampMixin, FilterSet):
fields = ('project', 'series', 'submitter')
-class PatchFilterSet(TimestampMixin, FilterSet):
+class PatchFilterSet(TimestampMixin, BaseFilterSet):
project = ProjectFilter(queryset=Project.objects.all())
# NOTE(stephenfin): We disable the select-based HTML widgets for these
@@ -187,9 +206,12 @@ class PatchFilterSet(TimestampMixin, FilterSet):
# which seems to rather defeat the point of using django-filters.
fields = ('project', 'series', 'submitter', 'delegate',
'state', 'archived', 'hash')
+ versioned_fields = {
+ '1.2': ('hash', ),
+ }
-class CheckFilterSet(TimestampMixin, FilterSet):
+class CheckFilterSet(TimestampMixin, BaseFilterSet):
user = UserFilter(queryset=User.objects.all())
@@ -198,7 +220,7 @@ class CheckFilterSet(TimestampMixin, FilterSet):
fields = ('user', 'state', 'context')
-class EventFilterSet(TimestampMixin, FilterSet):
+class EventFilterSet(TimestampMixin, BaseFilterSet):
# NOTE(stephenfin): We disable the select-based HTML widgets for these
# filters as the resulting query is _huge_
@@ -217,7 +239,7 @@ class EventFilterSet(TimestampMixin, FilterSet):
fields = ('project', 'category', 'series', 'patch', 'cover')
-class BundleFilterSet(FilterSet):
+class BundleFilterSet(BaseFilterSet):
project = ProjectFilter(queryset=Project.objects.all())
owner = UserFilter(queryset=User.objects.all())
diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py
index 4afc15a..bf3ef9f 100644
--- a/patchwork/tests/api/test_patch.py
+++ b/patchwork/tests/api/test_patch.py
@@ -188,6 +188,15 @@ class TestPatchAPI(utils.APITestCase):
'hash': 'da638d0746a115000bf890fada1f02679aa282e8'})
self.assertEqual(0, len(resp.data))
+ def test_list_filter_hash_version_1_1(self):
+ """Filter patches by hash using API v1.1."""
+ self._create_patch()
+
+ # we still see the patch since the hash field is ignored
+ resp = self.client.get(self.api_url(version='1.1'),
+ {'hash': 'garbagevalue'})
+ self.assertEqual(1, len(resp.data))
+
@utils.store_samples('patch-list-1-0')
def test_list_version_1_0(self):
"""List patches using API v1.0."""