From ee8facd0ccef0b3ce4243f286c0868ab2c46f538 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Fri, 20 Dec 2013 15:20:51 +0100
Subject: [PATCH] Move Event_send logic of sending email in MailMessage_send.

We only want to send an email for mail message portal type
---
 .../portal_skins/erp5_base/Event_send.xml     | 100 +---------
 .../erp5_base/MailMessage_send.xml            | 172 ++++++++++++++++++
 bt5/erp5_base/bt/revision                     |   2 +-
 3 files changed, 177 insertions(+), 97 deletions(-)
 create mode 100644 bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/MailMessage_send.xml

diff --git a/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/Event_send.xml b/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/Event_send.xml
index 9f56863912..1f5bea537f 100644
--- a/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/Event_send.xml
+++ b/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/Event_send.xml
@@ -50,102 +50,10 @@
         </item>
         <item>
             <key> <string>_body</string> </key>
-            <value> <string>from email.utils import formataddr\n
-from email import message_from_string\n
-portal = context.getPortalObject()\n
-\n
-if attachment_list is None:\n
-  attachment_list = []\n
-use_activity = False\n
-mail_message = None\n
-to_url_list = []\n
-\n
-if not context.isTempDocument() and to_url is None:\n
-  use_activity = True\n
-\n
-if to_url is None:\n
-  if use_activity:\n
-    recipient = context.getDestinationValue()\n
-    to_url_list.append(formataddr((recipient.getTitle(), recipient.getDefaultEmailText())))\n
-  else:\n
-    to_email_list = []\n
-    for recipient in context.getDestinationValueList():\n
-      email = recipient.getDefaultEmailText()\n
-      if email:\n
-        to_email_list.append(email)\n
-        to_url_list.append(formataddr((recipient.getTitle(), recipient.getDefaultEmailText())))\n
-      else:\n
-        raise ValueError("One email must be set to %s" % recipient.getRelativeUrl())\n
-else:\n
-  to_url_list.append(to_url)\n
-\n
-if download or not use_activity:\n
-  for to_url in to_url_list:\n
-    body = body or context.getTextContent()\n
-    subject = subject or context.getTitle()\n
-\n
-    # From\n
-    if from_url is None:\n
-      sender = context.getSourceValue()\n
-      from_url = formataddr((sender.getTitle(), sender.getDefaultEmailText()))\n
-\n
-    # Return-Path\n
-    if reply_url is None:\n
-      reply_url = portal.portal_preferences.getPreferredEventSenderEmail()\n
-    additional_headers = None\n
-    if reply_url:\n
-      additional_headers = {\'Return-Path\':reply_url}\n
-\n
-    # Attachments\n
-    document_type_list = list(context.getPortalEmbeddedDocumentTypeList()) + list(context.getPortalDocumentTypeList())\n
-    embedded_file_list = context.getAggregateValueList(portal_type=document_type_list)\n
-\n
-    content_type = context.getContentType()\n
-\n
-    mail_message = context.Base_createMailMessageAsString(from_url, \n
-      to_url,\n
-      subject,\n
-      body,\n
-      content_type,\n
-      attachment_list=attachment_list,\n
-      embedded_file_list=embedded_file_list)\n
-\n
-    if len(embedded_file_list):\n
-      # do not store aggregated documents in Email document\n
-      # to avoid duplicate data\n
-      tmp_mail_message = context.Base_createMailMessageAsString(from_url,\n
-        to_url,\n
-        subject,\n
-        body,\n
-        content_type,\n
-        attachment_list=attachment_list)\n
-      context.setData(tmp_mail_message)\n
-    else:\n
-      context.setData(mail_message)\n
-    if not use_activity:\n
-      context.activate(activity=\'SQLQueue\').sendMailHostMessage(mail_message)\n
-\n
-if use_activity:\n
-  destination_list = context.getDestinationValueList()\n
-  path_list = [x.getPath() for x in destination_list]\n
-  uid_list = [x.getUid() for x in destination_list]\n
-  method_kw = dict(event_relative_url=context.getRelativeUrl(),\n
-    from_url=from_url,\n
-    attachment_list=attachment_list)\n
-  context.activate(\n
-    after_path_and_method_id=(path_list, \n
-                              (\'immediateReindexObject\', \'recursiveImmediateReindexObject\'))).Event_sendByActivity(\n
-    uid_list=uid_list,\n
-    method_kw=method_kw, **kw)\n
-\n
-# Transit event workflow\n
-if context.getTypeInfo() is not None:\n
-  for transition_id in \'plan\', \'order\', \'start\':\n
-    if portal.portal_workflow.isTransitionPossible(context, transition_id):\n
-      getattr(context, transition_id)()\n
-\n
-if download:\n
-  return mail_message\n
+            <value> <string>"""Event send does nothing by default, it has to be implemented for each kind\n
+of event, look at MailMessage_send for example.\n
+"""\n
+pass\n
 </string> </value>
         </item>
         <item>
diff --git a/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/MailMessage_send.xml b/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/MailMessage_send.xml
new file mode 100644
index 0000000000..e35975ad70
--- /dev/null
+++ b/bt5/erp5_base/SkinTemplateItem/portal_skins/erp5_base/MailMessage_send.xml
@@ -0,0 +1,172 @@
+<?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>_body</string> </key>
+            <value> <string>"""Send a mail message.\n
+\n
+This script is used by notification tool, that\'s why it is in erp5_base.\n
+"""\n
+\n
+from email.utils import formataddr\n
+portal = context.getPortalObject()\n
+\n
+if attachment_list is None:\n
+  attachment_list = []\n
+use_activity = False\n
+mail_message = None\n
+to_url_list = []\n
+\n
+if not context.isTempDocument() and to_url is None:\n
+  use_activity = True\n
+\n
+if to_url is None:\n
+  for recipient in context.getDestinationValueList():\n
+    email = recipient.getDefaultEmailText()\n
+    if email:\n
+      to_url_list.append(formataddr((recipient.getTitle(), recipient.getDefaultEmailText())))\n
+    else:\n
+      # MailMessage portal type is supposed to have a constraint to report this\n
+      # to the user earlier\n
+      raise ValueError("One email must be set to %s" % recipient.getRelativeUrl())\n
+else:\n
+  to_url_list.append(to_url)\n
+\n
+if download or not use_activity:\n
+  for to_url in to_url_list:\n
+    body = body or context.getTextContent()\n
+    subject = subject or context.getTitle()\n
+\n
+    # From\n
+    if from_url is None:\n
+      sender = context.getSourceValue()\n
+      if sender is not None:\n
+        from_url = formataddr((sender.getTitle(), sender.getDefaultEmailText()))\n
+      else:\n
+        from_url = portal.portal_preferences.getPreferredEventSenderEmail()\n
+        if not from_url:\n
+          raise ValueError(\'Preferred Event Sender Email not configured\')\n
+\n
+    # Return-Path\n
+    if reply_url is None:\n
+      reply_url = portal.portal_preferences.getPreferredEventSenderEmail()\n
+    additional_headers = None\n
+    if reply_url:\n
+      additional_headers = {\'Return-Path\':reply_url}\n
+\n
+    # Attachments\n
+    document_type_list = list(context.getPortalEmbeddedDocumentTypeList()) + list(context.getPortalDocumentTypeList())\n
+    embedded_file_list = context.getAggregateValueList(portal_type=document_type_list)\n
+\n
+    content_type = context.getContentType()\n
+\n
+    mail_message = context.Base_createMailMessageAsString(from_url, \n
+      to_url,\n
+      subject,\n
+      body,\n
+      content_type,\n
+      attachment_list=attachment_list,\n
+      embedded_file_list=embedded_file_list)\n
+\n
+    if len(embedded_file_list):\n
+      # do not store aggregated documents in Email document\n
+      # to avoid duplicate data\n
+      tmp_mail_message = context.Base_createMailMessageAsString(from_url,\n
+        to_url,\n
+        subject,\n
+        body,\n
+        content_type,\n
+        attachment_list=attachment_list)\n
+      context.setData(tmp_mail_message)\n
+    else:\n
+      context.setData(mail_message)\n
+    if not use_activity:\n
+      context.activate(activity=\'SQLQueue\').sendMailHostMessage(mail_message)\n
+\n
+if use_activity:\n
+  destination_list = context.getDestinationValueList()\n
+  path_list = [x.getPath() for x in destination_list]\n
+  uid_list = [x.getUid() for x in destination_list]\n
+  method_kw = dict(event_relative_url=context.getRelativeUrl(),\n
+    from_url=from_url,\n
+    attachment_list=attachment_list)\n
+  context.activate(\n
+    after_path_and_method_id=(path_list, \n
+      # XXX Event_sendByActivity only applies to Mail Message at the moment.\n
+      # We should either renamted it to MailMessage_ or make this logic available to \n
+      # all kind of events.\n
+                              (\'immediateReindexObject\', \'recursiveImmediateReindexObject\'))).Event_sendByActivity(\n
+    uid_list=uid_list,\n
+    method_kw=method_kw, **kw)\n
+\n
+if download:\n
+  return mail_message\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>from_url=None, to_url=None, reply_url=None, subject=None, body=None, attachment_format=None, attachment_list=None, download=None, **kw</string> </value>
+        </item>
+        <item>
+            <key> <string>_proxy_roles</string> </key>
+            <value>
+              <tuple>
+                <string>Manager</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>MailMessage_send</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_base/bt/revision b/bt5/erp5_base/bt/revision
index 062e64ab9d..cb0a134b7b 100644
--- a/bt5/erp5_base/bt/revision
+++ b/bt5/erp5_base/bt/revision
@@ -1 +1 @@
-1054
\ No newline at end of file
+1057
\ No newline at end of file
-- 
2.30.9