[PATCH v3 07/10] REST: Add Patch.mbox_url

Andy Doan andy.doan at linaro.org
Thu May 19 13:30:59 AEST 2016


Provide a URL to the raw patch.

Signed-off-by: Andy Doan <andy.doan at linaro.org>
Reviewed-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/models.py              | 4 ++++
 patchwork/rest_serializers.py    | 7 ++++++-
 patchwork/tests/test_rest_api.py | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/patchwork/models.py b/patchwork/models.py
index 6324273..6209527 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -458,6 +458,10 @@ class Patch(Submission):
     def get_absolute_url(self):
         return ('patch-detail', (), {'patch_id': self.id})
 
+    @models.permalink
+    def get_mbox_url(self):
+        return ('patch-mbox', (), {'patch_id': self.id})
+
     def __str__(self):
         return self.name
 
diff --git a/patchwork/rest_serializers.py b/patchwork/rest_serializers.py
index 5f16954..46667b0 100644
--- a/patchwork/rest_serializers.py
+++ b/patchwork/rest_serializers.py
@@ -25,7 +25,7 @@ from patchwork.models import Check, Patch, Person, Project
 
 from rest_framework.serializers import (
     CurrentUserDefault, HiddenField, ListSerializer, ModelSerializer,
-    PrimaryKeyRelatedField)
+    PrimaryKeyRelatedField, SerializerMethodField)
 
 
 class PersonSerializer(ModelSerializer):
@@ -59,11 +59,13 @@ class PatchSerializer(ModelSerializer):
         list_serializer_class = PatchListSerializer
         read_only_fields = ('project', 'name', 'date', 'submitter', 'diff',
                             'content', 'hash', 'msgid')
+    mbox_url = SerializerMethodField()
 
     def to_representation(self, instance):
         request = self.context.get('request', None)
         data = super(PatchSerializer, self).to_representation(instance)
 
+        data['mbox_url'] = request.build_absolute_uri(data['mbox_url'])
         data['project'] = request.build_absolute_uri(
             reverse('api_1.0:project-detail', args=[data['project']]))
         data['submitter'] = request.build_absolute_uri(
@@ -73,6 +75,9 @@ class PatchSerializer(ModelSerializer):
             data['headers'] = email.parser.Parser().parsestr(headers, True)
         return data
 
+    def get_mbox_url(self, patch):
+        return patch.get_mbox_url()
+
 
 class CurrentPatchDefault(object):
     def set_context(self, serializer_field):
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 7e2545d..5fb4650 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -201,6 +201,7 @@ class TestPatchAPI(APITestCase):
         self.assertIn(TestPersonAPI.api_url(patches[0].submitter.id),
                       resp.data['submitter'])
         self.assertEqual(patches[0].state.id, resp.data['state'])
+        self.assertIn(patches[0].get_mbox_url(), resp.data['mbox_url'])
 
     def test_anonymous_writes(self):
         """Ensure anonymous "write" operations are rejected."""
-- 
2.7.4



More information about the Patchwork mailing list