[Patchew-devel] [PATCH v2 3/3] Make Import Command use Rest

Shubham Jain shubhamjain7495 at gmail.com
Fri Jun 29 14:05:51 UTC 2018


Make patchew-cli's Import user REST apis. Changed the exit status of authenticated user from cli test while importing since in REST every authenticated user can access the import api but messages would be imported only to the projects recognised ie whether it imported by maintainer or importer. Also added warning if message is not imported to any project.
---
 api/rest.py          |  3 ++-
 patchew-cli          | 18 +++++++++++++-----
 tests/test_import.py |  8 ++++----
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/api/rest.py b/api/rest.py
index 9b47f37..876be75 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -423,7 +423,8 @@ class MessagesViewSet(BaseMessageViewSet):
     serializer_class = MessageSerializer
     permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
     parser_classes = (JSONParser, MessagePlainTextParser, )
-    
+    authentication_classes = (CsrfExemptSessionAuthentication, )
+
     def create(self, request, *args, **kwargs):
         m = MboxMessage(request.data['mbox'])
         projects = [p for p in Project.objects.all() if p.recognizes(m)]
diff --git a/patchew-cli b/patchew-cli
index f90f3c5..3796caf 100755
--- a/patchew-cli
+++ b/patchew-cli
@@ -220,11 +220,18 @@ class ImportCommand(SubCommand):
                     print("[OLD] " + mo["Subject"])
                     return
             print("[NEW] " + mo["Subject"])
-            r = self.api_do("import", mboxes=[mo.as_string()])
-            for p in r:
-                if p not in projects:
-                    projects.add(p)
-                    print(p)
+            for mbox in [mo.as_string()]:
+                r = self.rest_api_do(url_cmd="messages",
+                                     request_method='post',
+                                     content_type='message/rfc822',
+                                     data=mbox)
+                projects_list = [x['resource_uri'].split("messages")[0] for x in r['results']]
+                for p in projects_list:
+                    if p not in projects:
+                        projects.add(p)
+                        print(p)
+                if len(projects_list)==0:
+                    print("The message was not imported to any project. Perhaps you're not logged in as an importer or maintainer")
             if ff:
                 open(ff, "wb").close()
 
@@ -251,6 +258,7 @@ class ImportCommand(SubCommand):
             try:
                 import_one(f)
             except:
+                raise
                 print("Error in importing:", f)
                 traceback.print_exc()
                 r = 1
diff --git a/tests/test_import.py b/tests/test_import.py
index 5693d7e..250f391 100755
--- a/tests/test_import.py
+++ b/tests/test_import.py
@@ -107,21 +107,21 @@ class UnprivilegedImportTest(ImportTest):
 
     test_import_belong_to_multiple_projects = None
 
-    def check_import_should_fail(self):
-        self.cli_import("0001-simple-patch.mbox.gz", 1)
+    def check_import_status(self, exit_status):
+        self.cli_import("0001-simple-patch.mbox.gz", exit_status)
         a, b = self.check_cli(["search"])
         self.assertNotIn('[Qemu-devel] [PATCH] quorum: Only compile when supported',
                          a.splitlines())
 
     def test_anonymous_import(self):
         self.cli_logout()
-        self.check_import_should_fail()
+        self.check_import_status(exit_status=1)
 
     def test_normal_user_import(self):
         self.cli_logout()
         self.create_user("someuser", "somepass")
         self.cli_login("someuser", "somepass")
-        self.check_import_should_fail()
+        self.check_import_status(exit_status=0)
 
     def test_project_update(self):
         p = Project.objects.all()[0]
-- 
2.15.1 (Apple Git-101)




More information about the Patchew-devel mailing list