[PATCH v4 08/10] REST: Add Patch.mbox_url

Andy Doan andy.doan at linaro.org
Sat May 21 05:17:10 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    | 5 +++++
 patchwork/tests/test_rest_api.py | 1 +
 3 files changed, 10 insertions(+)

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 94ac469..4fcd392 100644
--- a/patchwork/rest_serializers.py
+++ b/patchwork/rest_serializers.py
@@ -66,11 +66,16 @@ class PatchSerializer(HyperlinkedModelSerializer):
         list_serializer_class = PatchListSerializer
         read_only_fields = ('project', 'name', 'date', 'submitter', 'diff',
                             'content', 'hash', 'msgid')
+    mbox_url = SerializerMethodField()
     state = SerializerMethodField()
 
     def get_state(self, obj):
         return obj.state.name
 
+    def get_mbox_url(self, patch):
+        request = self.context.get('request', None)
+        return request.build_absolute_uri(patch.get_mbox_url())
+
     def to_representation(self, instance):
         data = super(PatchSerializer, self).to_representation(instance)
         data['checks'] = data['url'] + 'checks/'
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 2be3ed5..44be7d1 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -260,6 +260,7 @@ class TestPatchAPI(APITestCase):
         self.assertIn(TestPersonAPI.api_url(patches[0].submitter.id),
                       resp.data['submitter'])
         self.assertEqual(patches[0].state.name, 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