[Patchew-devel] [PATCH v2 1/3] Make Patchew cli use rest api

Fam Zheng famz at redhat.com
Mon Jul 2 07:04:06 UTC 2018


On Fri, 06/29 19:35, Shubham Jain wrote:
> -Make ProjectCommand use rest_api_do instead of api_do

I think the $subject should say "project" commands too.

> ---
>  api/rest.py |  3 ++-
>  patchew-cli | 34 +++++++++++++++++++---------------
>  2 files changed, 21 insertions(+), 16 deletions(-)
> 
> diff --git a/api/rest.py b/api/rest.py
> index 66644ba..39bdda0 100644
> --- a/api/rest.py
> +++ b/api/rest.py
> @@ -31,7 +31,7 @@ class CsrfExemptSessionAuthentication(SessionAuthentication):
>  
>      def enforce_csrf(self, request):
>          return  # To not perform the csrf check previously happening
> -        
> +
>  SEARCH_PARAM = 'q'
>  
>  # patchew-specific permission classes
> @@ -146,6 +146,7 @@ class ProjectsViewSet(viewsets.ModelViewSet):
>      queryset = Project.objects.all().order_by('id')
>      serializer_class = ProjectSerializer
>      permission_classes = (PatchewPermission,)
> +    authentication_classes = (CsrfExemptSessionAuthentication, )
>  
>      @action(methods=['post'], detail=True, permission_classes=[ImportPermission])
>      def update_project_head(self, request, pk=None):
> diff --git a/patchew-cli b/patchew-cli
> index e510e74..8425db2 100755
> --- a/patchew-cli
> +++ b/patchew-cli
> @@ -255,11 +255,11 @@ class ProjectCommand(SubCommand):
>          parser.add_argument("--verbose", "-v", action="store_true",
>                              help="Show details about projects")
>          args = parser.parse_args(argv)
> -        r = self.api_do("get-projects")
> +        r = self.rest_api_do(url_cmd="projects", request_method='get')
>          if args.raw:
>              print(json.dumps(r, indent=2, separators=",:"))
>              return 0
> -        for p in r:
> +        for p in r['results']:
>              print(p["name"])
>              if args.verbose:
>                  for k, v in p.items():
> @@ -273,8 +273,8 @@ class ProjectCommand(SubCommand):
>          parser.add_argument("name", nargs="+",
>                              help="The name of project to show info")
>          args = parser.parse_args(argv)
> -        r = self.api_do("get-projects")
> -        for p in r:
> +        r = self.rest_api_do(url_cmd="projects", request_method='get')
> +        for p in r['results']:
>              if not p["name"] in args.name:
>                  continue
>              if len(args.name) > 1:
> @@ -297,12 +297,15 @@ class ProjectCommand(SubCommand):
>          parser.add_argument("--desc", "-d", default="",
>                              help="Project short discription")
>          args = parser.parse_args(argv)
> -        self.api_do("add-project",
> -                    name=args.name,
> -                    mailing_list=args.mailing_list,
> -                    url=args.url,
> -                    git=args.git,
> -                    description=args.desc)
> +        data = {'name':args.name,
> +                'mailing_list':args.mailing_list,
> +                'url':args.url,
> +                'git':args.git,
> +                'description':args.desc}
> +        self.rest_api_do(url_cmd="projects",
> +                         request_method='post',
> +                         content_type='application/json',
> +                         data=json.dumps(data))
>  
>      def update_one_project(self, wd, project):
>          logging.info("Updating project '%s'", project["name"])
> @@ -354,16 +357,17 @@ class ProjectCommand(SubCommand):
>              except Exception as e:
>                  logging.warn("Failed to push the new head to patchew mirror: %s",
>                               str(e))
> -        self.api_do("update-project-head", project=project["name"],
> -                                           old_head=old_head,
> -                                           new_head=new_head,
> -                                           message_ids=msgids)
> +        url = project['resource_uri'] + "/update_project_head/"
> +        self.rest_api_do(url, old_head=old_head,
> +                         new_head=new_head,
> +                         message_ids=msgids)
>  
>      def update_project(self, argv):
>          parser = argparse.ArgumentParser()
>          parser.add_argument("--name", "-n", help="Name of the project")
>          args = parser.parse_args(argv)
> -        projects = self.api_do("get-projects", name=args.name)
> +        projects = self.rest_api_do("projects")
> +        projects = [p for p in projects['results'] if p['name'] == args.name]
>          wd = tempfile.mkdtemp()
>          logging.debug("TMPDIR: %s", wd)
>          try:
> -- 
> 2.15.1 (Apple Git-101)
> 
> _______________________________________________
> Patchew-devel mailing list
> Patchew-devel at redhat.com
> https://www.redhat.com/mailman/listinfo/patchew-devel




More information about the Patchew-devel mailing list