]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
compat: Handle older versions of django-filter
authorStephen Finucane <stephen@that.guru>
Tue, 4 Apr 2017 21:38:17 +0000 (22:38 +0100)
committerStephen Finucane <stephen@that.guru>
Fri, 28 Apr 2017 21:19:57 +0000 (22:19 +0100)
Recent versions of Django REST Framework (DRF) have deprecated the
'DjangoFilterBackend' filter found in-tree, in favour of an equivalent
implementation found in django-filter. However, we need to support older
versions of DRF for users who want to use system packages.

Seeing as the two implementations are, for all intents and purposes,
essentially the same thing, provide a shim that will allow us to use
both together.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Partial-bug: #94

patchwork/api/filters.py
patchwork/compat.py
patchwork/settings/base.py

index eff7ceb9fe548e5caacd457552017a2ae38f9f57..15734166b625c9c4a7fd3f741bc3594a7cb11b24 100644 (file)
@@ -20,6 +20,7 @@
 from django_filters import FilterSet
 from django_filters import IsoDateTimeFilter
 
+from patchwork.compat import LOOKUP_FIELD
 from patchwork.models import Bundle
 from patchwork.models import Check
 from patchwork.models import CoverLetter
@@ -31,8 +32,8 @@ from patchwork.models import Series
 class TimestampMixin(FilterSet):
 
     # TODO(stephenfin): These should filter on a 'updated_at' field instead
-    before = IsoDateTimeFilter(name='date', lookup_expr='lt')
-    since = IsoDateTimeFilter(name='date', lookup_expr='gte')
+    before = IsoDateTimeFilter(name='date', **{LOOKUP_FIELD: 'lt'})
+    since = IsoDateTimeFilter(name='date', **{LOOKUP_FIELD: 'gte'})
 
 
 class SeriesFilter(TimestampMixin, FilterSet):
index 5c8ca234ade8e55be9eaa959a5e3d0c3d743993c..af7d684a9670ad28bf87c1f46e35211c28690c55 100644 (file)
@@ -20,6 +20,7 @@
 """Compatibility wrappers for various Django versions."""
 
 import django
+from django.conf import settings
 
 
 # render_to_string
@@ -39,3 +40,39 @@ else:
         context_instance = RequestContext(request) if request else None
         return loader.render_to_string(template_name, context,
                                        context_instance)
+
+
+# DjangoFilterBackend
+#
+# The DjangoFilterBackend was provided in Django REST Framework from 3.0 to
+# 3.4, was marked as pending deprecation in 3.5, was deprecated in 3.6 and will
+# be removed in 3.7. However, the equivalent DjangoFilterBackend found in
+# django-filter is only available since 1.0 of that package.
+#
+# http://www.django-rest-framework.org/topics/3.6-announcement/
+
+if settings.ENABLE_REST_API:
+    import rest_framework  # noqa
+
+    if rest_framework.VERSION >= '3.5':
+        from django_filters.rest_framework import DjangoFilterBackend  # noqa
+    else:
+        from rest_framework.filters import DjangoFilterBackend  # noqa
+
+
+# LOOKUP_FIELD
+#
+# The django-filter library uses the 'lookup_expr' attribute to determine which
+# lookup type to use, e.g. exact, gt, lt etc. However, until 0.13 this was
+# called 'lookup_type', and 0.13 supported both but gave a deprecation warning.
+# We need to support these versions for use with older versions of DRF.
+#
+# https://github.com/carltongibson/django-filter/blob/v0.13/django_filters\
+#   /filters.py#L35-L36
+if settings.ENABLE_REST_API:
+    import django_filters  # noqa
+
+    if django_filters.VERSION >= (1, 0):
+        LOOKUP_FIELD = 'lookup_expr'
+    else:
+        LOOKUP_FIELD = 'lookup_type'
index 180a4691a248829c1465729adb4497813df01d0d..5d5caf1b2cafa236bc7903eefeca6ea47b6ac660 100644 (file)
@@ -147,7 +147,7 @@ REST_FRAMEWORK = {
         'rest_framework.versioning.NamespaceVersioning',
     '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',
     ),