[Patchew-devel] [PATCH v3] Text to json convertor for mbox

Shubham Jain shubhamjain7495 at gmail.com
Sat Apr 28 16:20:18 UTC 2018


I noticed two things after sending this.
First, we are returning a dictionary rather than a json object for text to
json converter. Returning a json dump of dictionary gives error of date
(date time object not json serialisable). Though POST can accept dictionary
object (I think), I should change the name to get_dictionary rather than
get_json.

Second, should we change the format of recipients to
[{"name":..,"address":..},..] while extracting recipients from mbox? and
similarly with sender? And the corresponding custom serialiser would like
something like this
recipients = AddressSerializer()
class AddressSerializer(serializers.Field):
    def to_representation(self,obj):
        ret = []
        for item in obj:
            ret.append({"name":item[0],"address":item[1]})
        return ret
    def to_internal_value(self, data):
        return [[x['name'],x['address']] for x in data]


On Sat, Apr 28, 2018 at 3:20 PM, Shubham Jain <shubhamjain7495 at gmail.com>
wrote:

> - Mbox would now return a dictionary object which would help REST API
> deserialize easily into model object.
> - Added test for text to json converter.
>
> [WIP] POST endpoint for messages.
>
> - It's a work in progress for creating new message which is improvement of
> legacy endpoint: "import"
> ---
>  api/rest.py        |   4 +-
>  mbox.py            |  18 +++++++++
>  tests/test_mbox.py | 110 ++++++++++++++++++++++++++++++
> +++++++++++++++++++++++
>  3 files changed, 129 insertions(+), 3 deletions(-)
>
> diff --git a/api/rest.py b/api/rest.py
> index 381b569..cced462 100644
> --- a/api/rest.py
> +++ b/api/rest.py
> @@ -286,7 +286,6 @@ class MessageSerializer(BaseMessageSerializer):
>      class Meta:
>          model = Message
>          fields = BaseMessageSerializer.Meta.fields + ('mbox', )
> -
>      def get_mbox(self, obj):
>          return obj.get_mbox()
>      mbox = SerializerMethodField()
> @@ -311,9 +310,8 @@ class StaticTextRenderer(renderers.BaseRenderer):
>              return data
>
>  class MessagesViewSet(ProjectMessagesViewSetMixin,
> -                      BaseMessageViewSet):
> +                      BaseMessageViewSet, mixins.CreateModelMixin):
>      serializer_class = MessageSerializer
> -
>      @detail_route(renderer_classes=[StaticTextRenderer])
>      def mbox(self, request, *args, **kwargs):
>          message = self.get_object()
> diff --git a/mbox.py b/mbox.py
> index fe108f3..c391fb0 100644
> --- a/mbox.py
> +++ b/mbox.py
> @@ -269,3 +269,21 @@ class MboxMessage(object):
>          if c == 0:
>              return True
>          return False
> +
> +    def get_json(self):
> +        """Return the JSON format of the mbox """
> +        msg = {}
> +        msg['message_id'] = self.get_message_id()
> +        msg['in_reply_to'] = self.get_in_reply_to() or ""
> +        msg['date'] = self.get_date()
> +        msg['subject'] = self.get_subject()
> +        msg['stripped_subject'] = self.get_subject(strip_tags=True)
> +        msg['version'] = self.get_version()
> +        msg['sender'] = self.get_from()
> +        msg['recipients'] = self.get_to() + self.get_cc()
> +        msg['prefixes']= self.get_prefixes()
> +        msg['is_series_head'] = self.is_series_head()
> +        msg['is_patch'] = self.is_patch()
> +        msg['patch_num'] = self.get_num()[0]
> +        msg['mbox'] = self.get_mbox()
> +        return msg
> diff --git a/tests/test_mbox.py b/tests/test_mbox.py
> index 8493df7..5b7f10e 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
>
>  class MboxTest(PatchewTestCase):
>
> @@ -52,5 +53,114 @@ Virtualization:  qemu.org | libvirt.org
>              msg = mbox.MboxMessage(f.read())
>          self.assertTrue(msg.is_patch())
>
> +    def test_get_json(self):
> +        expected = {'message_id': '20160628014747.20971-1-famz at redhat.com',
>
> +                    'in_reply_to': '',
> +                    'date': datetime.datetime(2016, 6, 28, 1, 47, 47),
> +                    'subject': '[Qemu-devel] [PATCH] quorum: Only compile
> when supported',
> +                    'stripped_subject': 'quorum: Only compile when
> supported',
> +                    'version': 1,
> +                    'sender': ('Fam Zheng', 'famz at redhat.com'),
> +                    'recipients': [('qemu-devel at nongnu.org', '
> qemu-devel at nongnu.org'),
> +                                   ('Kevin Wolf', 'kwolf at redhat.com'),
> +                                   ('Alberto Garcia', 'berto at igalia.com
> '),
> +                                   ('qemu-block at nongnu.org', '
> qemu-block at nongnu.org'),
> +                                   ('Max Reitz', 'mreitz at redhat.com')],
> +                    'prefixes': ['Qemu-devel', 'PATCH'],
> +                    'is_series_head': True,
> +                    'is_patch': True,
> +                    'patch_num': None,
> +                    'mbox': 'Delivered-To: importer at patchew.org\nReceived-SPF:
> '
> +                            'Pass (zoho.com: domain of
> qemu-devel-bounces at nongnu.org '
> +                            'designates 208.118.235.17 as permitted
> sender )  client-ip: '
> +                            '208.118.235.17\nReceived-SPF: pass (zoho.com:
> domain of '
> +                            'gnu.org designates 208.118.235.17 as
> permitted sender) '
> +                            'client-ip=208.118.235.17;
> envelope-from=qemu-devel-bounces+'
> +                            'importer=patchew.org at nongnu.org; helo=
> lists.gnu.org;\n'
> +                            'Return-Path: <qemu-devel-bounces+importer=p
> atchew.org at nongnu.org>'
> +                            '\nReceived: from lists.gnu.org (
> lists.gnu.org [208.118.235.17]) '
> +                            'by mx.zohomail.com\n\twith SMTPS id
> 1467078971424862.8927889595075;'
> +                            ' Mon, 27 Jun 2016 18:56:11 -0700
> (PDT)\nReceived: from localhost '
> +                            '([::1]:33689 helo=lists.gnu.org)\n\tby
> lists.gnu.org with esmtp '
> +                            '(Exim 4.71)\n\t(envelope-from
> <qemu-devel-bounces+importer='
> +                            'patchew.org at nongnu.org>)\n\tid
> 1bHi94-0006LP-Ok\n\tfor '
> +                            'importer at patchew.org; Mon, 27 Jun 2016
> 21:48:58 -0400\nReceived: '
> +                            'from eggs.gnu.org
> ([2001:4830:134:3::10]:53270)\n\tby lists.gnu.org'
> +                            ' with esmtp (Exim 4.71)\n\t(envelope-from <
> famz at redhat.com>) id '
> +                            '1bHi8E-0002Lm-KR\n\tfor
> qemu-devel at nongnu.org; Mon, 27 Jun 2016 '
> +                            '21:48:07 -0400\nReceived: from Debian-exim
> by eggs.gnu.org with '
> +                            'spam-scanned (Exim 4.71)\n\t(envelope-from <
> famz at redhat.com>) '
> +                            'id 1bHi8D-0008T4-N7\n\tfor
> qemu-devel at nongnu.org; Mon, 27 Jun '
> +                            '2016 21:48:06 -0400\nReceived: from
> mx1.redhat.com '
> +                            '([209.132.183.28]:47972)\n\tby eggs.gnu.org
> with esmtp '
> +                            '(Exim 4.71)\n\t(envelope-from <
> famz at redhat.com>)\n\tid '
> +                            '1bHi86-0008SN-IZ; Mon, 27 Jun 2016 21:47:58
> -0400\nReceived: '
> +                            'from int-mx10.intmail.prod.int.
> phx2.redhat.com\n\t'
> +                            '(int-mx10.intmail.prod.int.phx2.redhat.com
> [10.5.11.23])\n\t'
> +                            '(using TLSv1.2 with cipher
> ECDHE-RSA-AES256-GCM-SHA384 '
> +                            '(256/256 bits))\n\t(No client certificate
> requested)\n\tby '
> +                            'mx1.redhat.com (Postfix) with ESMTPS id
> BDB007F088;\n\tTue, '
> +                            '28 Jun 2016 01:47:57 +0000 (UTC)\nReceived: '
> +                            'from ad.usersys.redhat.com (
> dhcp-15-133.nay.redhat.com\n\t'
> +                            '[10.66.15.133])\n\tby
> int-mx10.intmail.prod.int.phx2.redhat.com'
> +                            ' (8.14.4/8.14.4) with ESMTP\n\tid
> u5S1lssT024908; Mon, 27 Jun '
> +                            '2016 21:47:55 -0400\nFrom: Fam Zheng <
> famz at redhat.com>\nTo: '
> +                            'qemu-devel at nongnu.org\nDate: Tue, 28 Jun
> 2016 09:47:47 '
> +                            '+0800\nMessage-Id: <
> 20160628014747.20971-1-famz at redhat.com>\n'
> +                            'X-Scanned-By: MIMEDefang 2.68 on
> 10.5.11.23\nX-Greylist: '
> +                            'Sender IP whitelisted, not delayed by
> milter-greylist-4.5.16\n\t'
> +                            '(mx1.redhat.com [10.5.110.26]);\n\tTue, 28
> Jun 2016 01:47:57 '
> +                            '+0000 (UTC)\nX-detected-operating-system:
> by eggs.gnu.org: '
> +                            'GNU/Linux 2.2.x-3.x
> [generic]\nX-Received-From: 209.132.183.28\n'
> +                            'Subject: [Qemu-devel] [PATCH] quorum: Only
> compile when supported\n'
> +                            'X-BeenThere: qemu-devel at nongnu.org\nX-Mailman-Version:
> 2.1.21\n'
> +                            'Precedence: list\nList-Id: <
> qemu-devel.nongnu.org>\nList-Unsubscribe:'
> +                            ' <https://lists.nongnu.org/
> mailman/options/qemu-devel>,\n\t'
> +                            '<mailto:qemu-devel-request at nongnu.org
> ?subject=unsubscribe>\n'
> +                            'List-Archive: <http://lists.nongnu.org/
> archive/html/qemu-devel/>\n'
> +                            'List-Post: <mailto:qemu-devel at nongnu.org>\nList-Help:
> '
> +                            '<mailto:qemu-devel-request at nongnu.org
> ?subject=help>\nList-Subscribe:'
> +                            ' <https://lists.nongnu.org/
> mailman/listinfo/qemu-devel>,\n\t'
> +                            '<mailto:qemu-devel-request at nongnu.org
> ?subject=subscribe>\nCc:'
> +                            ' Kevin Wolf <kwolf at redhat.com>, Alberto
> Garcia <berto at igalia.com>,'
> +                            '\n\tqemu-block at nongnu.org, Max Reitz <
> mreitz at redhat.com>\nErrors-To:'
> +                            ' qemu-devel-bounces+importer=pa
> tchew.org at nongnu.org\nSender: '
> +                            '"Qemu-devel" <qemu-devel-bounces+importer=p
> atchew.org at nongnu.org>\n'
> +                            'X-ZohoMail-Owner: <
> 20160628014747.20971-1-famz at redhat.com>+zmo_0_'
> +                            '<qemu-devel-bounces+importer=
> patchew.org at nongnu.org>\n'
> +                            'X-ZohoMail-Sender:
> 209.132.183.28\nX-ZohoMail: RSF_0  Z_629925259 '
> +                            'SPT_1 Z_629926901 SPT_1  SS_1 SFPD SFPP
> UW2468 UB2468 ZFF-EB_1'
> +                            ' COSF  ODL   SGR3_1_2_0_27046_53\nX-Zoho-Virus-Status:
> 2\n\n'
> +                            'This was the only exceptional module init
> function that does '
> +                            'something\nelse than a simple list of
> bdrv_register() calls, '
> +                            'in all the block\ndrivers.\n\nThe
> qcrypto_hash_supports is actually'
> +                            ' a static check, determined at\ncompile
> time.  Follow the '
> +                            'block-job-$(CONFIG_FOO) convention
> for\nconsistency.\n\n'
> +                            'Signed-off-by: Fam Zheng <famz at redhat.com>\n---\n
> block/'
> +                            'Makefile.objs | 2 +-\n block/quorum.c      |
> 4 ----\n 2 files'
> +                            ' changed, 1 insertion(+), 5
> deletions(-)\n\ndiff --git a/block/'
> +                            'Makefile.objs b/block/Makefile.objs\nindex
> 44a5416..c87d605 '
> +                            '100644\n--- a/block/Makefile.objs\n+++
> b/block/Makefile.objs\n@@'
> +                            ' -3,7 +3,7 @@ block-obj-y += qcow2.o
> qcow2-refcount.o qcow2-cluster.o'
> +                            ' qcow2-snapshot.o qcow2-c\n block-obj-y +=
> qed.o qed-gencb.o '
> +                            'qed-l2-cache.o qed-table.o qed-cluster.o\n
> block-obj-y += '
> +                            'qed-check.o\n block-obj-$(CONFIG_VHDX) +=
> vhdx.o vhdx-endian.o'
> +                            ' vhdx-log.o\n-block-obj-y +=
> quorum.o\n+block-obj-$(CONFIG_GNUTLS_HASH)'
> +                            ' += quorum.o\n block-obj-y += parallels.o
> blkdebug.o blkverify.o '
> +                            'blkreplay.o\n block-obj-y += block-backend.o
> snapshot.o qapi.o\n '
> +                            'block-obj-$(CONFIG_WIN32) += raw-win32.o
> win32-aio.o\ndiff --git '
> +                            'a/block/quorum.c b/block/quorum.c\nindex
> 331b726..18fbed8 100644\n'
> +                            '--- a/block/quorum.c\n+++ b/block/quorum.c\n@@
> -1113,10 +1113,6 @@'
> +                            ' static BlockDriver bdrv_quorum = {\n \n
> static void bdrv_quorum_init'
> +                            '(void)\n {\n-    if (!qcrypto_hash_supports(
> QCRYPTO_HASH_ALG_SHA256))'
> +                            ' {\n-        /* SHA256 hash support is
> required for quorum device */\n-'
> +                            '        return;\n-    }\n
>  bdrv_register(&bdrv_quorum);\n }\n \n-- \n'
> +                            '2.9.0\n\n\n'}
> +        dp = self.get_data_path("0001-simple-patch.mbox.gz")
> +        with open(dp, "r") as f:
> +            msg = mbox.MboxMessage(f.read()).get_json()
> +        self.assertEqual(msg, expected)
> +
> +
>  if __name__ == '__main__':
>      main()
> --
> 2.14.3 (Apple Git-98)
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/patchew-devel/attachments/20180428/0cffa36f/attachment.htm>


More information about the Patchew-devel mailing list