[MERGE] forward port of branch 7.0 up to 75d3ea6
[odoo/odoo.git] / addons / email_template / email_template.py
index c5e24b0..ed19efd 100644 (file)
 ##############################################################################
 
 import base64
+import datetime
+import dateutil.relativedelta as relativedelta
 import logging
+import lxml
+import urlparse
 
-from openerp import netsvc
+import openerp
+from openerp import SUPERUSER_ID
 from openerp.osv import osv, fields
-from openerp.osv import fields
 from openerp import tools
 from openerp.tools.translate import _
 from urllib import urlencode, quote as quote
 
 _logger = logging.getLogger(__name__)
 
+
+def format_tz(pool, cr, uid, dt, tz=False, format=False, context=None):
+    context = dict(context or {})
+    if tz:
+        context['tz'] = tz or pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] or "UTC"
+    timestamp = datetime.datetime.strptime(dt, tools.DEFAULT_SERVER_DATETIME_FORMAT)
+
+    ts = fields.datetime.context_timestamp(cr, uid, timestamp, context)
+
+    if format:
+        return ts.strftime(format)
+    else:
+        lang = context.get("lang")
+        lang_params = {}
+        if lang:
+            res_lang = pool.get('res.lang')
+            ids = res_lang.search(cr, uid, [("code", "=", lang)])
+            if ids:
+                lang_params = res_lang.read(cr, uid, ids[0], ["date_format", "time_format"])
+        format_date = lang_params.get("date_format", '%B-%d-%Y')
+        format_time = lang_params.get("time_format", '%I-%M %p')
+
+        fdate = ts.strftime(format_date)
+        ftime = ts.strftime(format_time)
+        return "%s %s (%s)" % (fdate, ftime, tz)
+
 try:
     # We use a jinja2 sandboxed environment to render mako templates.
     # Note that the rendering does not cover all the mako syntax, in particular
@@ -57,17 +87,80 @@ try:
         'str': str,
         'quote': quote,
         'urlencode': urlencode,
+        'datetime': datetime,
+        'len': len,
+        'abs': abs,
+        'min': min,
+        'max': max,
+        'sum': sum,
+        'filter': filter,
+        'reduce': reduce,
+        'map': map,
+        'round': round,
+
+        # dateutil.relativedelta is an old-style class and cannot be directly
+        # instanciated wihtin a jinja2 expression, so a lambda "proxy" is
+        # is needed, apparently.
+        'relativedelta': lambda *a, **kw : relativedelta.relativedelta(*a, **kw),
     })
 except ImportError:
     _logger.warning("jinja2 not available, templating features will not work!")
 
+
 class email_template(osv.osv):
     "Templates for sending email"
     _name = "email.template"
     _description = 'Email Templates'
     _order = 'name'
 
-    def render_template(self, cr, uid, template, model, res_id, context=None):
+    def default_get(self, cr, uid, fields, context=None):
+        res = super(email_template, self).default_get(cr, uid, fields, context)
+        if res.get('model'):
+            res['model_id'] = self.pool['ir.model'].search(cr, uid, [('model', '=', res.pop('model'))], context=context)[0]
+        return res
+
+    def _replace_local_links(self, cr, uid, html, context=None):
+        """ Post-processing of html content to replace local links to absolute
+        links, using web.base.url as base url. """
+        if not html:
+            return html
+
+        # form a tree
+        root = lxml.html.fromstring(html)
+        if not len(root) and root.text is None and root.tail is None:
+            html = '<div>%s</div>' % html
+            root = lxml.html.fromstring(html)
+
+        base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
+        (base_scheme, base_netloc, bpath, bparams, bquery, bfragment) = urlparse.urlparse(base_url)
+
+        def _process_link(url):
+            new_url = url
+            (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
+            if not scheme and not netloc:
+                new_url = urlparse.urlunparse((base_scheme, base_netloc, path, params, query, fragment))
+            return new_url
+
+        # check all nodes, replace :
+        # - img src -> check URL
+        # - a href -> check URL
+        for node in root.iter():
+            if node.tag == 'a' and node.get('href'):
+                node.set('href', _process_link(node.get('href')))
+            elif node.tag == 'img' and not node.get('src', 'data').startswith('data'):
+                node.set('src', _process_link(node.get('src')))
+
+        html = lxml.html.tostring(root, pretty_print=False, method='html')
+        # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that
+        if html.startswith('<div>') and html.endswith('</div>'):
+            html = html[5:-6]
+        return html
+
+    def render_post_process(self, cr, uid, html, context=None):
+        html = self._replace_local_links(cr, uid, html, context=context)
+        return html
+
+    def render_template_batch(self, cr, uid, template, model, res_ids, context=None, post_process=False):
         """Render the given template text, replace mako expressions ``${expr}``
            with the result of evaluating these expressions with
            an evaluation context containing:
@@ -79,46 +172,65 @@ class email_template(osv.osv):
 
            :param str template: the template text to render
            :param str model: model name of the document record this mail is related to.
-           :param int res_id: id of the document record this mail is related to.
+           :param int res_ids: list of ids of document records those mails are related to.
         """
-        if not template:
-            return u""
         if context is None:
             context = {}
+        results = dict.fromkeys(res_ids, u"")
+
+        # try to load the template
         try:
-            template = tools.ustr(template)
-            record = None
-            if res_id:
-                record = self.pool.get(model).browse(cr, uid, res_id, context=context)
-            user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
-            variables = {
-                'object': record,
-                'user': user,
-                'ctx': context,     # context kw would clash with mako internals
-            }
-            result = mako_template_env.from_string(template).render(variables)
-            if result == u"False":
-                result = u""
-            return result
+            template = mako_template_env.from_string(tools.ustr(template))
         except Exception:
-            _logger.exception("failed to render mako template value %r", template)
-            return u""
-
-    def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
+            _logger.exception("Failed to load template %r", template)
+            return results
+
+        # prepare template variables
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None]
+        variables = {
+            'format_tz': lambda dt, tz=False, format=False: format_tz(self.pool, cr, uid, dt, tz, format, context),
+            'user': user,
+            'ctx': context,  # context kw would clash with mako internals
+        }
+        for record in records:
+            res_id = record.id if record else None
+            variables['object'] = record
+            try:
+                render_result = template.render(variables)
+            except Exception:
+                _logger.exception("Failed to render template %r using values %r" % (template, variables))
+                render_result = u""
+            if render_result == u"False":
+                render_result = u""
+            results[res_id] = render_result
+
+        if post_process:
+            for res_id, result in results.iteritems():
+                results[res_id] = self.render_post_process(cr, uid, result, context=context)
+        return results
+
+    def get_email_template_batch(self, cr, uid, template_id=False, res_ids=None, context=None):
         if context is None:
             context = {}
+        if res_ids is None:
+            res_ids = [None]
+        results = dict.fromkeys(res_ids, False)
+
         if not template_id:
-            return False
+            return results
         template = self.browse(cr, uid, template_id, context)
-        lang = self.render_template(cr, uid, template.lang, template.model, record_id, context)
-        if lang:
-            # Use translated template if necessary
-            ctx = context.copy()
-            ctx['lang'] = lang
-            template = self.browse(cr, uid, template.id, ctx)
-        else:
-            template = self.browse(cr, uid, int(template_id), context)
-        return template
+        langs = self.render_template_batch(cr, uid, template.lang, template.model, res_ids, context)
+        for res_id, lang in langs.iteritems():
+            if lang:
+                # Use translated template if necessary
+                ctx = context.copy()
+                ctx['lang'] = lang
+                template = self.browse(cr, uid, template.id, ctx)
+            else:
+                template = self.browse(cr, uid, int(template_id), context)
+            results[res_id] = template
+        return results
 
     def onchange_model_id(self, cr, uid, ids, model_id, context=None):
         mod_name = False
@@ -146,13 +258,15 @@ class email_template(osv.osv):
             help="Sender address (placeholders may be used here). If not set, the default "
                     "value will be the author's email alias if configured, or email address."),
         'email_to': fields.char('To (Emails)', help="Comma-separated recipient addresses (placeholders may be used here)"),
-        'email_recipients': fields.char('To (Partners)', help="Comma-separated ids of recipient partners (placeholders may be used here)"),
+        'partner_to': fields.char('To (Partners)',
+            help="Comma-separated ids of recipient partners (placeholders may be used here)",
+            oldname='email_recipients'),
         'email_cc': fields.char('Cc', help="Carbon copy recipients (placeholders may be used here)"),
         'reply_to': fields.char('Reply-To', help="Preferred response address (placeholders may be used here)"),
         'mail_server_id': fields.many2one('ir.mail_server', 'Outgoing Mail Server', readonly=False,
                                           help="Optional preferred server for outgoing mails. If not set, the highest "
                                                "priority one will be used."),
-        'body_html': fields.text('Body', translate=True, help="Rich-text/HTML version of the message (placeholders may be used here)"),
+        'body_html': fields.html('Body', translate=True, sanitize=False, help="Rich-text/HTML version of the message (placeholders may be used here)"),
         'report_name': fields.char('Report Filename', translate=True,
                                    help="Name to use for the generated report file (may contain placeholders)\n"
                                         "The extension can be omitted and will then come from the report type."),
@@ -189,7 +303,6 @@ class email_template(osv.osv):
     }
 
     def create_action(self, cr, uid, ids, context=None):
-        vals = {}
         action_obj = self.pool.get('ir.actions.act_window')
         data_obj = self.pool.get('ir.model.data')
         for template in self.browse(cr, uid, ids, context=context):
@@ -197,7 +310,7 @@ class email_template(osv.osv):
             model_data_id = data_obj._get_id(cr, uid, 'mail', 'email_compose_message_wizard_form')
             res_id = data_obj.browse(cr, uid, model_data_id, context=context).res_id
             button_name = _('Send Mail (%s)') % template.name
-            vals['ref_ir_act_window'] = action_obj.create(cr, uid, {
+            act_id = action_obj.create(cr, SUPERUSER_ID, {
                  'name': button_name,
                  'type': 'ir.actions.act_window',
                  'res_model': 'mail.compose.message',
@@ -209,27 +322,29 @@ class email_template(osv.osv):
                  'target': 'new',
                  'auto_refresh':1
             }, context)
-            vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
+            ir_values_id = self.pool.get('ir.values').create(cr, SUPERUSER_ID, {
                  'name': button_name,
                  'model': src_obj,
                  'key2': 'client_action_multi',
-                 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
+                 'value': "ir.actions.act_window,%s" % act_id,
                  'object': True,
              }, context)
-        self.write(cr, uid, ids, {
-                    'ref_ir_act_window': vals.get('ref_ir_act_window',False),
-                    'ref_ir_value': vals.get('ref_ir_value',False),
-                }, context)
+
+            template.write({
+                'ref_ir_act_window': act_id,
+                'ref_ir_value': ir_values_id,
+            })
+
         return True
 
     def unlink_action(self, cr, uid, ids, context=None):
         for template in self.browse(cr, uid, ids, context=context):
             try:
                 if template.ref_ir_act_window:
-                    self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context)
+                    self.pool.get('ir.actions.act_window').unlink(cr, SUPERUSER_ID, template.ref_ir_act_window.id, context)
                 if template.ref_ir_value:
                     ir_values_obj = self.pool.get('ir.values')
-                    ir_values_obj.unlink(cr, uid, template.ref_ir_value.id, context)
+                    ir_values_obj.unlink(cr, SUPERUSER_ID, template.ref_ir_value.id, context)
             except Exception:
                 raise osv.except_osv(_("Warning"), _("Deletion of the action record failed."))
         return True
@@ -297,69 +412,81 @@ class email_template(osv.osv):
                         })
         return {'value': result}
 
-    def generate_email(self, cr, uid, template_id, res_id, context=None):
-        """Generates an email from the template for given (model, res_id) pair.
-
-           :param template_id: id of the template to render.
-           :param res_id: id of the record to use for rendering the template (model
-                          is taken from template definition)
-           :returns: a dict containing all relevant fields for creating a new
-                     mail.mail entry, with one extra key ``attachments``, in the
-                     format [(report_name, data)] where data is base64 encoded.
+    def generate_email_batch(self, cr, uid, template_id, res_ids, context=None, fields=None):
+        """Generates an email from the template for given the given model based on
+        records given by res_ids.
+
+        :param template_id: id of the template to render.
+        :param res_id: id of the record to use for rendering the template (model
+                       is taken from template definition)
+        :returns: a dict containing all relevant fields for creating a new
+                  mail.mail entry, with one extra key ``attachments``, in the
+                  format [(report_name, data)] where data is base64 encoded.
         """
         if context is None:
             context = {}
+        if fields is None:
+            fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to']
+
         report_xml_pool = self.pool.get('ir.actions.report.xml')
-        template = self.get_email_template(cr, uid, template_id, res_id, context)
-        values = {}
-        for field in ['subject', 'body_html', 'email_from',
-                      'email_to', 'email_recipients', 'email_cc', 'reply_to']:
-            values[field] = self.render_template(cr, uid, getattr(template, field),
-                                                 template.model, res_id, context=context) \
-                                                 or False
-        if template.user_signature:
-            signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
-            if signature:
-                values['body_html'] = tools.append_content_to_html(values['body_html'], signature)
-
-        if values['body_html']:
-            values['body'] = tools.html_sanitize(values['body_html'])
-
-        values.update(mail_server_id=template.mail_server_id.id or False,
-                      auto_delete=template.auto_delete,
-                      model=template.model,
-                      res_id=res_id or False)
-
-        attachments = []
-        # Add report in attachments
-        if template.report_template:
-            report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
-            report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
-            # Ensure report is rendered using template's language
-            ctx = context.copy()
-            if template.lang:
-                ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)
-            service = netsvc.LocalService(report_service)
-            (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
+        res_ids_to_templates = self.get_email_template_batch(cr, uid, template_id, res_ids, context)
+
+        # templates: res_id -> template; template -> res_ids
+        templates_to_res_ids = {}
+        for res_id, template in res_ids_to_templates.iteritems():
+            templates_to_res_ids.setdefault(template, []).append(res_id)
+
+        results = dict()
+        for template, template_res_ids in templates_to_res_ids.iteritems():
+            # generate fields value for all res_ids linked to the current template
+            for field in fields:
+                generated_field_values = self.render_template_batch(
+                    cr, uid, getattr(template, field), template.model, template_res_ids,
+                    post_process=(field == 'body_html'),
+                    context=context)
+                for res_id, field_value in generated_field_values.iteritems():
+                    results.setdefault(res_id, dict())[field] = field_value
+            # update values for all res_ids
+            for res_id in template_res_ids:
+                values = results[res_id]
+                if 'body_html' in fields and template.user_signature:
+                    signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
+                    if signature:
+                        values['body_html'] = tools.append_content_to_html(values['body_html'], signature)
+                if values.get('body_html'):
+                    values['body'] = tools.html_sanitize(values['body_html'])
+                values.update(
+                    mail_server_id=template.mail_server_id.id or False,
+                    auto_delete=template.auto_delete,
+                    model=template.model,
+                    res_id=res_id or False,
+                    attachment_ids=[attach.id for attach in template.attachment_ids],
+                )
+
+            # Add report in attachments: generate once for all template_res_ids
+            if template.report_template:
+                for res_id in template_res_ids:
+                    attachments = []
+                    report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
+                    report_service = report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
+                    # Ensure report is rendered using template's language
+                    ctx = context.copy()
+                    if template.lang:
+                        ctx['lang'] = self.render_template_batch(cr, uid, template.lang, template.model, [res_id], context)[res_id]  # take 0 ?
+                    result, format = openerp.report.render_report(cr, uid, [res_id], report_service, {'model': template.model}, ctx)
             # TODO in trunk, change return format to binary to match message_post expected format
-            result = base64.b64encode(result)
-            if not report_name:
-                report_name = report_service
-            ext = "." + format
-            if not report_name.endswith(ext):
-                report_name += ext
-            attachments.append((report_name, result))
-
-        attachment_ids = []
-        # Add template attachments
-        for attach in template.attachment_ids:
-            attachment_ids.append(attach.id)
-
-        values['attachments'] = attachments
-        values['attachment_ids'] = attachment_ids
-        return values
-
-    def send_mail(self, cr, uid, template_id, res_id, force_send=False, context=None):
+                    result = base64.b64encode(result)
+                    if not report_name:
+                        report_name = 'report.' + report_service
+                    ext = "." + format
+                    if not report_name.endswith(ext):
+                        report_name += ext
+                    attachments.append((report_name, result))
+                    results[res_id]['attachments'] = attachments
+
+        return results
+
+    def send_mail(self, cr, uid, template_id, res_id, force_send=False, raise_exception=False, context=None):
         """Generates a new mail message for the given template and record,
            and schedules it for delivery through the ``mail`` module's scheduler.
 
@@ -380,15 +507,15 @@ class email_template(osv.osv):
         values = self.generate_email(cr, uid, template_id, res_id, context=context)
         if not values.get('email_from'):
             raise osv.except_osv(_('Warning!'),_("Sender email is missing or empty after template rendering. Specify one to deliver your message"))
-        # process email_recipients field that is a comma separated list of partner_ids -> recipient_ids
+        # process partner_to field that is a comma separated list of partner_ids -> recipient_ids
         # NOTE: only usable if force_send is True, because otherwise the value is
         # not stored on the mail_mail, and therefore lost -> fixed in v8
-        recipient_ids = []
-        email_recipients = values.pop('email_recipients', '')
-        if email_recipients:
-            for partner_id in email_recipients.split(','):
-                if partner_id:  # placeholders could generate '', 3, 2 due to some empty field values
-                    recipient_ids.append(int(partner_id))
+        values['recipient_ids'] = []
+        partner_to = values.pop('partner_to', '')
+        if partner_to:
+            # placeholders could generate '', 3, 2 due to some empty field values
+            tpl_partner_ids = [int(pid) for pid in partner_to.split(',') if pid]
+            values['recipient_ids'] += [(4, pid) for pid in self.pool['res.partner'].exists(cr, SUPERUSER_ID, tpl_partner_ids, context=context)]
 
         attachment_ids = values.pop('attachment_ids', [])
         attachments = values.pop('attachments', [])
@@ -411,7 +538,17 @@ class email_template(osv.osv):
             mail_mail.write(cr, uid, msg_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context)
 
         if force_send:
-            mail_mail.send(cr, uid, [msg_id], recipient_ids=recipient_ids, context=context)
+            mail_mail.send(cr, uid, [msg_id], raise_exception=raise_exception, context=context)
         return msg_id
 
+    # Compatibility method
+    def render_template(self, cr, uid, template, model, res_id, context=None):
+        return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id]
+
+    def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
+        return self.get_email_template_batch(cr, uid, template_id, [record_id], context)[record_id]
+
+    def generate_email(self, cr, uid, template_id, res_id, context=None):
+        return self.generate_email_batch(cr, uid, template_id, [res_id], context)[res_id]
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: