diff mbox

[RFC,11/11] REST: Add States to the API

Message ID 1460744647-9729-12-git-send-email-andy.doan@linaro.org
State Superseded
Headers show

Commit Message

Andy Doan April 15, 2016, 6:24 p.m. UTC
This exports patch States via the REST API.

Security Constraints:
 * Anyone (logged in or not) can read all objects.
 * No one can create/update/delete

Signed-off-by: Andy Doan <andy.doan@linaro.org>
---
 patchwork/views/rest_api.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Stephen Finucane May 9, 2016, 2:10 p.m. UTC | #1
On 15 Apr 13:24, Andy Doan wrote:
> This exports patch States via the REST API.
> 
> Security Constraints:
>  * Anyone (logged in or not) can read all objects.
>  * No one can create/update/delete
> 
> Signed-off-by: Andy Doan <andy.doan@linaro.org>

I'd like to drop the whole customisable states feature in favour of an
enum [1]. Unless you've a good reason to include this, can we avoid
putting it into the API?

[1] https://github.com/getpatchwork/patchwork/issues/4

> ---
>  patchwork/views/rest_api.py | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py
> index e2ec616..15a75c7 100644
> --- a/patchwork/views/rest_api.py
> +++ b/patchwork/views/rest_api.py
> @@ -19,7 +19,7 @@
>  
>  from django.conf.urls import url, include
>  
> -from patchwork.models import Check, Patch, Person, Project
> +from patchwork.models import Check, Patch, Person, Project, State
>  from patchwork.views.patch import mbox
>  
>  from rest_framework import permissions
> @@ -29,7 +29,8 @@ from rest_framework.response import Response
>  from rest_framework.routers import DefaultRouter
>  from rest_framework.serializers import (
>      CurrentUserDefault, HiddenField, ModelSerializer, PrimaryKeyRelatedField)
> -from rest_framework.viewsets import GenericViewSet, ModelViewSet
> +from rest_framework.viewsets import (
> +    GenericViewSet, ModelViewSet, ReadOnlyModelViewSet)
>  
>  from rest_framework_nested.routers import NestedSimpleRouter
>  
> @@ -200,10 +201,18 @@ class MboxViewSet(GenericViewSet):
>          return mbox(request, patch_pk)
>  
>  
> +class StateViewSet(ReadOnlyModelViewSet):
> +    serializer_class = create_model_serializer(State)
> +
> +    def get_queryset(self):
> +        return self.serializer_class.Meta.model.objects.all()
> +
> +
>  router = DefaultRouter()
>  router.register('patches', PatchViewSet, 'patch')
>  router.register('people', PeopleViewSet, 'person')
>  router.register('projects', ProjectViewSet, 'project')
> +router.register('states', StateViewSet, 'state')
>  
>  patches_router = NestedSimpleRouter(router, r'patches', lookup='patch')
>  patches_router.register(r'checks', ChecksViewSet, base_name='patch-checks')
> -- 
> 2.7.4
> 
> _______________________________________________
> Patchwork mailing list
> Patchwork@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
Andy Doan May 10, 2016, 10:30 p.m. UTC | #2
On 05/09/2016 09:10 AM, Finucane, Stephen wrote:
> I'd like to drop the whole customisable states feature in favour of an
> enum [1]. Unless you've a good reason to include this, can we avoid
> putting it into the API?

I included this because Patch.state is returned as a numeric ID. We need 
the strings to show something useful in CLI tool. I've updated the v2 
version of this series to return the patch state as string so that this 
patch isn't needed.
diff mbox

Patch

diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py
index e2ec616..15a75c7 100644
--- a/patchwork/views/rest_api.py
+++ b/patchwork/views/rest_api.py
@@ -19,7 +19,7 @@ 
 
 from django.conf.urls import url, include
 
-from patchwork.models import Check, Patch, Person, Project
+from patchwork.models import Check, Patch, Person, Project, State
 from patchwork.views.patch import mbox
 
 from rest_framework import permissions
@@ -29,7 +29,8 @@  from rest_framework.response import Response
 from rest_framework.routers import DefaultRouter
 from rest_framework.serializers import (
     CurrentUserDefault, HiddenField, ModelSerializer, PrimaryKeyRelatedField)
-from rest_framework.viewsets import GenericViewSet, ModelViewSet
+from rest_framework.viewsets import (
+    GenericViewSet, ModelViewSet, ReadOnlyModelViewSet)
 
 from rest_framework_nested.routers import NestedSimpleRouter
 
@@ -200,10 +201,18 @@  class MboxViewSet(GenericViewSet):
         return mbox(request, patch_pk)
 
 
+class StateViewSet(ReadOnlyModelViewSet):
+    serializer_class = create_model_serializer(State)
+
+    def get_queryset(self):
+        return self.serializer_class.Meta.model.objects.all()
+
+
 router = DefaultRouter()
 router.register('patches', PatchViewSet, 'patch')
 router.register('people', PeopleViewSet, 'person')
 router.register('projects', ProjectViewSet, 'project')
+router.register('states', StateViewSet, 'state')
 
 patches_router = NestedSimpleRouter(router, r'patches', lookup='patch')
 patches_router.register(r'checks', ChecksViewSet, base_name='patch-checks')