[IMP] mail: refactor various places where we append content to HTML

bzr revid: odo@openerp.com-20120831155103-t13p33p7232g0128
This commit is contained in:
Olivier Dony
2012-08-31 17:51:03 +02:00
parent bdd972e3c9
commit 24fab3ca7d
4 changed files with 9 additions and 31 deletions
+5 -6
View File
@@ -29,7 +29,7 @@ from osv import fields
import tools
from tools.translate import _
from tools.html_sanitize import html_sanitize
from tools import html2plaintext
from tools import html2plaintext, append_content_to_html
from urllib import quote as quote
_logger = logging.getLogger(__name__)
@@ -322,13 +322,12 @@ class email_template(osv.osv):
template.model, res_id, context=context) \
or False
if values['body_html']:
values['body'] = html_sanitize(values['body_html'])
values['body_text'] = html2plaintext(values['body_html'])
if template.user_signature:
signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
values['body'] += '\n\n' + signature
values['body_html'] = append_content_to_html(values['body_html'], signature)
if values['body_html']:
values['body'] = html_sanitize(values['body_html'])
values.update(mail_server_id = template.mail_server_id.id or False,
auto_delete = template.auto_delete,
@@ -100,8 +100,6 @@ class mail_compose_message(osv.osv_memory):
values.update(use_template=use_template, template_id=template_id)
print 'returning ', values
return {'value': values}
def toggle_template(self, cr, uid, ids, context=None):
+1 -6
View File
@@ -98,12 +98,7 @@ class mail_notification(osv.Model):
body_html = msg.body
signature = msg.author_id and msg.author_id.user_ids[0].signature or ''
if signature:
signature_block = u'\n<pre>%s</pre>\n' % signature
insertion_point = body_html.find('</html>')
if insertion_point > -1:
body_html = body_html[:insertion_point] + signature_block + body_html[insertion_point:]
else:
body_html += signature_block
body_html = tools.append_content_to_html(body_html, signature)
towrite = {
'mail_message_id': msg.id,
+3 -17
View File
@@ -460,29 +460,15 @@ class mail_thread(osv.Model):
continue
# 2) text/plain -> <pre/>
if part.get_content_type() == 'text/plain' and (not alternative or not body):
insertion_point = body.find('</html>')
# plain text is wrapped in <pre/> to preserve formatting
text = u'<pre>%s</pre>' % tools.ustr(part.get_payload(decode=True),
encoding, errors='replace')
if insertion_point != -1:
body = body[:insertion_point] + text + body[insertion_point:]
else:
body += text
body = tools.append_content_to_html(body, tools.ustr(part.get_payload(decode=True),
encoding, errors='replace'))
# 3) text/html -> raw
elif part.get_content_type() == 'text/html':
html = tools.ustr(part.get_payload(decode=True), encoding, errors='replace')
if alternative:
body = html
# force </html> tag to lowercase, for easier matching
body = re.sub(r'(?i)</html>', r'</html>', body)
else:
# strip enclosing html/body tags and append to existing body
html = re.sub(r'(?i)(</?html>|</?body>)', '', html)
insertion_point = body.find('</html>')
if insertion_point != -1:
body = body[:insertion_point] + html + body[insertion_point:]
else:
body += html
body = tools.append_content_to_html(body, html, plaintext=False)
# 4) Anything else -> attachment
else:
attachments.append((filename or 'attachment', part.get_payload(decode=True)))