diff mbox

[20/51] api: Expose Series

Message ID 1441986924-26689-21-git-send-email-damien.lespiau@intel.com
State Superseded
Headers show

Commit Message

Damien Lespiau Sept. 11, 2015, 3:54 p.m. UTC
The main difficult is that we need to select the Series valid for the
project we're looking at ie:

  http://127.0.0.1:8000/api/1.0/projects/i915/series/

will only return the i915 series. To manage that we need to override the
get_queryset() method of the GenericModelViewSet class and properly
filter with the project name we've got from the URL.

We're also using the handy mixin classes defined by DRF to only support
a few operations on series. In particular, we don't want to be able to
create or destroy a series with the API.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 patchwork/serializers.py | 16 +++++++++++++++-
 patchwork/urls.py        |  5 +++++
 patchwork/views/api.py   | 22 +++++++++++++++++++---
 3 files changed, 39 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/patchwork/serializers.py b/patchwork/serializers.py
index f210af3..468daa7 100644
--- a/patchwork/serializers.py
+++ b/patchwork/serializers.py
@@ -17,7 +17,7 @@ 
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from patchwork.models import Project
+from patchwork.models import Project, Series
 from rest_framework import serializers
 
 class ProjectSerializer(serializers.HyperlinkedModelSerializer):
@@ -25,3 +25,17 @@  class ProjectSerializer(serializers.HyperlinkedModelSerializer):
         model = Project
         fields = ('name', 'linkname', 'listemail', 'web_url', 'scm_url',
                   'webscm_url')
+
+class SeriesSerializer(serializers.ModelSerializer):
+    submitter__name = serializers.CharField(source='submitter.name',
+                                            read_only=True)
+    reviewer__name = serializers.CharField(source='reviewer.name',
+                                           read_only=True)
+
+    class Meta:
+        model = Series
+        fields = ('id', 'name', 'n_patches', 'submitter', 'submitter__name',
+                  'submitted', 'last_updated', 'version', 'reviewer',
+                  'reviewer__name')
+        read_only_fields = ('n_patches', 'submitter', 'submitted',
+                            'last_updated', 'version')
diff --git a/patchwork/urls.py b/patchwork/urls.py
index e7a0c85..041ba1c 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -29,6 +29,10 @@  import patchwork.views.api as api
 # /projects/$project/
 project_router = routers.SimpleRouter()
 project_router.register('projects', api.ProjectViewSet)
+# /projects/$project/series
+series_list_router = routers.NestedSimpleRouter(project_router, 'projects',
+                                                lookup='project')
+series_list_router.register(r'series', api.SeriesListViewSet)
 
 admin.autodiscover()
 
@@ -37,6 +41,7 @@  urlpatterns = patterns('',
 
     # API
     (r'^api/1.0/', include(project_router.urls)),
+    (r'^api/1.0/', include(series_list_router.urls)),
 
     # Example:
     (r'^$', 'patchwork.views.projects'),
diff --git a/patchwork/views/api.py b/patchwork/views/api.py
index 30b42c2..fa8ebc1 100644
--- a/patchwork/views/api.py
+++ b/patchwork/views/api.py
@@ -17,10 +17,11 @@ 
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from patchwork.models import Project
-from rest_framework import viewsets
+from patchwork.models import Project, Series
+from rest_framework import viewsets, mixins, generics
 from rest_framework.response import Response
-from patchwork.serializers import ProjectSerializer
+from rest_framework.generics import get_object_or_404
+from patchwork.serializers import ProjectSerializer, SeriesSerializer
 
 class ProjectViewSet(viewsets.ViewSet):
     model = Project
@@ -34,3 +35,18 @@  class ProjectViewSet(viewsets.ViewSet):
         queryset = Project.objects.get(name=pk)
         serializer = ProjectSerializer(queryset)
         return Response(serializer.data)
+
+class SeriesListViewSet(mixins.ListModelMixin,
+                        viewsets.GenericViewSet):
+    queryset = Series.objects.all()
+    serializer_class = SeriesSerializer
+    paginate_by = 20
+    paginate_by_param = 'perpage'
+    max_paginate_by = 100
+
+    def get_queryset(self):
+        filter_kwargs = { 'project__linkname': self.kwargs['project_pk'] }
+
+        # Ensure queryset is re-evaluated on each request.
+        queryset = self.queryset.filter(**filter_kwargs)
+        return queryset