[6/7] REST: Use ModelMultipleChoiceField for other fields

Message ID 20180411161338.420-7-stephen@that.guru
State New
Headers show
Series
  • Add support for multiple filters
Related show

Commit Message

Stephen Finucane April 11, 2018, 4:13 p.m.
There's benefit to being able to do stuff like select multiple patches.
Let's do that.

Signed-off-by: Stephen Finucane <stephen@that.guru>
---
 patchwork/api/filters.py | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

Patch

diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
index afef10f4..4d8d504d 100644
--- a/patchwork/api/filters.py
+++ b/patchwork/api/filters.py
@@ -41,6 +41,9 @@  from patchwork.models import State
 class ModelMultipleChoiceField(BaseMultipleChoiceField):
 
     def _get_filter(self, value):
+        if not self.alternate_lookup:
+            return 'pk', value
+
         try:
             return 'pk', int(value)
         except ValueError:
@@ -88,14 +91,14 @@  class ModelMultipleChoiceField(BaseMultipleChoiceField):
         return qs
 
 
-class ProjectChoiceField(ModelMultipleChoiceField):
+class BaseField(ModelMultipleChoiceField):
 
-    alternate_lookup = 'linkname__iexact'
+    alternate_lookup = None
 
 
-class ProjectFilter(ModelMultipleChoiceFilter):
+class BaseFilter(ModelMultipleChoiceFilter):
 
-    field_class = ProjectChoiceField
+    field_class = BaseField
 
 
 class PersonChoiceField(ModelMultipleChoiceField):
@@ -108,6 +111,16 @@  class PersonFilter(ModelMultipleChoiceFilter):
     field_class = PersonChoiceField
 
 
+class ProjectChoiceField(ModelMultipleChoiceField):
+
+    alternate_lookup = 'linkname__iexact'
+
+
+class ProjectFilter(ModelMultipleChoiceFilter):
+
+    field_class = ProjectChoiceField
+
+
 class StateChoiceField(ModelMultipleChoiceField):
 
     def _get_filter(self, value):
@@ -154,6 +167,7 @@  class SeriesFilterSet(TimestampMixin, FilterSet):
 class CoverLetterFilterSet(TimestampMixin, FilterSet):
 
     project = ProjectFilter(queryset=Project.objects.all())
+    series = BaseFilter(queryset=Project.objects.all())
     submitter = PersonFilter(queryset=Person.objects.all())
 
     class Meta:
@@ -164,6 +178,7 @@  class CoverLetterFilterSet(TimestampMixin, FilterSet):
 class PatchFilterSet(TimestampMixin, FilterSet):
 
     project = ProjectFilter(queryset=Project.objects.all())
+    series = BaseFilter(queryset=Series.objects.all())
     submitter = PersonFilter(queryset=Person.objects.all())
     delegate = UserFilter(queryset=User.objects.all())
     state = StateFilter(queryset=State.objects.all())
@@ -186,6 +201,9 @@  class CheckFilterSet(TimestampMixin, FilterSet):
 class EventFilterSet(TimestampMixin, FilterSet):
 
     project = ProjectFilter(queryset=Project.objects.all())
+    series = BaseFilter(queryset=Series.objects.all())
+    patch = BaseFilter(queryset=Patch.objects.all())
+    cover = BaseFilter(queryset=CoverLetter.objects.all())
 
     class Meta:
         model = Event