[PATCH 21/51] api: Expose Revisions

Damien Lespiau damien.lespiau at intel.com
Sat Sep 12 01:54:54 AEST 2015


Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
---
 patchwork/serializers.py | 22 +++++++++++++++++++++-
 patchwork/urls.py        |  9 +++++++++
 patchwork/views/api.py   | 19 +++++++++++++++++--
 3 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/patchwork/serializers.py b/patchwork/serializers.py
index 468daa7..05c9f15 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, Series
+from patchwork.models import Project, Series, SeriesRevision, Patch
 from rest_framework import serializers
 
 class ProjectSerializer(serializers.HyperlinkedModelSerializer):
@@ -39,3 +39,23 @@ class SeriesSerializer(serializers.ModelSerializer):
                   'reviewer__name')
         read_only_fields = ('n_patches', 'submitter', 'submitted',
                             'last_updated', 'version')
+
+class PatchSerializer(serializers.ModelSerializer):
+    submitter__name = serializers.CharField(source='submitter.name',
+                                            read_only=True)
+    state__name = serializers.CharField(source='state.name', read_only=True)
+    class Meta:
+        model = Patch
+        fields = ('name', 'msgid', 'date', 'submitter', 'submitter__name',
+                  'state', 'state__name', 'archived', 'content')
+        read_only_fields = ('name', 'msgid', 'date', 'submitter', 'archived',
+                            'content')
+
+
+class RevisionSerializer(serializers.HyperlinkedModelSerializer):
+    patches = PatchSerializer(many=True, read_only=True)
+
+    class Meta:
+        model = SeriesRevision
+        fields = ('version', 'cover_letter', 'patches')
+        read_only_fields = ('version', 'cover_letter')
diff --git a/patchwork/urls.py b/patchwork/urls.py
index 041ba1c..67c6c6b 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -33,6 +33,13 @@ project_router.register('projects', api.ProjectViewSet)
 series_list_router = routers.NestedSimpleRouter(project_router, 'projects',
                                                 lookup='project')
 series_list_router.register(r'series', api.SeriesListViewSet)
+# /series/$id/
+series_router = routers.SimpleRouter()
+series_router.register(r'series', api.SeriesViewSet)
+# /series/$id/revisions/$rev
+revisions_router = routers.NestedSimpleRouter(series_router, 'series',
+                                             lookup='series')
+revisions_router.register(r'revisions', api.RevisionViewSet)
 
 admin.autodiscover()
 
@@ -42,6 +49,8 @@ urlpatterns = patterns('',
     # API
     (r'^api/1.0/', include(project_router.urls)),
     (r'^api/1.0/', include(series_list_router.urls)),
+    (r'^api/1.0/', include(series_router.urls)),
+    (r'^api/1.0/', include(revisions_router.urls)),
 
     # Example:
     (r'^$', 'patchwork.views.projects'),
diff --git a/patchwork/views/api.py b/patchwork/views/api.py
index fa8ebc1..95124fa 100644
--- a/patchwork/views/api.py
+++ b/patchwork/views/api.py
@@ -17,11 +17,12 @@
 # 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, Series
+from patchwork.models import Project, Series, SeriesRevision
 from rest_framework import viewsets, mixins, generics
 from rest_framework.response import Response
 from rest_framework.generics import get_object_or_404
-from patchwork.serializers import ProjectSerializer, SeriesSerializer
+from patchwork.serializers import ProjectSerializer, SeriesSerializer, \
+                                  RevisionSerializer
 
 class ProjectViewSet(viewsets.ViewSet):
     model = Project
@@ -50,3 +51,17 @@ class SeriesListViewSet(mixins.ListModelMixin,
         # Ensure queryset is re-evaluated on each request.
         queryset = self.queryset.filter(**filter_kwargs)
         return queryset
+
+class SeriesViewSet(mixins.RetrieveModelMixin,
+                    mixins.UpdateModelMixin,
+                    viewsets.GenericViewSet):
+    queryset = Series.objects.all()
+    serializer_class = SeriesSerializer
+
+class RevisionViewSet(viewsets.ViewSet):
+    model = SeriesRevision
+
+    def retrieve(self, request, series_pk=None, pk=None):
+        rev = get_object_or_404(SeriesRevision, series=series_pk, version=pk)
+        serializer = RevisionSerializer(rev)
+        return Response(serializer.data)
-- 
2.1.0



More information about the Patchwork mailing list