From patchwork Thu May 19 03:31:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Doan X-Patchwork-Id: 623783 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r9GqT4llbz9vL6 for ; Thu, 19 May 2016 13:33:09 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=IVErow8q; dkim-atps=neutral Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3r9GqT2wfwzDqLZ for ; Thu, 19 May 2016 13:33:09 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=IVErow8q; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mail-oi0-x22f.google.com (mail-oi0-x22f.google.com [IPv6:2607:f8b0:4003:c06::22f]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3r9GnK5xLvzDqGs for ; Thu, 19 May 2016 13:31:17 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=IVErow8q; dkim-atps=neutral Received: by mail-oi0-x22f.google.com with SMTP id x201so109048213oif.3 for ; Wed, 18 May 2016 20:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=f+OT9S8Q52wwXe9tgV1gKoiQbse+2vboynsoFqaWPqE=; b=IVErow8qPGYDShF4BzfvRPfrFFdNfgqcP5OQyTOTfdncC4ELC0JoY7trgVRHGVdw4/ 3jvXiAnIPXM4Ot1amMQvDVQsB1JwXrqVmkI3nZkGV7Uuy69/1thTiOY+Y/Np8rQLZvcv 3N3ahThwqexLh93sRnmHQcQJOmK4nLLeMBxsg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=f+OT9S8Q52wwXe9tgV1gKoiQbse+2vboynsoFqaWPqE=; b=CnZHSha8OOSEAkfJgeMm9msr6GGkmI6XG+lOXAMCv8XPaygcKPxZB86dyTxLmTxXda MileJv1vUIra8bd3W1/GRvIN09yH3dXLFrKzkSSAzkzFw6KZlLc9XzN9ExPN9atbkHp6 AhIUwfP540m8Fnk2FtI+ai+gB+QjRiQVqx1rc8zOv/9dHt2HQkckkn1+mGGCPR4MqFFz K1M4qelldYkYfxlfSRX56MtD3YJJzps4igSEL4aoFaGxBDgi93qmXuDzVTrRZnjKHLeN bpJRQI0NRSeEo9U8bSBzX4aLCLaedvnbAkrdUTbBfcS7SlSOVWldLosop1To2zwhPsKv QENA== X-Gm-Message-State: AOPr4FUXGrZKf22seoE//tjCAKvomUsyGeRdi34UwgbGI7Apal8TwDfLN+h5N3YreA7kaVqn X-Received: by 10.157.18.179 with SMTP id g48mr5757278otg.53.1463628675950; Wed, 18 May 2016 20:31:15 -0700 (PDT) Received: from doanac-xps.local (cpe-70-117-126-139.austin.res.rr.com. [70.117.126.139]) by smtp.gmail.com with ESMTPSA id m19sm35273obe.12.2016.05.18.20.31.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 May 2016 20:31:15 -0700 (PDT) From: Andy Doan To: patchwork@lists.ozlabs.org Subject: [PATCH v3 08/10] REST API: make patch.state a string rather than int Date: Wed, 18 May 2016 22:31:00 -0500 Message-Id: <1463628662-26284-9-git-send-email-andy.doan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1463628662-26284-1-git-send-email-andy.doan@linaro.org> References: <1463628662-26284-1-git-send-email-andy.doan@linaro.org> X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" The int value isn't very useful and the goal is to move this to an enum over time. Signed-off-by: Andy Doan Reviewed-by: Stephen Finucane --- 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):