Commit fc3c66a1 authored by Damien Lespiau's avatar Damien Lespiau
Browse files

api: Add filtering to /patches/

Just like in the series case, we'll use the filtering from the web page
and git-pw.
Signed-off-by: default avatarDamien Lespiau <>
parent 4ec7ce74
......@@ -626,6 +626,29 @@ Patches
:query project: Filter patches by project ``id``.
:query name: Filter patches by name.
:query submitter: Filter patches by submitter ``id``. ``self`` can be used
as a special value meaning the current logged in user.
:query submitted_since: Retrieve only submitted patches newer than a
specified time. Format is the same as ``date``
in response, an ISO 8601 date.
:query updated_since: Retrieve only updated patches newer than a
specified time. Format is the same as
``last_updated`` in response, an ISO 8601 date.
:query submitted_before: Retrieve only submitted patches older than the
specified time. Format is the same as
``date`` in response, an ISO 8601 date.
:query updated_before: Retrieve only updated patches older than a
specified time. Format is the same as
``last_updated`` in response, an ISO 8601 date.
.. http:get:: /api/1.0/patches/
List of all patches.
......@@ -213,6 +213,10 @@ class State(models.Model):
ordering = models.IntegerField(unique=True)
action_required = models.BooleanField(default=True)
def from_string(cls, name):
return State.objects.get(name__iexact=name)
def __str__(self):
......@@ -31,7 +31,7 @@ from django.db.models import Q
from django.http import HttpResponse
from patchwork.tasks import send_reviewer_notification
from patchwork.models import (Project, Series, SeriesRevision, Patch, EventLog,
Test, TestResult, TestState, Person,
State, Test, TestResult, TestState, Person,
RevisionState, Event)
from rest_framework import (views, viewsets, mixins, filters, permissions,
......@@ -489,11 +489,51 @@ class RevisionResultViewSet(viewsets.ViewSet, ResultMixin):
return response
class PatchFilter(django_filters.FilterSet):
def filter_submitter(self, queryset, submitter):
submitter = int(submitter)
queryset = queryset.filter(submitter=submitter)
except ValueError:
if submitter == 'self' and self.request.user.is_authenticated():
people = Person.objects.filter(user=self.request.user)
queryset = queryset.filter(submitter__in=people)
return queryset
def filter_state(self, queryset, state_names):
if not state_names:
return queryset
states = map(State.from_string, state_names.split(','))
return queryset.filter(state__in=states)
except State.DoesNotExist:
return queryset
submitted_since = django_filters.CharFilter(name='date',
updated_since = django_filters.CharFilter(name='last_updated',
submitted_before = django_filters.CharFilter(name='date',
updated_before = django_filters.CharFilter(name='last_updated',
submitter = django_filters.MethodFilter()
name = django_filters.CharFilter(lookup_type='icontains')
state = django_filters.MethodFilter()
class Meta:
model = Patch
fields = ['project']
class PatchListMixin(ListMixin):
queryset = Patch.objects.all()
serializer_class = PatchSerializer
select_fields__expand = ('project', 'submitter', 'state')
filter_backends = (RequestDjangoFilterBackend, RelatedOrderingFilter)
filter_class = PatchFilter
permission_classes = (MaintainerPermission, )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment