[Patchew-devel] [PATCH] rest: Add endpoint from update-project-head

Paolo Bonzini pbonzini at redhat.com
Tue May 29 12:10:08 UTC 2018


On 29/05/2018 14:05, Shubham wrote:
> From: Wingify <wingify at Wingifys-MacBook-Air-3.local>
> 
> Added extra action in the ProjectViewSet which will update the project head at endpoint /projects/../update_project_head. This is legacy conversion(UpdateProjectHeadView) into rest
> ---
>  api/rest.py | 25 ++++++++++++++++++++++++-
>  1 file changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/api/rest.py b/api/rest.py
> index fa6ca3f..e9282c3 100644
> --- a/api/rest.py
> +++ b/api/rest.py
> @@ -18,7 +18,7 @@ from .models import Project, Message
>  from .search import SearchEngine
>  from rest_framework import (permissions, serializers, viewsets, filters,
>      mixins, generics, renderers, status)
> -from rest_framework.decorators import detail_route
> +from rest_framework.decorators import detail_route, action
>  from rest_framework.fields import SerializerMethodField, CharField, JSONField, EmailField
>  from rest_framework.relations import HyperlinkedIdentityField
>  from rest_framework.response import Response
> @@ -141,6 +141,29 @@ class ProjectsViewSet(viewsets.ModelViewSet):
>      serializer_class = ProjectSerializer
>      permission_classes = (PatchewPermission,)
>  
> +    @action(methods=['post','get'], detail=True, permission_classes=[ImportPermission])

I think this should be POST only.  Retrieving the current head should be
doable with the /projects/{pk}/ endpoint.

Also please make the URL use dashes, like update-project-head

> +    def update_project_head(self, request, pk=None):
> +        """
> +        updates the project head and message_id which are matched are merged. 
> +        Data input format:
> +        {
> +            "old_head": "..",
> +            "new_head": "..",
> +            "message_ids": []
> +        }
> +        """
> +        project = self.get_object()
> +        head = project.project_head
> +        if request.method == 'POST':
> +            old_head = request.data['old_head']
> +            message_ids = request.data['message_ids']
> +            if old_head_0 and old_head_0 != old_head:

What is old_head_0?

> +                raise Exception("wrong old head")

This should return an HTTP status code of "409 Conflict" I think.

Finally, you need to add unit tests too.  You can copy-and-paste the
tests for the legacy API.

Thanks,

Paolo

> +            ret = project.series_update(message_ids)
> +            project.project_head = request.data['new_head']
> +            Response({"new_head": project.project_head, "count": ret})
> +        else:
> +            return Response({'project_head': head})
>  # Common classes for series and messages
>  
>  class HyperlinkedMessageField(HyperlinkedIdentityField):





More information about the Patchew-devel mailing list