[Erp5-report] r18331 - in /erp5/trunk/products/ERP5Type: ZopePatch.py patches/MailTemplates.py

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Dec 14 15:28:44 CET 2007


Author: alex
Date: Fri Dec 14 15:28:39 2007
New Revision: 18331

URL: http://svn.erp5.org?rev=18331&view=rev
Log:
Add a monkeypatch to MailTemplates: as iHotfix forces PageTemplates to be
rendered as utf-8 encoded strings, it is incompatible with Products that
expect them to render as unicode objects.

- this patch is based on MailTemplates 1.1.0
- it will only try to encode() text if it's of type unicode


Added:
    erp5/trunk/products/ERP5Type/patches/MailTemplates.py
Modified:
    erp5/trunk/products/ERP5Type/ZopePatch.py

Modified: erp5/trunk/products/ERP5Type/ZopePatch.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/ZopePatch.py?rev=18331&r1=18330&r2=18331&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/ZopePatch.py (original)
+++ erp5/trunk/products/ERP5Type/ZopePatch.py Fri Dec 14 15:28:39 2007
@@ -54,6 +54,7 @@
 from Products.ERP5Type.patches import PersistentMapping
 from Products.ERP5Type.patches import DateTimePatch
 from Products.ERP5Type.patches import PythonScript
+from Products.ERP5Type.patches import MailTemplates
 
 # for python2.3 compatibility
 import threading

Added: erp5/trunk/products/ERP5Type/patches/MailTemplates.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/patches/MailTemplates.py?rev=18331&view=auto
==============================================================================
--- erp5/trunk/products/ERP5Type/patches/MailTemplates.py (added)
+++ erp5/trunk/products/ERP5Type/patches/MailTemplates.py Fri Dec 14 15:28:39 2007
@@ -1,0 +1,81 @@
+#!/usr/bin/python
+# Copyright (c) 2005 Simplistix Ltd
+#
+# This Software is released under the MIT License:
+# http://www.opensource.org/licenses/mit-license.html
+# See license.txt for more details.
+
+"""
+this patch is based on MailTemplates 1.1.0
+it will only try to encode() text if it's of type unicode
+"""
+
+try:
+  from Products.MailTemplates import BaseMailTemplate
+except ImportError:
+  BaseMailTemplate = None
+
+if BaseMailTemplate is not None:
+  def _process_utf8(self,kw):
+      # sort out what encoding we're going to use
+      encoding = kw.get('encoding',
+                        self.getProperty('encoding',
+                                         BaseMailTemplate.default_encoding))
+      text = self.__class__.__bases__[1].__call__(self,**kw)
+      if not self.html() and isinstance(text, unicode):
+          text = text.encode(encoding,'replace')
+      # now turn the result into a MIMEText object
+      msg = BaseMailTemplate.MIMEText(
+          text.replace('\r',''),
+          self.content_type.split('/')[1],
+          encoding
+          )
+      # sort out what headers and addresses we're going to use
+      headers = {}
+      values = {}
+      # headers from the headers property
+      for header in getattr(self,'headers',()):
+          name,value = header.split(':',1)
+          headers[name]=value
+      # headers from the headers parameter
+      headers_param = kw.get('headers',{})
+      headers.update(headers_param)
+      # values and some specific headers
+      for key,header in (('mfrom','From'),
+                         ('mto','To'),
+                         ('mcc','Cc'),
+                         ('mbcc','Bcc'),
+                         ('subject','Subject')):
+          value = kw.get(key,
+                         headers_param.get(header,
+                                           getattr(self,
+                                                   key,
+                                                   headers.get(header))))
+          if value is not None:
+              values[key]=value
+              # turn some sequences in coma-seperated strings
+              if isinstance(value,tuple) or isinstance(value,list):
+                  value = ', '.join(value)
+              # make sure we have no unicode headers
+              if isinstance(value,unicode):
+                  value = value.encode(encoding)
+              headers[header]=value
+      # check required values have been supplied
+      errors = []
+      for param in ('mfrom','mto','subject'):
+          if not values.get(param):
+              errors.append(param)
+      if errors:
+          raise TypeError(
+              'The following parameters were required by not specified: '+(
+              ', '.join(errors)
+              ))
+      # add date header
+      headers['Date']=BaseMailTemplate.DateTime().rfc822()
+      # turn headers into an ordered list for predictable header order
+      keys = headers.keys()
+      keys.sort()
+      return msg,values,[(key,headers[key]) for key in keys]
+
+  BaseMailTemplate.BaseMailTemplate._process = _process_utf8
+




More information about the Erp5-report mailing list