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

Paolo Bonzini pbonzini at redhat.com
Mon Jun 4 12:09:56 UTC 2018


On 01/06/2018 05:54, cic wrote:
> From: Shubham <shubhamjain7495 at gmail.com>
> 
> 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        | 26 +++++++++++++++++++++++++-
>  tests/test_rest.py | 18 ++++++++++++++++++
>  2 files changed, 43 insertions(+), 1 deletion(-)
> 
> diff --git a/api/rest.py b/api/rest.py
> index fa6ca3f..d62a144 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,30 @@ class ProjectsViewSet(viewsets.ModelViewSet):
>      serializer_class = ProjectSerializer
>      permission_classes = (PatchewPermission,)
>  
> +    @action(methods=['post','get'], detail=True, permission_classes=[ImportPermission])
> +    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 head and head != old_head:
> +                raise Exception("wrong old head")
> +            ret = project.series_update(message_ids)
> +            project.project_head = request.data['new_head']
> +            return Response({"new_head": project.project_head, "count": ret})
> +        else:
> +            return Response({'project_head': head})

Maybe my message didn't reach the list.  I suggested returning a 409
Conflict exception for the "wrong old head" case, and not supporting GET
requests at all (it's an "update" endpoint).

Otherwise looks good!

Thanks,

Paolo

>  # Common classes for series and messages
>  
>  class HyperlinkedMessageField(HyperlinkedIdentityField):
> diff --git a/tests/test_rest.py b/tests/test_rest.py
> index 1399801..2a85815 100755
> --- a/tests/test_rest.py
> +++ b/tests/test_rest.py
> @@ -86,6 +86,24 @@ class RestTest(PatchewTestCase):
>          self.assertEquals(resp.data['mailing_list'], "qemu-block at nongnu.org")
>          self.assertEquals(resp.data['parent_project'], self.PROJECT_BASE)
>  
> +    def test_update_project_head(self):
> +       	resp = self.apply_and_retrieve('0001-simple-patch.mbox.gz',
> +                                       self.p.id, '20160628014747.20971-1-famz at redhat.com')
> +        self.api_client.login(username=self.user, password=self.password)        
> +        resp_before = self.api_client.get(self.PROJECT_BASE + "series/"+ "20160628014747.20971-1-famz at redhat.com/")
> +        data = {
> +                "message_ids": ["20160628014747.20971-1-famz at redhat.com"],
> +                "old_head": "None",
> +                "new_head": "000000"
> +                }
> +        resp = self.api_client.post(self.PROJECT_BASE + "update_project_head/", data=json.dumps(data), content_type='application/json')
> +        resp_after = self.api_client.get(self.PROJECT_BASE + "series/"+ "20160628014747.20971-1-famz at redhat.com/")
> +        self.assertEquals(resp_before.data['is_merged'], False)
> +        self.assertEquals(resp.status_code, 200)
> +        self.assertEquals(resp.data['count'], 1)
> +        self.assertEquals(resp.data['new_head'], "000000")
> +        self.assertEquals(resp_after.data['is_merged'], True)
> +
>      def test_project_post_no_login(self):
>          data = {
>              'name': 'keycodemapdb',
> 




More information about the Patchew-devel mailing list