[PATCH v3 08/10] REST API: make patch.state a string rather than int

Andy Doan andy.doan at linaro.org
Thu May 19 13:31:00 AEST 2016


The int value isn't very useful and the goal is to move this to an enum
over time.

Signed-off-by: Andy Doan <andy.doan at linaro.org>
---
 patchwork/rest_serializers.py    | 13 ++++++++++++-
 patchwork/tests/test_rest_api.py | 12 +++++++++---
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/patchwork/rest_serializers.py b/patchwork/rest_serializers.py
index 46667b0..801978c 100644
--- a/patchwork/rest_serializers.py
+++ b/patchwork/rest_serializers.py
@@ -21,7 +21,7 @@ import email.parser
 
 from django.core.urlresolvers import reverse
 
-from patchwork.models import Check, Patch, Person, Project
+from patchwork.models import Check, Patch, Person, Project, State
 
 from rest_framework.serializers import (
     CurrentUserDefault, HiddenField, ListSerializer, ModelSerializer,
@@ -61,10 +61,21 @@ class PatchSerializer(ModelSerializer):
                             'content', 'hash', 'msgid')
     mbox_url = SerializerMethodField()
 
+    def run_validation(self, data):
+        state = data.get('state')
+        if state:
+            for st in State.objects.all():
+                if st.name == state:
+                    data['state'] = st.id
+                    break
+        return super(PatchSerializer, self).run_validation(data)
+
     def to_representation(self, instance):
         request = self.context.get('request', None)
         data = super(PatchSerializer, self).to_representation(instance)
 
+        data['state'] = instance.state.name
+
         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']]))
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 5fb4650..7ac9852 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -200,7 +200,7 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(patches[0].diff, resp.data['diff'])
         self.assertIn(TestPersonAPI.api_url(patches[0].submitter.id),
                       resp.data['submitter'])
-        self.assertEqual(patches[0].state.id, resp.data['state'])
+        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):
@@ -247,13 +247,19 @@ class TestPatchAPI(APITestCase):
         # A maintainer can update
         user = create_maintainer(defaults.project)
         self.client.force_authenticate(user=user)
-        resp = self.client.patch(self.api_url(patches[0].id), {'state': 2})
+        resp = self.client.patch(
+            self.api_url(patches[0].id), {'state': 'Accepted'})
+        self.assertEqual(status.HTTP_200_OK, resp.status_code)
+
+        resp = self.client.patch(
+            self.api_url(patches[0].id), {'pull_url': 'foo'})
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
 
         # A normal user can't
         user = create_user()
         self.client.force_authenticate(user=user)
-        resp = self.client.patch(self.api_url(patches[0].id), {'state': 2})
+        resp = self.client.patch(
+            self.api_url(patches[0].id), {'state': 'Accepted'})
         self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
 
     def test_delete(self):
-- 
2.7.4



More information about the Patchwork mailing list