[RFC 10/11] REST: Allow projects to be retrieved by linkname

Finucane, Stephen stephen.finucane at intel.com
Tue May 10 00:09:20 AEST 2016


On 15 Apr 13:24, Andy Doan wrote:
> 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 at linaro.org>

Good idea. One question.

Stephen

> ---
>  patchwork/tests/test_rest_api.py |  5 +++++
>  patchwork/views/rest_api.py      | 16 ++++++++++++++++
>  2 files changed, 21 insertions(+)
> 
> diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
> index 037c4e8..56bb6f7 100644
> --- a/patchwork/tests/test_rest_api.py
> +++ b/patchwork/tests/test_rest_api.py
> @@ -52,6 +52,11 @@ 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('/api/1.0/projects/%s/' % resp.data['linkname'])
> +        self.assertEqual(status.HTTP_200_OK, resp.status_code)
> +        self.assertEqual(defaults.project.name, resp.data['name'])
> +
>      def test_anonymous_writes(self):
>          """Ensure anonymous "write" operations are rejected."""
>          defaults.project.save()
> diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py
> index ae86627..e2ec616 100644
> --- a/patchwork/views/rest_api.py
> +++ b/patchwork/views/rest_api.py
> @@ -127,6 +127,22 @@ class ProjectViewSet(PatchworkViewSet):
>      permission_classes = (PatchworkPermission,)
>      serializer_class = create_model_serializer(Project)
>  
> +    def retrieve(self, request, pk=None):
> +        try:
> +            int(pk)
> +        except ValueError:
> +            self.kwargs = {'linkname': pk}  # try and lookup by linkname
> +            self.lookup_field = 'linkname'
> +        return super(ProjectViewSet, self).retrieve(request, pk)
> +
> +    def partial_update(self, request, pk=None):
> +        try:
> +            int(pk)
> +        except ValueError:
> +            self.kwargs = {'linkname': pk}  # try and lookup by linkname
> +            self.lookup_field = 'linkname'
> +        return super(ProjectViewSet, self).partial_update(request, pk)

I'm not sure of the lifecycle of this object, but do we need to worry
about changing 'lookup_field' to 'id' an unsetting kwards in the
non-exception path? Will we ever reenter this function from a different
point?

> +
>  
>  class CurrentPatchDefault(object):
>      def set_context(self, serializer_field):
> -- 
> 2.7.4
> 
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork


More information about the Patchwork mailing list