[PATCH v2 1/3] REST: Integrate django-filter support

Stephen Finucane stephen at that.guru
Tue Jan 3 08:23:45 AEDT 2017


This mostly works out of the box, thanks to Django REST Framework.
Mostly unique fields, like name or email, are excluded as these will be
handled separately.

Signed-off-by: Stephen Finucane <stephen at that.guru>
Cc: Andrew Donnellan <andrew.donnellan at au1.ibm.com>
Cc: Russell Currey <ruscur at russell.cc>
---
v2:
- Move new filters to 'api/filters' instead of 'filters'
- Add 'project' filter to '/cover' and '/patch' endpoints
- Bump django-filter to 1.0
---
 patchwork/api/check.py     |  2 ++
 patchwork/api/cover.py     |  2 ++
 patchwork/api/filters.py   | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 patchwork/api/patch.py     |  2 ++
 patchwork/api/series.py    |  3 ++-
 patchwork/settings/base.py |  6 ++++-
 requirements-prod.txt      |  1 +
 requirements-test.txt      |  1 +
 8 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 patchwork/api/filters.py

diff --git a/patchwork/api/check.py b/patchwork/api/check.py
index a66106e..dcdc5c5 100644
--- a/patchwork/api/check.py
+++ b/patchwork/api/check.py
@@ -27,6 +27,7 @@ from rest_framework.serializers import HyperlinkedModelSerializer
 from rest_framework.serializers import HyperlinkedIdentityField
 
 from patchwork.api.base import MultipleFieldLookupMixin
+from patchwork.api.filters import CheckFilter
 from patchwork.models import Check
 from patchwork.models import Patch
 
@@ -89,6 +90,7 @@ class CheckMixin(object):
 
     queryset = Check.objects.prefetch_related('patch', 'user')
     serializer_class = CheckSerializer
+    filter_class = CheckFilter
 
 
 class CheckListCreate(CheckMixin, ListCreateAPIView):
diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py
index b440d51..6374ce9 100644
--- a/patchwork/api/cover.py
+++ b/patchwork/api/cover.py
@@ -26,6 +26,7 @@ from rest_framework.serializers import HyperlinkedModelSerializer
 from rest_framework.serializers import HyperlinkedRelatedField
 from rest_framework.serializers import SerializerMethodField
 
+from patchwork.api.filters import CoverLetterFilter
 from patchwork.models import CoverLetter
 
 
@@ -66,6 +67,7 @@ class CoverLetterList(ListAPIView):
     """List cover letters."""
 
     serializer_class = CoverLetterListSerializer
+    filter_class = CoverLetterFilter
 
     def get_queryset(self):
         qs = CoverLetter.objects.all().prefetch_related('series')\
diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
new file mode 100644
index 0000000..5f237b4
--- /dev/null
+++ b/patchwork/api/filters.py
@@ -0,0 +1,62 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2017 Stephen Finucane <stephen at that.guru>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from django_filters import FilterSet
+from django_filters import IsoDateTimeFilter
+
+from patchwork.models import Check
+from patchwork.models import CoverLetter
+from patchwork.models import Patch
+from patchwork.models import Series
+
+
+class TimestampMixin(object):
+
+    # 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')
+
+
+class SeriesFilter(TimestampMixin, FilterSet):
+
+    class Meta:
+        model = Series
+        fields = ('submitter',)
+
+
+class CoverLetterFilter(TimestampMixin, FilterSet):
+
+    class Meta:
+        model = CoverLetter
+        fields = ('project', 'series', 'submitter')
+
+
+class PatchFilter(FilterSet):
+
+    class Meta:
+        model = Patch
+        fields = ('project', 'series', 'submitter', 'delegate', 'state',
+                  'archived')
+
+
+class CheckFilter(TimestampMixin, FilterSet):
+
+    class Meta:
+        model = Check
+        fields = ('user', 'state', 'context')
diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
index e78f683..d12464f 100644
--- a/patchwork/api/patch.py
+++ b/patchwork/api/patch.py
@@ -29,6 +29,7 @@ from rest_framework.serializers import SerializerMethodField
 
 from patchwork.api.base import PatchworkPermission
 from patchwork.api.base import STATE_CHOICES
+from patchwork.api.filters import PatchFilter
 from patchwork.models import Patch
 from patchwork.models import State
 
@@ -117,6 +118,7 @@ class PatchList(ListAPIView):
 
     permission_classes = (PatchworkPermission,)
     serializer_class = PatchListSerializer
+    filter_class = PatchFilter
 
     def get_queryset(self):
         return Patch.objects.all().with_tag_counts()\
diff --git a/patchwork/api/series.py b/patchwork/api/series.py
index ade37fb..edde9a9 100644
--- a/patchwork/api/series.py
+++ b/patchwork/api/series.py
@@ -22,6 +22,7 @@ from rest_framework.generics import RetrieveAPIView
 from rest_framework.serializers import HyperlinkedModelSerializer
 
 from patchwork.api.base import PatchworkPermission
+from patchwork.api.filters import SeriesFilter
 from patchwork.models import Series
 
 
@@ -54,7 +55,7 @@ class SeriesMixin(object):
 class SeriesList(SeriesMixin, ListAPIView):
     """List series."""
 
-    pass
+    filter_class = SeriesFilter
 
 
 class SeriesDetail(SeriesMixin, RetrieveAPIView):
diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
index b7b10c3..35b85c7 100644
--- a/patchwork/settings/base.py
+++ b/patchwork/settings/base.py
@@ -133,7 +133,8 @@ try:
     import rest_framework  # NOQA
 
     INSTALLED_APPS += [
-        'rest_framework'
+        'rest_framework',
+        'django_filters',
     ]
 except ImportError:
     pass
@@ -143,6 +144,9 @@ REST_FRAMEWORK = {
     'DEFAULT_VERSIONING_CLASS':
         'rest_framework.versioning.NamespaceVersioning',
     'DEFAULT_PAGINATION_CLASS': 'patchwork.api.base.LinkHeaderPagination',
+    'DEFAULT_FILTER_BACKENDS': (
+        'django_filters.rest_framework.DjangoFilterBackend',
+    ),
 }
 
 #
diff --git a/requirements-prod.txt b/requirements-prod.txt
index 64d4339..a56a62f 100644
--- a/requirements-prod.txt
+++ b/requirements-prod.txt
@@ -1,4 +1,5 @@
 Django>=1.8,<1.11
 djangorestframework>=3.5,<3.6
+django-filter>=1.0,<1.1
 psycopg2>2.6,<2.7
 sqlparse
diff --git a/requirements-test.txt b/requirements-test.txt
index aa756f5..55713ef 100644
--- a/requirements-test.txt
+++ b/requirements-test.txt
@@ -2,3 +2,4 @@ mysqlclient>=1.3,<1.4  # replace this with psycopg2 for a PostgreSQL backend
 django-debug-toolbar==1.6
 python-dateutil>2.0,<3.0
 selenium>=3.0,<3.1
+django-filter>=1.0,<1.1
-- 
2.9.3



More information about the Patchwork mailing list