Make patchew-cli's Import REST apis. For an authenticated but unauthorised user the exit status would be 1 ie when no projects are imported.
Now importing same message to a project again would not give 404 error.
Also changed a test case in test_tags accordingly.
---
api/models.py | 3 +++
api/rest.py | 1 +
patchew-cli | 18 +++++++++++++-----
tests/test_tags.py | 4 +---
4 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/api/models.py b/api/models.py
index e0c4fa2..b3ba457 100644
--- a/api/models.py
+++ b/api/models.py
@@ -361,6 +361,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 d714d09..a9b6be3 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -423,6 +423,7 @@ 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'])
diff --git a/patchew-cli b/patchew-cli
index cb0b650..f8d6e57 100755
--- a/patchew-cli
+++ b/patchew-cli
@@ -221,11 +221,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()
@@ -251,6 +256,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_tags.py b/tests/test_tags.py
index 6c162de..c91e9c1 100755
--- a/tests/test_tags.py
+++ b/tests/test_tags.py
@@ -22,9 +22,7 @@ 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@redhat.com')
- self.assertEquals(resp.status_code, 404)
+ self.cli_import("0017-qemu-web-is-not-qemu.mbox.gz", rc=1)
def test_rest_single(self):
resp = self.apply_and_retrieve('0003-single-patch-reviewed.mbox.gz',
--
2.15.1 (Apple Git-101)
_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel