aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2016-06-16 16:13:25 -0500
committerStephen Finucane <stephen.finucane@intel.com>2016-06-27 18:20:37 +0100
commitc84476d0052fe948524aaa57c6ac67bfd08837ef (patch)
tree4c5b4555d444779ef48218ee5374a0637f19603b
parentdcc343f2ba7786abd564e4db538e9b0c17e20e1a (diff)
downloadpatchwork-c84476d0052fe948524aaa57c6ac67bfd08837ef.tar
patchwork-c84476d0052fe948524aaa57c6ac67bfd08837ef.tar.gz
REST: Allow projects to be retrieved by linkname
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 <andy.doan@linaro.org> Reviewed-by: Stephen Finucane <stephen.finucane@intel.com>
-rw-r--r--patchwork/tests/test_rest_api.py14
-rw-r--r--patchwork/views/rest_api.py18
2 files changed, 32 insertions, 0 deletions
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index 4f5886f..8131c64 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -60,6 +60,20 @@ class TestProjectAPI(APITestCase):
self.assertEqual(status.HTTP_200_OK, resp.status_code)
self.assertEqual(self.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."""
resp = self.client.post(
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,)