From nobody Wed Apr 24 04:35:20 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 1525807970496103.48653525525981; Tue, 8 May 2018 12:32:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 89BC6883AE; Tue, 8 May 2018 19:32:49 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7788F5D9C8; Tue, 8 May 2018 19:32:49 +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 69F3E4CA96; Tue, 8 May 2018 19:32:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w48JWmsi017088 for ; Tue, 8 May 2018 15:32:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 160ED608F4; Tue, 8 May 2018 19:32:48 +0000 (UTC) Received: from mx1.redhat.com (ext-mx14.extmail.prod.ext.phx2.redhat.com [10.5.110.43]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0DACB608F0 for ; Tue, 8 May 2018 19:32:43 +0000 (UTC) Received: from mail-pf0-f177.google.com (mail-pf0-f177.google.com [209.85.192.177]) (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 2F59330B79EF for ; Tue, 8 May 2018 19:32:32 +0000 (UTC) Received: by mail-pf0-f177.google.com with SMTP id e9so20432102pfi.4 for ; Tue, 08 May 2018 12:32:32 -0700 (PDT) Received: from localhost.localdomain ([112.196.135.149]) by smtp.gmail.com with ESMTPSA id d19sm62749993pfk.59.2018.05.08.12.32.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 12:32: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:in-reply-to:references; bh=7556FTf7xN2hEcfagBsxU1YwNIJXNUr4GeQNokJMGUM=; b=WS+GmK9+U1xonkvooTbS95zOtlGKl4dq0C4mCEJuwBZbW9OnNqTMSlsnXe+PB/CFLG XK6FRAFrVVPF81jAaWv719DF9foXF28AsCKhQ6aXiIYV1CwF1gdaQ0enEFF/PoSQot6N SU1fXVmF26/GbDzniqXBxVXaYA1cXd+KD996drHMNWbx40kSYaGvjncc0b96rLHJdM7C tA5O9P6/EGqXppLqHo7SOngs5dYf7XF4hqL492jPbHNv8tQKJP4bZ2bfBh7o20sPfq1W QR9uu8kTysfVQDhkunVTD2RhVHYb5+kxAayp1yDODjs5uJWnl7w1nM8iDEExlco2BO6l lEkA== 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:in-reply-to :references; bh=7556FTf7xN2hEcfagBsxU1YwNIJXNUr4GeQNokJMGUM=; b=Eu4SJtrPTiIfp4WmhArVmJB1jqYlqcLTpyEMsK3SmCSxQ84DGaZzQJ6zl+nazPiG9d aeQgOJtlhRju/QJnl8at5S4uZe5KMmPbFosCrI7Yifpe9JCSgTCJtODJFvdnWfHUa8Nk L96elRTa6pxfQlSf8q6Wwqig9yDid9NGQTLd0MNGvXZHRg/yfae2suWovXBV88F81XM9 PDFgjXc86ek04hmSmt0CjC+igS328H0KOsS68Ii9fXTF4xgOC7uod6xlwJhYaamGNQHS F6SO3i4u4u6+YQGoJgSFThtwyX1DmI5bQSpICC+siGHXL74lpH+c726oMsQOavFGnA0k IM8A== X-Gm-Message-State: ALQs6tBNysMou4QR9gFIqhnz80uXah0BfRJXD66jhEQ8B+gJ95RCoQuV qMHlvc60XozNkP88dFhnfDxyeA== X-Google-Smtp-Source: AB8JxZqq/D+dLFlrtdqrZGPsbY11KRt65CM798inskEGFp05H/Y91bazF+zgNzvmHA8uFHlRwzrsVw== X-Received: by 10.98.98.194 with SMTP id w185mr35232637pfb.78.1525807951069; Tue, 08 May 2018 12:32:31 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Wed, 9 May 2018 01:02:06 +0530 Message-Id: <20180508193207.7663-2-shubhamjain7495@gmail.com> In-Reply-To: <20180508193207.7663-1-shubhamjain7495@gmail.com> References: <20180508193207.7663-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Tue, 08 May 2018 19:32:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Tue, 08 May 2018 19:32:32 +0000 (UTC) for IP:'209.85.192.177' DOMAIN:'mail-pf0-f177.google.com' HELO:'mail-pf0-f177.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) 209.85.192.177 mail-pf0-f177.google.com 209.85.192.177 mail-pf0-f177.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.43 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH 1/2] Text to json converter 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.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 08 May 2018 19:32:49 +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. - Add test for text to json converter. - Modified to_representation of AddressSerializer so that it use add_db_to_= rest to avoid redundancy For recepients and sender while converting to json couldn't use AddressSeri= alizer as api.rest calls models which call back mbox. Use add_db_to_rest in= stead. --- api/rest.py | 6 ++---- mbox.py | 18 ++++++++++++++++++ tests/test_mbox.py | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/api/rest.py b/api/rest.py index ed40a10..06faf57 100644 --- a/api/rest.py +++ b/api/rest.py @@ -23,6 +23,7 @@ from rest_framework.fields import SerializerMethodField, = CharField, JSONField, E from rest_framework.relations import HyperlinkedIdentityField from rest_framework.response import Response import rest_framework +from mbox import addr_db_to_rest =20 SEARCH_PARAM =3D 'q' =20 @@ -123,10 +124,7 @@ class AddressSerializer(serializers.Serializer): address =3D EmailField() =20 def to_representation(self, obj): - if obj[0] !=3D obj[1]: - return {"name": obj[0], "address": obj[1]} - else: - return {"address": obj[1]} + return addr_db_to_rest(obj) =20 def create(self, validated_data): try: diff --git a/mbox.py b/mbox.py index fe108f3..35b2270 100644 --- a/mbox.py +++ b/mbox.py @@ -13,6 +13,7 @@ import email.utils import email.header import datetime import re +from rest_framework.fields import DateTimeField =20 def _parse_header(header): r =3D '' @@ -34,6 +35,11 @@ def _addr_fmt_text(name, addr): else: return addr =20 +def addr_db_to_rest(obj): + if obj[0] !=3D obj[1]: + return {"name": obj[0], "address": obj[1]} + else: + return {"address": obj[1]} =20 class MboxMessage(object): """ Helper class to process mbox """ @@ -269,3 +275,15 @@ 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 DateTimeField().to_representation(self.get_date()) + msg['subject'] =3D self.get_subject() + msg['sender'] =3D addr_db_to_rest(self.get_from()) + msg['recipients'] =3D [addr_db_to_rest(x) for x in (self.get_to() = + self.get_cc())] + msg['mbox'] =3D self.get_mbox() + return msg \ No newline at end of file diff --git a/tests/test_mbox.py b/tests/test_mbox.py index 8493df7..b63608d 100755 --- a/tests/test_mbox.py +++ b/tests/test_mbox.py @@ -52,5 +52,24 @@ Virtualization: qemu.org | libvirt.org msg =3D mbox.MboxMessage(f.read()) self.assertTrue(msg.is_patch()) =20 + def test_get_json(self): + dp =3D self.get_data_path("0001-simple-patch.mbox.gz") + with open(dp, "r") as f: + content =3D f.read() + expected =3D {'message_id': '20160628014747.20971-1-famz@redha= t.com', + 'in_reply_to': '', + 'date': '2016-06-28T01:47:47', + 'subject': '[Qemu-devel] [PATCH] quorum: Only comp= ile when supported', + 'sender': {'name': 'Fam Zheng', 'address': 'famz@r= edhat.com'}, + 'recipients': [{'address': 'qemu-devel@nongnu.org'= }, + {'name': 'Kevin Wolf', 'address': '= kwolf@redhat.com'}, + {'name': 'Alberto Garcia', 'address= ': 'berto@igalia.com'}, + {'address': 'qemu-block@nongnu.org'= }, + {'name': 'Max Reitz', 'address': 'm= reitz@redhat.com'}], + 'mbox':content + } + msg =3D mbox.MboxMessage(content).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 From nobody Wed Apr 24 04:35:21 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 1525807974592616.4217010067574; Tue, 8 May 2018 12:32:54 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C59F61BB4; Tue, 8 May 2018 19:32:53 +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 8CFC2600CC; Tue, 8 May 2018 19:32:53 +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 81607180BADC; Tue, 8 May 2018 19:32:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w48JWpZ2017124 for ; Tue, 8 May 2018 15:32:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id EC2655D9CC; Tue, 8 May 2018 19:32:51 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E3D035D9CA for ; Tue, 8 May 2018 19:32:47 +0000 (UTC) Received: from mail-pl0-f68.google.com (mail-pl0-f68.google.com [209.85.160.68]) (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 80E1480C29 for ; Tue, 8 May 2018 19:32:36 +0000 (UTC) Received: by mail-pl0-f68.google.com with SMTP id az12-v6so2803979plb.8 for ; Tue, 08 May 2018 12:32:36 -0700 (PDT) Received: from localhost.localdomain ([112.196.135.149]) by smtp.gmail.com with ESMTPSA id d19sm62749993pfk.59.2018.05.08.12.32.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 12:32:34 -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:in-reply-to:references; bh=tSi8kRt8vLRtbeltawAP7j3GJadC384Kzbl2JwDy2Ww=; b=SPzQfhvcg6nrtPyo2e+vYd7nYcYVPhnM02OZb6adLkuKBNfroiCX81e+C6narVaRY0 P5jdRpFOFhNUTJTHd0s24P3etg1XV4o5Vz9rKL4thUOrcUcdRoazS3aad59u18QvQ4rL 4GJ6obBpHUb/62yojfeSrx6+2f3fA403qhH7JI2WJDWb6DFFhJlhu+AK4TcFQ1Mi7eYm ZYo2sI0K/04bRTQDuTr91nhrwp6lx4PqKOYBIONLBMcT1SIGQClKd49Vj8typ/SE5UwA 9nyknxo5nWLiJAGdpk8chooDZg5Pnag1Xg8mrFZ6nD+0taGiz0LvD/pNX4/tKkIpqIsT dscg== 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:in-reply-to :references; bh=tSi8kRt8vLRtbeltawAP7j3GJadC384Kzbl2JwDy2Ww=; b=oVODa7/RSgftCO33pkXXs6l3gyfexYLL+1UBYl1QV9PYLsH06UITcJRa1iNfqdurJ3 ViBv0NHgCdfS4L9vb84MhW1RbPJz/N2wtxRDknBvwagGlLqR64KH16MF0ekFl1axcX/P CtOGAQzLyo4+vaPUgVEaGCu9tFovpslJj4W2IKpGz6jGELC6UbmC3hp26OiT3Ch3xNyr RIHjzUkINgX4COACWyA9UqpIipSAvzF7rzffQhasnxGvyEf6MZ7832hQE2kNJj9puShx 215vtUjkNYusAHPLS/RaMH5l9OtLef9kokpxip2FYcU5AuGOSKCAX6DBvNw+5ApDnwAi Cp8Q== X-Gm-Message-State: ALQs6tAm3JapAIgipNrdSkU5eWPkJrxJcHigpAz4xvytHFTNaZGTSoIn QVmkN9pGt5NaZnylAWxLywhqvw== X-Google-Smtp-Source: AB8JxZrNQ43V518vwN0Tpvc8LHg3NPSDexa/IULnWYmtj7wzfhK4rgMl1edxh28IZXCU07FSb1419g== X-Received: by 2002:a17:902:564:: with SMTP id 91-v6mr42310427plf.63.1525807955456; Tue, 08 May 2018 12:32:35 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Wed, 9 May 2018 01:02:07 +0530 Message-Id: <20180508193207.7663-3-shubhamjain7495@gmail.com> In-Reply-To: <20180508193207.7663-1-shubhamjain7495@gmail.com> References: <20180508193207.7663-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 08 May 2018 19:32:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 08 May 2018 19:32:36 +0000 (UTC) for IP:'209.85.160.68' DOMAIN:'mail-pl0-f68.google.com' HELO:'mail-pl0-f68.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) 209.85.160.68 mail-pl0-f68.google.com 209.85.160.68 mail-pl0-f68.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH 2/2] rest : POST for message api endpoint 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 08 May 2018 19:32:53 +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" Add text/plain support to ProjectMessageSerializer's create (POST) action a= nd add corresponding test to it. --- api/rest.py | 13 ++++++++++++- tests/test_rest.py | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/api/rest.py b/api/rest.py index 06faf57..4bd064d 100644 --- a/api/rest.py +++ b/api/rest.py @@ -23,7 +23,8 @@ from rest_framework.fields import SerializerMethodField, = CharField, JSONField, E from rest_framework.relations import HyperlinkedIdentityField from rest_framework.response import Response import rest_framework -from mbox import addr_db_to_rest +from mbox import addr_db_to_rest, MboxMessage +from rest_framework.parsers import JSONParser, BaseParser =20 SEARCH_PARAM =3D 'q' =20 @@ -316,9 +317,19 @@ class StaticTextRenderer(renderers.BaseRenderer): else: return data =20 +class MessagePlainTextParser(BaseParser): + media_type =3D 'text/plain' + + def parse(self, stream, media_type=3DNone, parser_context=3DNone): + =20 + data =3D stream.read().decode("utf-8") + return MboxMessage(data).get_json() + class MessagesViewSet(ProjectMessagesViewSetMixin, BaseMessageViewSet, mixins.CreateModelMixin): serializer_class =3D MessageSerializer + parser_classes =3D (JSONParser, MessagePlainTextParser, ) + @detail_route(renderer_classes=3D[StaticTextRenderer]) def mbox(self, request, *args, **kwargs): message =3D self.get_object() diff --git a/tests/test_rest.py b/tests/test_rest.py index 3baadd5..1911800 100755 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -268,6 +268,15 @@ class RestTest(PatchewTestCase): self.assertEqual(resp.data['subject'], "[Qemu-devel] [PATCH v2 10/= 27] imx_fec: Reserve full 4K " "page for the register file") =20 + def test_create_text_message(self): + dp =3D self.get_data_path("0004-multiple-patch-reviewed.mbox.gz") + with open(dp, "r") as f: + data =3D f.read() + resp =3D self.api_client.post(self.PROJECT_BASE + "messages/", dat= a, content_type=3D'text/plain') + self.assertEqual(resp.status_code, 201) + resp_get =3D self.api_client.get(self.PROJECT_BASE + "messages/146= 9192015-16487-1-git-send-email-berrange@redhat.com/") + self.assertEqual(resp_get.status_code, 200) + self.assertEqual(resp.data['subject'], "[Qemu-devel] [PATCH v4 0/2= ] Report format specific info for LUKS block driver") =20 def test_message(self): series =3D self.apply_and_retrieve('0001-simple-patch.mbox.gz', --=20 2.14.3 (Apple Git-98) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel