summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-08-10 13:37:37 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-08-11 17:00:34 +0200
commitb8410e1e199a581155566c6c3b9d40dbda32b082 (patch)
treea9dbd66030b1a070192b264630a0512d6cac175b /service/pixelated/adapter
parent4ae8a4bb0dedae1ab5a6b66eea3e1b9598a72e9f (diff)
Fixed test_search integration test.
Diffstat (limited to 'service/pixelated/adapter')
-rw-r--r--service/pixelated/adapter/mailstore/body_parser.py41
-rw-r--r--service/pixelated/adapter/mailstore/leap_mailstore.py18
-rw-r--r--service/pixelated/adapter/search/__init__.py2
3 files changed, 56 insertions, 5 deletions
diff --git a/service/pixelated/adapter/mailstore/body_parser.py b/service/pixelated/adapter/mailstore/body_parser.py
new file mode 100644
index 00000000..893daac9
--- /dev/null
+++ b/service/pixelated/adapter/mailstore/body_parser.py
@@ -0,0 +1,41 @@
+#
+# Copyright (c) 2015 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+from email.parser import Parser
+
+
+class BodyParser(object):
+
+ def __init__(self, content, content_type='text/plain; charset="us-ascii"', content_transfer_encoding=None):
+ self._content = content
+ self._content_type = content_type
+ self._content_transfer_encoding = content_transfer_encoding
+
+ def parsed_content(self):
+ parser = Parser()
+
+ text=''
+ text += 'Content-Type: %s\n' % self._content_type
+ if self._content_transfer_encoding is not None:
+ text += 'Content-Transfer-Encoding: %s\n' % self._content_transfer_encoding
+ text += '\n'
+ text += self._content
+
+ parsed_body = parser.parsestr(text)
+
+ result = unicode(parsed_body.get_payload(decode=True), encoding='utf-8')
+
+ return unicode(result) \ No newline at end of file
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py
index 9261f877..8831ae84 100644
--- a/service/pixelated/adapter/mailstore/leap_mailstore.py
+++ b/service/pixelated/adapter/mailstore/leap_mailstore.py
@@ -16,6 +16,7 @@
from uuid import uuid4
from leap.mail.adaptors.soledad import SoledadMailAdaptor
from twisted.internet import defer
+from pixelated.adapter.mailstore.body_parser import BodyParser
from pixelated.adapter.mailstore.mailstore import MailStore, underscore_uuid
from leap.mail.mail import Message
@@ -65,8 +66,9 @@ class LeapMail(Mail):
def raw(self):
result = ''
for k, v in self._headers.items():
- result = result + '%s: %s\n' % (k, v)
- result = result + '\n'
+ result += '%s: %s\n' % (k, v)
+ result += '\n'
+
if self._body:
result = result + self._body
@@ -151,10 +153,11 @@ class LeapMailStore(MailStore):
mailbox = yield self._get_or_create_mailbox(mailbox_name)
message = SoledadMailAdaptor().get_msg_from_string(Message, raw_msg)
message.get_wrapper().set_mbox_uuid(mailbox.uuid)
+
yield message.get_wrapper().create(self.soledad)
# add behavious from insert_mdoc_id from mail.py
- mail = yield self._leap_message_to_leap_mail(message.get_wrapper().mdoc.doc_id, message, include_body=False)
+ mail = yield self._leap_message_to_leap_mail(message.get_wrapper().mdoc.doc_id, message, include_body=True) # TODO test that asserts include_body
defer.returnValue(mail)
@defer.inlineCallbacks
@@ -200,7 +203,8 @@ class LeapMailStore(MailStore):
@defer.inlineCallbacks
def _leap_message_to_leap_mail(self, mail_id, message, include_body):
if include_body:
- body = (yield message.get_wrapper().get_body(self.soledad)).raw
+ # TODO use body from message if available
+ body = yield self._raw_message_body(message)
else:
body = None
@@ -213,6 +217,12 @@ class LeapMailStore(MailStore):
defer.returnValue(mail)
@defer.inlineCallbacks
+ def _raw_message_body(self, message):
+ content_doc = (yield message.get_wrapper().get_body(self.soledad))
+ parser = BodyParser(content_doc.raw, content_type=content_doc.content_type, content_transfer_encoding=content_doc.content_transfer_encoding)
+ defer.returnValue(parser.parsed_content())
+
+ @defer.inlineCallbacks
def _mailbox_name_from_uuid(self, uuid):
map = (yield self._mailbox_uuid_to_name_map())
defer.returnValue(map[uuid])
diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py
index 759ad5ef..a95055d2 100644
--- a/service/pixelated/adapter/search/__init__.py
+++ b/service/pixelated/adapter/search/__init__.py
@@ -137,7 +137,7 @@ class SearchEngine(object):
'body': unicode(mdict['textPlainBody'] if 'textPlainBody' in mdict else mdict['body']),
'ident': unicode(mdict['ident']),
'flags': unicode(','.join(unique(mail.flags))),
- 'raw': unicode(mail.raw.decode('utf-8'))
+ 'raw': unicode(mail.raw)
}
writer.update_document(**index_data)