From nobody Sat Apr 27 15:05:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1529928410239391.2107756831125; Mon, 25 Jun 2018 05:06:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 69BB8C049D40; Mon, 25 Jun 2018 12:06:48 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 540A878DE3; Mon, 25 Jun 2018 12:06:48 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 459BB18037EC; Mon, 25 Jun 2018 12:06:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w5PC6khe022370 for ; Mon, 25 Jun 2018 08:06:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id DCB2E7C5F6; Mon, 25 Jun 2018 12:06:46 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D20997C5EC for ; Mon, 25 Jun 2018 12:06:43 +0000 (UTC) Received: from mail-pg0-f47.google.com (mail-pg0-f47.google.com [74.125.83.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EBAAB2027A for ; Mon, 25 Jun 2018 12:06:31 +0000 (UTC) Received: by mail-pg0-f47.google.com with SMTP id w8-v6so5966303pgp.7 for ; Mon, 25 Jun 2018 05:06:31 -0700 (PDT) Received: from localhost.localdomain ([14.141.249.170]) by smtp.gmail.com with ESMTPSA id k13-v6sm28348451pfg.130.2018.06.25.05.06.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 25 Jun 2018 05:06:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=9Mw43d/N4TaZ6pSIh8xKGW7pYdpT2trQIVqx3+yuO8o=; b=kHwyb9N5rdvLSo+5rPrqqA3ytpdI1xdxYfyg6Pzl28n3MPPHow1YnOtxv/C1TvfxxM ynol9F5Rk6IFeFGDg74XjSNeFdrZ1edqk0QilU0OuOtND5/dkGBPL3mZxRKjaqjwfBc6 xvdEOfEnihUO3PuwqGjXWySxh8fgCQ7NDljRD4sNqChIQrRYJj+p38z7WCi9UL5Lkhdq qK0FtQmQiWb/r8BVYzpglapK/dLLqb84ek724vVF9FQVI90equDd/QxGgrT6mV8svCYp JfOMD1Yf6c9Elwcksc4aBeg9D3hKV2MSZPmCxJkgnjXziWR1n8GcCNQ7yto0cu10Da6U PN3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=9Mw43d/N4TaZ6pSIh8xKGW7pYdpT2trQIVqx3+yuO8o=; b=TirUsAR4prNZETAXCRSrvgnNqnOy4/H9pvUZeT1eUjACQ5mmxqpvXb34bMLUS9w84l wNtPbI4jOyCxdpp54ZiNUa7DQHC6qPdlDor+hwjOI3wxIpEQDTATqyub+bqB1eIeA9qt Qfx66pqHRHXTHIjpaplk0A2EocSvLT70Ldl4bua9MRTMrbhAPkPL2mnOpXEAbeW53w6r 5FzeEHFmINdUkWQh6haTLhW7cQpeRgdB2DayT8HRCMGIDW76bkndh8HpAo9KxB4UemYi e7tobJ4VmH/02/aLZMSLlBhAuuGEqc5GpjlZsc64VtyNqq/oK3qYYe2R53iiwEjj7HJF sKoQ== X-Gm-Message-State: APt69E0/MFFBxljZOCFTehGo+gz12C50EKUIIDWyjb24raC17w/IDOpj bXlBprw4NUxHaVTBWOU8amwumA== X-Google-Smtp-Source: ADUXVKIIXq8nYSETjqSBv10NQR+ZvdvmmBX28e03fFuEboz70im31bnEIlcTQdWZQuABqCzy9WJ+5Q== X-Received: by 2002:a62:a104:: with SMTP id b4-v6mr12883123pff.159.1529928390842; Mon, 25 Jun 2018 05:06:30 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Mon, 25 Jun 2018 17:36:17 +0530 Message-Id: <20180625120617.15821-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 25 Jun 2018 12:06:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 25 Jun 2018 12:06:32 +0000 (UTC) for IP:'74.125.83.47' DOMAIN:'mail-pg0-f47.google.com' HELO:'mail-pg0-f47.google.com' FROM:'shubhamjain7495@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.55 * (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS, T_DKIM_INVALID) 74.125.83.47 mail-pg0-f47.google.com 74.125.83.47 mail-pg0-f47.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH] Make Patchew cli use rest api X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 25 Jun 2018 12:06:48 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" - Disabled CSRF authentication until OAuth authentication, since DRF leads = to csrf error while handling sessionauthentication. - Added general rest_api function to make request to apis and get response. - Make project functions use the rest_api_do instead of api_do --- api/rest.py | 9 +++++++- patchew-cli | 69 +++++++++++++++++++++++++++++++++++++++++++++++----------= ---- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/api/rest.py b/api/rest.py index 084dbbc..a584578 100644 --- a/api/rest.py +++ b/api/rest.py @@ -25,6 +25,12 @@ from rest_framework.response import Response import rest_framework from mbox import addr_db_to_rest, MboxMessage from rest_framework.parsers import JSONParser, BaseParser +from rest_framework.authentication import SessionAuthentication + +class CsrfExemptSessionAuthentication(SessionAuthentication): + + def enforce_csrf(self, request): + return # To not perform the csrf check previously happening =20 SEARCH_PARAM =3D 'q' =20 @@ -140,7 +146,8 @@ class ProjectsViewSet(viewsets.ModelViewSet): queryset =3D Project.objects.all().order_by('id') serializer_class =3D ProjectSerializer permission_classes =3D (PatchewPermission,) - + authentication_classes =3D (CsrfExemptSessionAuthentication, ) + =20 @action(methods=3D['post'], detail=3DTrue, permission_classes=3D[Impor= tPermission]) def update_project_head(self, request, pk=3DNone): """ diff --git a/patchew-cli b/patchew-cli index 174d1e6..cd5b3fa 100755 --- a/patchew-cli +++ b/patchew-cli @@ -61,7 +61,7 @@ class SubCommand(object): help =3D "" want_argv =3D False # Whether the command accepts extra arguments =20 - def api_do(self, cmd, **data): + def api_do(self, cmd, **data): =20 """Do server api call""" logging.debug("API call '%s':" % cmd) logging.debug("data:\n%s" % data) @@ -92,6 +92,39 @@ class SubCommand(object): r =3D None return r =20 + def rest_api_do(self, url_cmd, request_method=3D'get', content_type=3D= None, **data): + cmd =3D url_cmd + logging.debug("API call '%s':" % url_cmd) + logging.debug("data:\n%s" % data) + cookie =3D http.cookiejar.MozillaCookieJar(COOKIE_FILENAME) + try: + cookie.load() + except IOError: + pass + except http.cookiejar.LoadError: + print("Error while loading cookie", COOKIE_FILENAME) + pass + handler =3D urllib.request.HTTPCookieProcessor(cookie) + opener =3D urllib.request.build_opener(handler) + url =3D self.base_url + '/api/v1/' + url_cmd + '/' + if data: + post_data =3D json.dumps(data) + else: + post_data =3D "" + req =3D urllib.request.Request(url, data=3Dbytes(post_data, encodi= ng=3D"utf-8")) + if content_type is not None: + req.add_header('Content-Type', content_type) + req.get_method =3D lambda: request_method.upper() + resp =3D opener.open(req) + cookie.save(ignore_discard=3DTrue, ignore_expires=3DTrue) + respdata =3D resp.read() + logging.debug("Server response:\n%s" % (respdata or "")) + if respdata: + r =3D json.loads(respdata.decode("utf-8")) + else: + r =3D None + return r + def do(self, args, argv): """Do command""" print("Not implemented") @@ -221,11 +254,11 @@ class ProjectCommand(SubCommand): parser.add_argument("--verbose", "-v", action=3D"store_true", help=3D"Show details about projects") args =3D parser.parse_args(argv) - r =3D self.api_do("get-projects") + r =3D self.rest_api_do(url_cmd=3D"projects", request_method=3D'get= ') if args.raw: print(json.dumps(r, indent=3D2, separators=3D",:")) return 0 - for p in r: + for p in r['results']: print(p["name"]) if args.verbose: for k, v in p.items(): @@ -239,8 +272,8 @@ class ProjectCommand(SubCommand): parser.add_argument("name", nargs=3D"+", help=3D"The name of project to show info") args =3D parser.parse_args(argv) - r =3D self.api_do("get-projects") - for p in r: + r =3D self.rest_api_do(url_cmd=3D"projects", request_method=3D'get= ') + for p in r['results']: if not p["name"] in args.name: continue if len(args.name) > 1: @@ -263,12 +296,15 @@ class ProjectCommand(SubCommand): parser.add_argument("--desc", "-d", default=3D"", help=3D"Project short discription") args =3D parser.parse_args(argv) - self.api_do("add-project", - name=3Dargs.name, - mailing_list=3Dargs.mailing_list, - url=3Dargs.url, - git=3Dargs.git, - description=3Dargs.desc) + + self.rest_api_do(url_cmd=3D"projects", + request_method=3D'post', + content_type=3D'application/json', + name=3Dargs.name, + mailing_list=3Dargs.mailing_list, + url=3Dargs.url, + git=3Dargs.git, + description=3Dargs.desc) =20 def update_one_project(self, wd, project): logging.info("Updating project '%s'", project["name"]) @@ -320,16 +356,17 @@ class ProjectCommand(SubCommand): except Exception as e: logging.warn("Failed to push the new head to patchew mirro= r: %s", str(e)) - self.api_do("update-project-head", project=3Dproject["name"], - old_head=3Dold_head, - new_head=3Dnew_head, - message_ids=3Dmsgids) + url =3D "project/" + project.id + "/update_project_head/" + self.rest_api_do("project-head",old_head=3Dold_head, + new_head=3Dnew_head, + message_ids=3Dmsgids) =20 def update_project(self, argv): parser =3D argparse.ArgumentParser() parser.add_argument("--name", "-n", help=3D"Name of the project") args =3D parser.parse_args(argv) - projects =3D self.api_do("get-projects", name=3Dargs.name) + projects =3D self.rest_api_do("projects") + projects =3D [p for p in projects['results'] if p['name'] =3D=3D a= rgs.name] wd =3D tempfile.mkdtemp() logging.debug("TMPDIR: %s", wd) try: --=20 2.15.1 (Apple Git-101) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel