From nobody Sat Apr 27 15:55:18 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 1524830493966666.754599181033; Fri, 27 Apr 2018 05:01:33 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D5D4A356CB; Fri, 27 Apr 2018 12:01:32 +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 C4F1431327A5; Fri, 27 Apr 2018 12:01:32 +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 B23CD1808870; Fri, 27 Apr 2018 12:01:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3RBsjuZ031270 for ; Fri, 27 Apr 2018 07:54:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5FC841001913; Fri, 27 Apr 2018 11:54:45 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 593E31001911 for ; Fri, 27 Apr 2018 11:54:42 +0000 (UTC) Received: from mail-pg0-f51.google.com (mail-pg0-f51.google.com [74.125.83.51]) (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 0631F356CB for ; Fri, 27 Apr 2018 11:54:31 +0000 (UTC) Received: by mail-pg0-f51.google.com with SMTP id i6-v6so1414598pgv.3 for ; Fri, 27 Apr 2018 04:54:30 -0700 (PDT) Received: from localhost.localdomain ([112.196.135.81]) by smtp.gmail.com with ESMTPSA id d8sm2732325pfh.177.2018.04.27.04.54.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Apr 2018 04:54:28 -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=RIK14/Ektwd+fWJ0Y8iYOC5eQV1ZIB8drNw+vn2npx4=; b=BqqpqfODpvwlBs1Bt1sU7Df1ab5NEtzb0djsHT8CCDkUnQU7YONKxozw9pbce6p9K7 0AVgjSliWMZecjKg0uMifchJeNZBs0alxqZjcFD8vwUOPyFJOZrjgkNL5YeFoguruao3 NpZCR5UflS5gfyIdbWaiAnJ0EISI2qV0ZU+5OlMlF9uPhiKDj+f+uU/oVJsqkDJkTLcz eLMOlTShIOUp+Lwe3pBXQT0Y1t6dFWebLjMSe1KHcfr5fQFS1JYImQSZUmGRPATvJqXG y3h4GaWBKfmCqgG4WvKUUAsmgCR4UMtfFUWf2C+iPKbt75vAPZg0Zb5qvlcSpY66h2gB thaw== 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=RIK14/Ektwd+fWJ0Y8iYOC5eQV1ZIB8drNw+vn2npx4=; b=M0Bw7C9yT5oDhp2sHah9ZsePYtZYugXIGLilo1J7ERL46qpMJdzkfjXgLopoifoMTu 9wToolhnXHWILCnaiawpA6uu0Tgy2h+ugl9q+XGwPNy0KcpyW9OE61DOhyQE8DM9a2qM hjZqGPYJoWbIA7lSwSWugAQlAtDOn8j9wGi03En6r84jas7W0qMgNmq3ZD5EjUES+wwK EJj6BiA1VuTwbZdmM7n8op2ZRzehfUXy1EypkLtp9JlxnwaM8AMrutnVYQ4ztmIml6rW pmfM+3hKhrUpvx80UWnQBKCBrQXyReWRidylhstJJ2kwcUiJep2i9Rqv8rqhc8Am7aEf jP0w== X-Gm-Message-State: ALQs6tAr/bS5h/aH3W/x91S7DavA4SJWEFn7t+UjH13rdgns/oIkPL2g c4y7UzIfuwvQilr8u27Igani6w== X-Google-Smtp-Source: AB8JxZqIosklaUxcSbpkAxbG8zYd72b+3QNyAQmDSYroHmPoeN+FOHbhhZ8ZJY0vsz3OTX5i+T0jOg== X-Received: by 2002:a17:902:6c4b:: with SMTP id h11-v6mr2113344pln.33.1524830069744; Fri, 27 Apr 2018 04:54:29 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Fri, 27 Apr 2018 17:23:54 +0530 Message-Id: <20180427115354.38587-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 27 Apr 2018 11:54:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 27 Apr 2018 11:54:31 +0000 (UTC) for IP:'74.125.83.51' DOMAIN:'mail-pg0-f51.google.com' HELO:'mail-pg0-f51.google.com' FROM:'shubhamjain7495@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.531 * (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_DKIM_INVALID) 74.125.83.51 mail-pg0-f51.google.com 74.125.83.51 mail-pg0-f51.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH v2] Text to json convertor for mbox 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.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 27 Apr 2018 12:01:32 +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" - Mbox would now return a dictionary object which would help REST API deser= ialize easily into model object. [WIP] POST endpoint for messages. - It's a work in progress for creating new message which is improvement of = legacy endpoint: "import" Message-Id: <20180424160400.29938-1-shubhamjain7495@gmail.com> POST endpoint for messages - Added test for text to json converter of Mbox - Removed extra field from the dictionary object --- api/rest.py | 21 +++++++++++---------- mbox.py | 18 ++++++++++++++++++ tests/test_mbox.py | 19 +++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/api/rest.py b/api/rest.py index 381b569..917ed22 100644 --- a/api/rest.py +++ b/api/rest.py @@ -125,8 +125,8 @@ class BaseMessageSerializer(serializers.ModelSerializer= ): =20 resource_uri =3D HyperlinkedMessageField(view_name=3D'messages-detail') =20 - recipients =3D SerializerMethodField() - sender =3D SerializerMethodField() + # recipients =3D SerializerMethodField() + # sender =3D SerializerMethodField() =20 def format_name_addr(self, name, addr): d =3D {} @@ -135,12 +135,12 @@ class BaseMessageSerializer(serializers.ModelSerializ= er): d['address'] =3D addr return d =20 - def get_recipients(self, obj): - return [self.format_name_addr(*x) for x in obj.get_recipients()] + # def get_recipients(self, obj): + # return [self.format_name_addr(*x) for x in obj.get_recipients()] =20 - def get_sender(self, obj): - name, addr =3D obj.get_sender() - return self.format_name_addr(*obj.get_sender()) + # def get_sender(self, obj): + # name, addr =3D obj.get_sender() + # return self.format_name_addr(*obj.get_sender()) =20 # a message_id is *not* unique, so we can only list class BaseMessageViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): @@ -285,7 +285,8 @@ class ProjectSeriesViewSet(ProjectMessagesViewSetMixin, class MessageSerializer(BaseMessageSerializer): class Meta: model =3D Message - fields =3D BaseMessageSerializer.Meta.fields + ('mbox', ) + # fields =3D BaseMessageSerializer.Meta.fields + ('mbox', ) + fields =3D '__all__' =20 def get_mbox(self, obj): return obj.get_mbox() @@ -311,9 +312,9 @@ class StaticTextRenderer(renderers.BaseRenderer): return data =20 class MessagesViewSet(ProjectMessagesViewSetMixin, - BaseMessageViewSet): + BaseMessageViewSet, mixins.CreateModelMixin): serializer_class =3D MessageSerializer - + =20 @detail_route(renderer_classes=3D[StaticTextRenderer]) def mbox(self, request, *args, **kwargs): message =3D self.get_object() diff --git a/mbox.py b/mbox.py index fe108f3..00336ba 100644 --- a/mbox.py +++ b/mbox.py @@ -13,6 +13,7 @@ import email.utils import email.header import datetime import re +import json =20 def _parse_header(header): r =3D '' @@ -269,3 +270,20 @@ class MboxMessage(object): if c =3D=3D 0: return True return False + + def get_json(self): + """Return the JSON format of the mbox """ + msg =3D {} + msg['message_id'] =3D self.get_message_id() + msg['in_reply_to'] =3D self.get_in_reply_to() or "" + msg['date'] =3D self.get_date() + msg['subject'] =3D self.get_subject() + msg['stripped_subject'] =3D self.get_subject(strip_tags=3DTrue) + msg['version'] =3D self.get_version() + msg['sender'] =3D json.dumps(self.get_from()) + msg['recipients'] =3D json.dumps(self.get_to() + self.get_cc()) + msg['prefixes']=3Djson.dumps(self.get_prefixes()) + msg['is_series_head'] =3D self.is_series_head() + msg['is_patch'] =3D self.is_patch() + msg['patch_num'] =3D self.get_num()[0] + return msg diff --git a/tests/test_mbox.py b/tests/test_mbox.py index 8493df7..e4d69d2 100755 --- a/tests/test_mbox.py +++ b/tests/test_mbox.py @@ -13,6 +13,7 @@ import sys import mbox sys.path.append(os.path.dirname(__file__)) from patchewtest import PatchewTestCase, main +import datetime =20 class MboxTest(PatchewTestCase): =20 @@ -52,5 +53,23 @@ Virtualization: qemu.org | libvirt.org msg =3D mbox.MboxMessage(f.read()) self.assertTrue(msg.is_patch()) =20 + def test_get_json(self): + expected =3D {'message_id': '20160628014747.20971-1-famz@redhat.co= m',=20 + 'in_reply_to': '',=20 + 'date': datetime.datetime(2016, 6, 28, 1, 47, 47),=20 + 'subject': '[Qemu-devel] [PATCH] quorum: Only compile = when supported',=20 + 'stripped_subject': 'quorum: Only compile when support= ed',=20 + 'version': 1,=20 + 'sender': '["Fam Zheng", "famz@redhat.com"]',=20 + 'recipients': '[["qemu-devel@nongnu.org", "qemu-devel@= nongnu.org"], ["Kevin Wolf", "kwolf@redhat.com"], ["Alberto Garcia", "berto= @igalia.com"], ["qemu-block@nongnu.org", "qemu-block@nongnu.org"], ["Max Re= itz", "mreitz@redhat.com"]]',=20 + 'prefixes': '["Qemu-devel", "PATCH"]',=20 + 'is_series_head': True,=20 + 'is_patch': True,=20 + 'patch_num': None } + dp =3D self.get_data_path("0001-simple-patch.mbox.gz") + with open(dp, "r") as f: + msg =3D mbox.MboxMessage(f.read()).get_json() + self.assertEqual(msg, expected) + if __name__ =3D=3D '__main__': main() --=20 2.14.3 (Apple Git-98) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel