[RFC 08/11] REST: Add Patch mbox to the API

Andy Doan andy.doan at linaro.org
Sat Apr 16 04:24:04 AEST 2016


This allows a user to download the Patch in the MBOX format directly
from the API.

Security Constraints:
 * Read-only to everyone

Signed-off-by: Andy Doan <andy.doan at linaro.org>
---
 patchwork/tests/test_rest_api.py | 8 ++++++++
 patchwork/views/rest_api.py      | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index f98ad75..fe21fcf 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -237,6 +237,14 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
         self.assertEqual(1, Patch.objects.all().count())
 
+    def test_mbox(self):
+        """Ensure we can download the raw mbox version of the patch."""
+        patches = create_patches()
+        resp = self.client.get('/api/1.0/patches/%d/mbox/' % patches[0].id)
+        self.assertEqual(status.HTTP_200_OK, resp.status_code)
+        self.assertIn('\nMIME-Version:', resp.content)
+        self.assertIn(patches[0].diff, resp.content)
+
 
 @unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
 class TestCheckAPI(APITestCase):
diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py
index 3918a7a..2e7fcbb 100644
--- a/patchwork/views/rest_api.py
+++ b/patchwork/views/rest_api.py
@@ -20,6 +20,7 @@
 from django.conf.urls import url, include
 
 from patchwork.models import Check, Patch, Person, Project
+from patchwork.views.patch import mbox
 
 from rest_framework import permissions
 from rest_framework.exceptions import PermissionDenied
@@ -142,6 +143,11 @@ class CheckViewSet(GenericViewSet):
         return Response({'state': state})
 
 
+class MboxViewSet(GenericViewSet):
+    def list(self, request, patch_pk):
+        return mbox(request, patch_pk)
+
+
 router = DefaultRouter()
 router.register('patches', PatchViewSet, 'patch')
 router.register('people', PeopleViewSet, 'person')
@@ -150,6 +156,7 @@ router.register('projects', ProjectViewSet, 'project')
 patches_router = NestedSimpleRouter(router, r'patches', lookup='patch')
 patches_router.register(r'checks', ChecksViewSet, base_name='patch-checks')
 patches_router.register(r'check', CheckViewSet, base_name='patch-check')
+patches_router.register(r'mbox', MboxViewSet, base_name='patch-mbox')
 
 urlpatterns = [
     url(r'^api/1.0/', include(router.urls)),
-- 
2.7.4



More information about the Patchwork mailing list