From patchwork Thu Jun 16 21:13:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Doan X-Patchwork-Id: 636685 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rVx4s5GPmz9t1W for ; Fri, 17 Jun 2016 07:15:57 +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=JIYatvAg; dkim-atps=neutral Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3rVx4s4D4jzDqs4 for ; Fri, 17 Jun 2016 07:15:57 +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=JIYatvAg; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mail-oi0-x22b.google.com (mail-oi0-x22b.google.com [IPv6:2607:f8b0:4003:c06::22b]) (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 3rVx2G5bcTzDqsx for ; Fri, 17 Jun 2016 07:13:42 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=JIYatvAg; dkim-atps=neutral Received: by mail-oi0-x22b.google.com with SMTP id p204so89267682oih.3 for ; Thu, 16 Jun 2016 14:13:42 -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=8BuwAcufTsQvsCnLC14E9mcglZjxCmt/21m+8T88x9k=; b=JIYatvAgEqpq0LCbtBMqzv+gpk3epDIDgJxVwIsxd2BPY8fMDSXBChdwThA491w5OT 0yK47BAOQ3kn7bmqxXjHDyQf0iCqmzWriBg16vzTIRZSjn+4xXBtENu/TK4qK/6U/HJF KxRoOTeHf68/8BqDw7AHx4KhAvj0jgMO4qEow= 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=8BuwAcufTsQvsCnLC14E9mcglZjxCmt/21m+8T88x9k=; b=QRmvHiQSKd8znk5kE62uddienlrDFerUfpabcfA720zX+g9EmLiEyetoWkEJCB7rlM LAyVjqU0eeGawNKKn1rEOGGdLdOYB2jGTKW4LYfc84Pjs8/EFR90he0qhfIpyPq6aqs5 ck8LRPTFb6IDA00iJrzvtew92n7Q4cGHzfigLoOMdA3ZVRogA724Fat7HBnl4IBETZvM TDgorAtRy7M1ntC06gnthCpAKme2spSLcTEJeQoLsxOL8fhNq7mq/XtjWwO5VifjgiBz 9FFgET2Owxy9Ta9LNdXv/osnlLKhLp3id0USlfIW4fhE2AXnejLDc9oYQu8vgyBX8Wjg 8K+A== X-Gm-Message-State: ALyK8tLbp8W5OveOZy5aWHB7dXcX7RNpmu/wLM5tq/kAMUu3FQreeHoV9mjHCnL9aJQ9cXZ9 X-Received: by 10.202.51.133 with SMTP id z127mr3839512oiz.194.1466111620696; Thu, 16 Jun 2016 14:13:40 -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 k79sm9554444oih.12.2016.06.16.14.13.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 Jun 2016 14:13:40 -0700 (PDT) From: Andy Doan To: patchwork@lists.ozlabs.org Subject: [PATCH v6 10/10] REST: Allow projects to be retrieved by linkname Date: Thu, 16 Jun 2016 16:13:25 -0500 Message-Id: <1466111605-3059-11-git-send-email-andy.doan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1466111605-3059-1-git-send-email-andy.doan@linaro.org> References: <1466111605-3059-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" Building a user-friendly CLI becomes difficult when project-ids are required. It also makes it almost impossible to work with the current format of the .pwclientrc file. Signed-off-by: Andy Doan Reviewed-by: Stephen Finucane --- patchwork/tests/test_rest_api.py | 14 ++++++++++++++ patchwork/views/rest_api.py | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py index f303e42..61dd24e 100644 --- a/patchwork/tests/test_rest_api.py +++ b/patchwork/tests/test_rest_api.py @@ -58,6 +58,20 @@ class TestProjectAPI(APITestCase): self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(defaults.project.name, resp.data['name']) + # make sure we can look up by linkname + resp = self.client.get(self.api_url(resp.data['link_name'])) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(defaults.project.name, resp.data['name']) + + def test_get_numeric_linkname(self): + """Validate we try to do the right thing for numeric linkname""" + project = Project(linkname='12345', name='Test Project', + listid='test.example.com') + project.save() + resp = self.client.get(self.api_url('12345')) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(project.name, resp.data['name']) + def test_anonymous_create(self): """Ensure anonymous POST operations are rejected.""" defaults.project.save() diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py index 7d798a1..d412587 100644 --- a/patchwork/views/rest_api.py +++ b/patchwork/views/rest_api.py @@ -101,6 +101,24 @@ class ProjectViewSet(PatchworkViewSet): permission_classes = (PatchworkPermission, ) serializer_class = ProjectSerializer + def _handle_linkname(self, pk): + '''Make it easy for users to list by project-id or linkname''' + qs = self.get_queryset() + try: + qs.get(id=pk) + except (self.serializer_class.Meta.model.DoesNotExist, ValueError): + # probably a non-numeric value which means we are going by linkname + self.kwargs = {'linkname': pk} # try and lookup by linkname + self.lookup_field = 'linkname' + + def retrieve(self, request, pk=None): + self._handle_linkname(pk) + return super(ProjectViewSet, self).retrieve(request, pk) + + def partial_update(self, request, pk=None): + self._handle_linkname(pk) + return super(ProjectViewSet, self).partial_update(request, pk) + class PatchViewSet(PatchworkViewSet): permission_classes = (PatchworkPermission,)