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

Andy Doan andy.doan at linaro.org
Wed May 11 08:39:25 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    | 21 ++++++++++++++++++++-
 patchwork/tests/test_rest_api.py |  8 +++++---
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/patchwork/rest_serializers.py b/patchwork/rest_serializers.py
index 6760948..f189e89 100644
--- a/patchwork/rest_serializers.py
+++ b/patchwork/rest_serializers.py
@@ -17,7 +17,7 @@
 # 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 Check, Patch, Person, Project
+from patchwork.models import Check, Patch, Person, Project, State
 
 from rest_framework.serializers import (
     CurrentUserDefault, ModelSerializer, HiddenField, PrimaryKeyRelatedField,
@@ -41,6 +41,25 @@ class PatchSerializer(ModelSerializer):
                             'content', 'hash', 'msgid')
     mbox_url = SerializerMethodField()
 
+    def run_validation(self, data):
+        for state in State.objects.all():
+            if state.name == data['state']:
+                data['state'] = state.id
+                break
+        return super(PatchSerializer, self).run_validation(data)
+
+    def to_representation(self, instance):
+        data = super(PatchSerializer, self).to_representation(instance)
+        # an instance of this object lives across a single http request, so
+        # we can cache patch states once and not do repeated lookups
+        if not getattr(self, 'patch_states_cache', None):
+            self.patch_states_cache = [
+                (x.id, x.name) for x in State.objects.all()]
+        for state, name in self.patch_states_cache:
+            if state == data['state']:
+                data['state'] = name
+        return data
+
     def get_mbox_url(self, patch):
         return patch.get_mbox_url()
 
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 780edd4..2bf8f94 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -194,7 +194,7 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(patches[0].msgid, resp.data['msgid'])
         self.assertEqual(patches[0].diff, resp.data['diff'])
         self.assertEqual(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.assertEqual(patches[0].get_mbox_url(), resp.data['mbox_url'])
 
     def test_anonymous_writes(self):
@@ -241,13 +241,15 @@ 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)
 
         # 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