[Patchew-devel] [PATCH v3 4/4] Make Import Command use Rest

Shubham Jain shubhamjain7495 at gmail.com
Fri Jul 6 09:39:47 UTC 2018


Make patchew-cli's Import REST apis. Only authenticated users would be able to import to recognized projects or the projects maintained by importer. If no projects are imported, exit status as 1 would be returned. Also changed a test case in test_tags accordingly.
---
 api/models.py        |  3 +++
 api/rest.py          |  3 ++-
 patchew-cli          | 18 +++++++++++++-----
 tests/test_import.py |  8 ++++----
 tests/test_tags.py   |  5 ++---
 5 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/api/models.py b/api/models.py
index ebaecde..6c21537 100644
--- a/api/models.py
+++ b/api/models.py
@@ -359,6 +359,9 @@ class MessageManager(models.Manager):
     def create(self, project, **validated_data):
         mbox = validated_data.pop('mbox')
         m = MboxMessage(mbox)
+        msg = Message.objects.filter(message_id=m.get_message_id(), project=project).first()
+        if msg is not None:
+            return msg
         msg = Message(**validated_data)
         if 'in_reply_to' not in validated_data:
             msg.in_reply_to = m.get_in_reply_to() or ""
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..08f85b4 100755
--- a/patchew-cli
+++ b/patchew-cli
@@ -220,11 +220,16 @@ 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 ff:
                 open(ff, "wb").close()
 
@@ -250,6 +255,9 @@ class ImportCommand(SubCommand):
         for f in args.file:
             try:
                 import_one(f)
+                if len(projects)==0:
+                    print("The message was not imported to any project. Perhaps you're not logged in as an importer or maintainer")
+                    r =1
             except:
                 print("Error in importing:", f)
                 traceback.print_exc()
diff --git a/tests/test_import.py b/tests/test_import.py
index 5693d7e..07de3c6 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=1)
 
     def test_project_update(self):
         p = Project.objects.all()[0]
diff --git a/tests/test_tags.py b/tests/test_tags.py
index 6c162de..b4f21ac 100755
--- a/tests/test_tags.py
+++ b/tests/test_tags.py
@@ -22,9 +22,8 @@ class ImportTest(PatchewTestCase):
         self.p.save()
 
     def test_import_one(self):
-        resp = self.apply_and_retrieve('0017-qemu-web-is-not-qemu.mbox.gz',
-                                       self.p.id, '1504250391-6353-1-git-send-email-thuth at redhat.com')
-        self.assertEquals(resp.status_code, 404)
+        self.cli_import("0001-simple-patch.mbox.gz", rc=1)
+        # self.assertEquals(resp.status_code, 404)
 
     def test_rest_single(self):
         resp = self.apply_and_retrieve('0003-single-patch-reviewed.mbox.gz',
-- 
2.15.1 (Apple Git-101)




More information about the Patchew-devel mailing list