[PATCH 2/4] Add support for django-filter 2.0

Stephen Finucane stephen at that.guru
Sun Oct 7 06:01:36 AEDT 2018


This is necessary for Django 2.1 support. We retain support for
django-filter 1.0 and 1.1 as 2.0 is Python 3-only. Thankfully there is
essentially zero cost in doing so for now.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/api/bundle.py                       |  2 +-
 patchwork/api/check.py                        |  2 +-
 patchwork/api/cover.py                        |  2 +-
 patchwork/api/event.py                        |  2 +-
 patchwork/api/patch.py                        |  2 +-
 patchwork/api/series.py                       |  2 +-
 patchwork/compat.py                           | 20 +++++++++++++++++++
 patchwork/settings/base.py                    |  2 +-
 .../django-filter-2-0-3240949516a873e3.yaml   |  6 ++++++
 requirements-dev.txt                          |  3 ++-
 requirements-prod.txt                         |  3 ++-
 tox.ini                                       |  5 +++--
 12 files changed, 40 insertions(+), 11 deletions(-)
 create mode 100644 releasenotes/notes/django-filter-2-0-3240949516a873e3.yaml

diff --git a/patchwork/api/bundle.py b/patchwork/api/bundle.py
index 04045888..2dec70d1 100644
--- a/patchwork/api/bundle.py
+++ b/patchwork/api/bundle.py
@@ -66,7 +66,7 @@ class BundleMixin(object):
 class BundleList(BundleMixin, ListAPIView):
     """List bundles."""
 
-    filter_class = BundleFilterSet
+    filter_class = filterset_class = BundleFilterSet
     search_fields = ('name',)
     ordering_fields = ('id', 'name', 'owner')
     ordering = 'id'
diff --git a/patchwork/api/check.py b/patchwork/api/check.py
index fbf05adb..48019e72 100644
--- a/patchwork/api/check.py
+++ b/patchwork/api/check.py
@@ -66,7 +66,7 @@ class CheckSerializer(HyperlinkedModelSerializer):
 class CheckMixin(object):
 
     serializer_class = CheckSerializer
-    filter_class = CheckFilterSet
+    filter_class = filterset_class = CheckFilterSet
 
     def get_queryset(self):
         patch_id = self.kwargs['patch_id']
diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py
index d3d26bdf..40f8c351 100644
--- a/patchwork/api/cover.py
+++ b/patchwork/api/cover.py
@@ -83,7 +83,7 @@ class CoverLetterList(ListAPIView):
     """List cover letters."""
 
     serializer_class = CoverLetterListSerializer
-    filter_class = CoverLetterFilterSet
+    filter_class = filterset_class = CoverLetterFilterSet
     search_fields = ('name',)
     ordering_fields = ('id', 'name', 'date', 'submitter')
     ordering = 'id'
diff --git a/patchwork/api/event.py b/patchwork/api/event.py
index 7fec6740..c0d973d8 100644
--- a/patchwork/api/event.py
+++ b/patchwork/api/event.py
@@ -75,7 +75,7 @@ class EventList(ListAPIView):
     """List events."""
 
     serializer_class = EventSerializer
-    filter_class = EventFilterSet
+    filter_class = filterset_class = EventFilterSet
     page_size_query_param = None  # fixed page size
     ordering_fields = ()
     ordering = '-date'
diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
index f400c138..1e647283 100644
--- a/patchwork/api/patch.py
+++ b/patchwork/api/patch.py
@@ -153,7 +153,7 @@ class PatchList(ListAPIView):
 
     permission_classes = (PatchworkPermission,)
     serializer_class = PatchListSerializer
-    filter_class = PatchFilterSet
+    filter_class = filterset_class = PatchFilterSet
     search_fields = ('name',)
     ordering_fields = ('id', 'name', 'project', 'date', 'state', 'archived',
                        'submitter', 'check')
diff --git a/patchwork/api/series.py b/patchwork/api/series.py
index 5d4183b6..f7bb8c06 100644
--- a/patchwork/api/series.py
+++ b/patchwork/api/series.py
@@ -62,7 +62,7 @@ class SeriesMixin(object):
 class SeriesList(SeriesMixin, ListAPIView):
     """List series."""
 
-    filter_class = SeriesFilterSet
+    filter_class = filterset_class = SeriesFilterSet
     search_fields = ('name',)
     ordering_fields = ('id', 'name', 'date', 'submitter', 'received_all')
     ordering = 'id'
diff --git a/patchwork/compat.py b/patchwork/compat.py
index 8fcb04cf..8d949607 100644
--- a/patchwork/compat.py
+++ b/patchwork/compat.py
@@ -13,12 +13,32 @@ from django.conf import settings
 # The django-filter library renamed 'Filter.name' to 'Filter.field_name' in
 # 1.1.
 #
+# DjangoFilterBackend
+
+# The django-filter library changed the default strictness level in 2.0
+#
 # https://django-filter.readthedocs.io/en/master/guide/migration.html#migrating-to-2-0
 
 if settings.ENABLE_REST_API:
     import django_filters  # noqa
+    from django_filters import rest_framework  # noqa
+    from rest_framework import exceptions  # noqa
 
     if django_filters.VERSION >= (1, 1):
         NAME_FIELD = 'field_name'
     else:
         NAME_FIELD = 'name'
+
+    if django_filters.VERSION >= (2, 0):
+        # TODO(stephenfin): Enable strict mode in API v2.0, possibly with a
+        # bump in the minimum version of django-filter [1]
+        #
+        # [1] https://github.com/carltongibson/django-filter/pull/983
+        class DjangoFilterBackend(rest_framework.DjangoFilterBackend):
+            def filter_queryset(self, request, queryset, view):
+                try:
+                    return super().filter_queryset(request, queryset, view)
+                except exceptions.ValidationError:
+                    return queryset.none()
+    else:
+        DjangoFilterBackend = rest_framework.DjangoFilterBackend
diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
index 1a62404f..c69947be 100644
--- a/patchwork/settings/base.py
+++ b/patchwork/settings/base.py
@@ -127,7 +127,7 @@ REST_FRAMEWORK = {
         'rest_framework.versioning.URLPathVersioning',
     'DEFAULT_PAGINATION_CLASS': 'patchwork.api.base.LinkHeaderPagination',
     'DEFAULT_FILTER_BACKENDS': (
-        'django_filters.rest_framework.DjangoFilterBackend',
+        'patchwork.compat.DjangoFilterBackend',
         'rest_framework.filters.SearchFilter',
         'rest_framework.filters.OrderingFilter',
     ),
diff --git a/releasenotes/notes/django-filter-2-0-3240949516a873e3.yaml b/releasenotes/notes/django-filter-2-0-3240949516a873e3.yaml
new file mode 100644
index 00000000..ee5e496c
--- /dev/null
+++ b/releasenotes/notes/django-filter-2-0-3240949516a873e3.yaml
@@ -0,0 +1,6 @@
+---
+upgrade:
+  - |
+    `django-filter 2.0
+    <https://github.com/carltongibson/django-filter/releases/tag/2.0.0>`_ is
+    now supported. This requires Python 3.
diff --git a/requirements-dev.txt b/requirements-dev.txt
index 70b747f5..2574fd37 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,5 +1,6 @@
 Django==2.0.8; python_version >= '3.4'
 Django==1.11.15; python_version < '3.0'
 djangorestframework==3.8.2
-django-filter==1.1.0
+django-filter==2.0.0; python_version >= '3.4'
+django-filter==1.1.0; python_version < '3.0'  # pyup: ignore
 -r requirements-test.txt
diff --git a/requirements-prod.txt b/requirements-prod.txt
index efe6743c..9b7cc6a2 100644
--- a/requirements-prod.txt
+++ b/requirements-prod.txt
@@ -1,6 +1,7 @@
 Django==2.0.8; python_version >= '3.4'
 Django==1.11.15; python_version < '3.0'
 djangorestframework==3.8.2
-django-filter==1.1.0
+django-filter==2.0.0; python_version >= '3.4'
+django-filter==1.1.0; python_version < '3.0'  # pyup: ignore
 psycopg2-binary==2.7.5
 sqlparse==0.2.4
diff --git a/tox.ini b/tox.ini
index ab35be82..da2a5060 100644
--- a/tox.ini
+++ b/tox.ini
@@ -8,10 +8,11 @@ deps =
     -r{toxinidir}/requirements-test.txt
     django111: django>=1.11,<2.0
     django111: djangorestframework>=3.6,<3.9
-    django111: django-filter>=1.0,<1.2
+    django111: django-filter>=1.0,<3.0; python_version >= '3.4'
+    django111: django-filter>=1.0,<2.0; python_version < '3.0'
     django20: django>=2.0,<2.1
     django20: djangorestframework>=3.7,<3.9
-    django20: django-filter>=1.1,<1.2
+    django20: django-filter>=2.0,<3.0
 setenv =
     DJANGO_SETTINGS_MODULE = patchwork.settings.dev
     PYTHONDONTWRITEBYTECODE = 1
-- 
2.17.1



More information about the Patchwork mailing list