[Patchew-devel] [PATCH 1/2] Text to json converter for mbox
Shubham Jain
shubhamjain7495 at gmail.com
Tue May 8 19:32:06 UTC 2018
- Mbox would now return a dictionary object which would help REST API deserialize 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 AddressSerializer as api.rest calls models which call back mbox. Use add_db_to_rest instead.
---
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
SEARCH_PARAM = 'q'
@@ -123,10 +124,7 @@ class AddressSerializer(serializers.Serializer):
address = EmailField()
def to_representation(self, obj):
- if obj[0] != obj[1]:
- return {"name": obj[0], "address": obj[1]}
- else:
- return {"address": obj[1]}
+ return addr_db_to_rest(obj)
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
def _parse_header(header):
r = ''
@@ -34,6 +35,11 @@ def _addr_fmt_text(name, addr):
else:
return addr
+def addr_db_to_rest(obj):
+ if obj[0] != obj[1]:
+ return {"name": obj[0], "address": obj[1]}
+ else:
+ return {"address": obj[1]}
class MboxMessage(object):
""" Helper class to process mbox """
@@ -269,3 +275,15 @@ 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'] = DateTimeField().to_representation(self.get_date())
+ msg['subject'] = self.get_subject()
+ msg['sender'] = addr_db_to_rest(self.get_from())
+ msg['recipients'] = [addr_db_to_rest(x) for x in (self.get_to() + self.get_cc())]
+ msg['mbox'] = 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 = mbox.MboxMessage(f.read())
self.assertTrue(msg.is_patch())
+ def test_get_json(self):
+ dp = self.get_data_path("0001-simple-patch.mbox.gz")
+ with open(dp, "r") as f:
+ content = f.read()
+ expected = {'message_id': '20160628014747.20971-1-famz at redhat.com',
+ 'in_reply_to': '',
+ 'date': '2016-06-28T01:47:47',
+ 'subject': '[Qemu-devel] [PATCH] quorum: Only compile when supported',
+ 'sender': {'name': 'Fam Zheng', 'address': 'famz at redhat.com'},
+ 'recipients': [{'address': 'qemu-devel at nongnu.org'},
+ {'name': 'Kevin Wolf', 'address': 'kwolf at redhat.com'},
+ {'name': 'Alberto Garcia', 'address': 'berto at igalia.com'},
+ {'address': 'qemu-block at nongnu.org'},
+ {'name': 'Max Reitz', 'address': 'mreitz at redhat.com'}],
+ 'mbox':content
+ }
+ msg = mbox.MboxMessage(content).get_json()
+ self.assertEqual(msg, expected)
+
if __name__ == '__main__':
main()
--
2.14.3 (Apple Git-98)
More information about the Patchew-devel
mailing list