Commit 8965506d authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_interface_post: add ingestion method on Internet Message Posts

So a Mail Message can be created from a Post, and linked to another Mail Message if it is a response
parent e864bfb6
"""
From an Internet Message Post, creates a Mail Message (and attachements)
in ERP5.
"""
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
import email
portal = context.getPortalObject()
email_object = email.message_from_string(context.getData())
mail_message = portal.portal_contributions.newContent(
container_path='event_module',
filename='internet_message_post_ingestion.eml',
data=context.getData(),
)
mail_message.setAggregateValue(context)
strict_causality_reference = context.stripMessageId(email_object['in-reply-to'])
if strict_causality_reference != None:
causality_post_result_list = portal.internet_message_post_module.searchFolder(
portal_type=context.getPortalType(),
query=SimpleQuery(reference=strict_causality_reference),
)
if len(causality_post_result_list) == 1:
causality_event = causality_post_result_list[0].getAggregateRelatedValue()
mail_message.setCausalityValue(causality_event)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>InternetMessagePost_ingest</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -196,8 +196,9 @@ class TestInterfacePost(ERP5TypeLiveTestCase): ...@@ -196,8 +196,9 @@ class TestInterfacePost(ERP5TypeLiveTestCase):
for internet_message_post in internet_message_post_list: for internet_message_post in internet_message_post_list:
self.assertEqual(internet_message_post.getSimulationState(), 'exported') self.assertEqual(internet_message_post.getSimulationState(), 'exported')
mail_object = email.message_from_string(internet_message_post.getData())
self.assertEqual( self.assertEqual(
internet_message_post.getReference(), mail_message.getReference() internet_message_post.getReference(), mail_object['message-id'].strip('<>')
) )
self.assertEqual( self.assertEqual(
mail_message.getTextContent(), mail_message.getTextContent(),
...@@ -243,6 +244,49 @@ class TestInterfacePost(ERP5TypeLiveTestCase): ...@@ -243,6 +244,49 @@ class TestInterfacePost(ERP5TypeLiveTestCase):
mail_message.MailMessage_viewPreview.my_text_content.render(), mail_message.MailMessage_viewPreview.my_text_content.render(),
) )
def stepCheckCreateAndIngestInternetMessagePostResponse(self, sequence=None, sequence_list=None):
post = sequence['internet_message_post']
# Create a response mail object
mail_object = email.message_from_string(post.getData())
sender = mail_object['from']
recipient = mail_object['to']
mail_object.replace_header('from', recipient)
mail_object.replace_header('to', sender)
mail_object.add_header('In-Reply-To', mail_object['message-id'])
mail_object.add_header('References', mail_object['in-reply-to'])
mail_object.replace_header('message-id', email.utils.make_msgid())
mail_object.replace_header('subject', 'Re: {}'.format(mail_object['subject']))
# Ingest it
response_post = self.portal.internet_message_post_module.newContent(
portal_type='Internet Message Post',
data=mail_object.as_string(),
)
response_post.InternetMessagePost_ingest()
sequence['internet_message_post_response'] = response_post
def stepCheckMailMessageResponseCreated(self, sequence=None, sequence_list=None):
response_list = self._portal_catalog(
portal_type='Mail Message',
title='Re: Promotional campaign',
)
self.assertEqual(len(response_list), 1)
response, = response_list
self.assertEqual(
response.getCausalityValue(),
sequence['mail_message'],
)
self.assertEqual(
response.getAggregateValue(portal_type='Internet Message Post'),
sequence['internet_message_post_response']
)
self.assertEqual(response.getSourceTitle(), 'recipient')
self.assertEqual(response.getDestinationTitle(), 'sender')
self.assertEqual(response.getSimulationState(), 'delivered')
def test_emailSendingIsPilotedByInternetMessagePost(self): def test_emailSendingIsPilotedByInternetMessagePost(self):
""" """
""" """
...@@ -301,3 +345,19 @@ class TestInterfacePost(ERP5TypeLiveTestCase): ...@@ -301,3 +345,19 @@ class TestInterfacePost(ERP5TypeLiveTestCase):
""" """
sequence_list.addSequenceString(sequence_string) sequence_list.addSequenceString(sequence_string)
sequence_list.play(self) sequence_list.play(self)
def test_ingestedInternetMessagePostCreateMailMessageWithCausality(self):
sequence_list = SequenceList()
sequence_string = """
stepCreateMailMessage
stepStartMailMessage
stepCheckMailMessage
stepTic
stepCheckInternetMessagePostCreated
stepCheckLatestMessageListFromMailHost
stepCheckCreateAndIngestInternetMessagePostResponse
stepTic
stepCheckMailMessageResponseCreated
"""
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment