diff mbox

[18/51] api: Expose projects

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

Commit Message

Damien Lespiau Sept. 11, 2015, 3:54 p.m. UTC
We'll expose Series and Patches as sub-urls of projects. So they are a
good object to start with.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 apps/patchwork/serializers.py | 27 +++++++++++++++++++++++++++
 apps/patchwork/views/api.py   | 36 ++++++++++++++++++++++++++++++++++++
 patchwork/urls.py             | 12 ++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 apps/patchwork/serializers.py
 create mode 100644 apps/patchwork/views/api.py
diff mbox

Patch

diff --git a/apps/patchwork/serializers.py b/apps/patchwork/serializers.py
new file mode 100644
index 0000000..f210af3
--- /dev/null
+++ b/apps/patchwork/serializers.py
@@ -0,0 +1,27 @@ 
+# Patchwork - automated patch tracking system
+# Copyright (C) 2014 Intel Corporation
+#
+# 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 patchwork.models import Project
+from rest_framework import serializers
+
+class ProjectSerializer(serializers.HyperlinkedModelSerializer):
+    class Meta:
+        model = Project
+        fields = ('name', 'linkname', 'listemail', 'web_url', 'scm_url',
+                  'webscm_url')
diff --git a/apps/patchwork/views/api.py b/apps/patchwork/views/api.py
new file mode 100644
index 0000000..30b42c2
--- /dev/null
+++ b/apps/patchwork/views/api.py
@@ -0,0 +1,36 @@ 
+# Patchwork - automated patch tracking system
+# Copyright (C) 2014 Intel Corporation
+#
+# 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 patchwork.models import Project
+from rest_framework import viewsets
+from rest_framework.response import Response
+from patchwork.serializers import ProjectSerializer
+
+class ProjectViewSet(viewsets.ViewSet):
+    model = Project
+
+    def list(self, request):
+        queryset = Project.objects.all()
+        serializer = ProjectSerializer(queryset, many=True)
+        return Response(serializer.data)
+
+    def retrieve(self, request, pk=None):
+        queryset = Project.objects.get(name=pk)
+        serializer = ProjectSerializer(queryset)
+        return Response(serializer.data)
diff --git a/patchwork/urls.py b/patchwork/urls.py
index b28eb90..e7a0c85 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -21,12 +21,24 @@  from django.conf.urls import patterns, url, include
 from django.conf import settings
 from django.contrib import admin
 from django.contrib.auth import views as auth_views
+from rest_framework_nested import routers
+import patchwork.views.api as api
+
+# API
+
+# /projects/$project/
+project_router = routers.SimpleRouter()
+project_router.register('projects', api.ProjectViewSet)
 
 admin.autodiscover()
 
 urlpatterns = patterns('',
     url(r'^admin/', include(admin.site.urls)),
 
+    # API
+    (r'^api/1.0/', include(project_router.urls)),
+
+    # Example:
     (r'^$', 'patchwork.views.projects'),
     (r'^project/(?P<project_id>[^/]+)/list/$', 'patchwork.views.patch.list'),
     (r'^project/(?P<project_id>[^/]+)/$', 'patchwork.views.project.project'),