]*\shref=")(/[^/][^"]+)""", _sub_relative2absolute, html)
html = re.sub(r"""(<[^>]+\bstyle="[^"]+\burl\('?)(/[^/'][^'")]+)""", _sub_relative2absolute, html)
return html
@api.model
def _render_encapsulate(self, layout_xmlid, html, add_context=None, context_record=None):
try:
template = self.env.ref(layout_xmlid, raise_if_not_found=True)
except ValueError:
_logger.warning('QWeb template %s not found when rendering encapsulation template.' % (layout_xmlid))
else:
record_name = context_record.display_name if context_record else ''
model_description = self.env['ir.model']._get(context_record._name).display_name if context_record else False
template_ctx = {
'body': html,
'record_name': record_name,
'model_description': model_description,
'company': context_record['company_id'] if (context_record and 'company_id' in context_record) else self.env.company,
'record': context_record,
}
if add_context:
template_ctx.update(**add_context)
html = template._render(template_ctx, engine='ir.qweb', minimal_qcontext=True)
html = self.env['mail.render.mixin']._replace_local_links(html)
return html
@api.model
def _prepend_preview(self, html, preview):
""" Prepare the email body before sending. Add the text preview at the
beginning of the mail. The preview text is displayed bellow the mail
subject of most mail client (gmail, outlook...).
:param html: html content for which we want to prepend a preview
:param preview: the preview to add before the html content
:return: html with preprended preview
"""
if preview:
preview = preview.strip()
if preview:
html_preview = f"""
{tools.html_escape(preview)}
"""
return tools.prepend_html_content(html, html_preview)
return html
# ------------------------------------------------------------
# RENDERING
# ------------------------------------------------------------
@api.model
def _render_qweb_eval_context(self):
""" Prepare qweb evaluation context, containing for all rendering
* ``user``: current user browse record;
* ``ctx```: current context;
* various formatting tools;
"""
render_context = {
'format_date': lambda date, date_format=False, lang_code=False: format_date(self.env, date, date_format, lang_code),
'format_datetime': lambda dt, tz=False, dt_format=False, lang_code=False: format_datetime(self.env, dt, tz, dt_format, lang_code),
'format_amount': lambda amount, currency, lang_code=False: tools.format_amount(self.env, amount, currency, lang_code),
'format_duration': lambda value: tools.format_duration(value),
'user': self.env.user,
'ctx': self._context,
}
return render_context
@api.model
def _render_template_qweb(self, template_src, model, res_ids, add_context=None):
""" Render a QWeb template.
:param str template_src: source QWeb template. It should be a string
XmlID allowing to fetch an ir.ui.view;
:param str model: see ``MailRenderMixin._render_field)``;
:param list res_ids: see ``MailRenderMixin._render_field)``;
:param dict add_context: additional context to give to renderer. It
allows to add values to base rendering context generated by
``MailRenderMixin._render_qweb_eval_context()``;
:return dict: {res_id: string of rendered template based on record}
"""
view = self.env.ref(template_src, raise_if_not_found=False) or self.env['ir.ui.view']
results = dict.fromkeys(res_ids, u"")
if not view:
return results
# prepare template variables
variables = self._render_qweb_eval_context()
if add_context:
variables.update(**add_context)
for record in self.env[model].browse(res_ids):
variables['object'] = record
try:
render_result = view._render(variables, engine='ir.qweb', minimal_qcontext=True)
except Exception as e:
_logger.info("Failed to render template : %s (%d)" % (template_src, view.id), exc_info=True)
raise UserError(_("Failed to render template : %s (%d)", template_src, view.id))
results[record.id] = render_result
return results
@api.model
def _render_jinja_eval_context(self):
""" Prepare jinja evaluation context, containing for all rendering
* ``user``: current user browse record;
* ``ctx```: current context, named ctx to avoid clash with jinja
internals that already uses context;
* various formatting tools;
"""
render_context = {
'format_date': lambda date, date_format=False, lang_code=False: format_date(self.env, date, date_format, lang_code),
'format_datetime': lambda dt, tz=False, dt_format=False, lang_code=False: format_datetime(self.env, dt, tz, dt_format, lang_code),
'format_amount': lambda amount, currency, lang_code=False: tools.format_amount(self.env, amount, currency, lang_code),
'format_duration': lambda value: tools.format_duration(value),
'user': self.env.user,
'ctx': self._context,
}
return render_context
@api.model
def _render_template_jinja(self, template_txt, model, res_ids, add_context=None):
""" Render a string-based template on records given by a model and a list
of IDs, using jinja.
In addition to the generic evaluation context given by _render_jinja_eval_context
some new variables are added, depending on each record
* ``object``: record based on which the template is rendered;
:param str template_txt: template text to render
:param str model: model name of records on which we want to perform rendering
:param list res_ids: list of ids of records (all belonging to same model)
:return dict: {res_id: string of rendered template based on record}
"""
# TDE FIXME: remove that brol (6dde919bb9850912f618b561cd2141bffe41340c)
no_autoescape = self._context.get('safe')
results = dict.fromkeys(res_ids, u"")
if not template_txt:
return results
# try to load the template
try:
jinja_env = jinja_safe_template_env if no_autoescape else jinja_template_env
template = jinja_env.from_string(tools.ustr(template_txt))
except Exception:
_logger.info("Failed to load template %r", template_txt, exc_info=True)
return results
# prepare template variables
variables = self._render_jinja_eval_context()
if add_context:
variables.update(**add_context)
safe_eval.check_values(variables)
# TDE CHECKME
# records = self.env[model].browse(it for it in res_ids if it) # filter to avoid browsing [None]
if any(r is None for r in res_ids):
raise ValueError(_('Unsuspected None'))
for record in self.env[model].browse(res_ids):
variables['object'] = record
try:
render_result = template.render(variables)
except Exception as e:
_logger.info("Failed to render template : %s" % e, exc_info=True)
raise UserError(_("Failed to render template : %s", e))
if render_result == u"False":
render_result = u""
results[record.id] = render_result
return results
@api.model
def _render_template_postprocess(self, rendered):
""" Tool method for post processing. In this method we ensure local
links ('/shop/Basil-1') are replaced by global links ('https://www.
mygardin.com/hop/Basil-1').
:param rendered: result of ``_render_template``
:return dict: updated version of rendered
"""
for res_id, html in rendered.items():
rendered[res_id] = self._replace_local_links(html)
return rendered
@api.model
def _render_template(self, template_src, model, res_ids, engine='jinja', add_context=None, post_process=False):
""" Render the given string on records designed by model / res_ids using
the given rendering engine. Currently only jinja or qweb are supported.
:param str template_src: template text to render (jinja) or xml id of view (qweb)
this could be cleaned but hey, we are in a rush
:param str model: model name of records on which we want to perform rendering
:param list res_ids: list of ids of records (all belonging to same model)
:param string engine: jinja
:param post_process: see ``MailRenderMixin._render_field``;
:return dict: {res_id: string of rendered template based on record}
"""
if not isinstance(res_ids, (list, tuple)):
raise ValueError(_('Template rendering should be called only using on a list of IDs.'))
if engine not in ('jinja', 'qweb'):
raise ValueError(_('Template rendering supports only jinja or qweb.'))
if engine == 'qweb':
rendered = self._render_template_qweb(template_src, model, res_ids, add_context=add_context)
else:
rendered = self._render_template_jinja(template_src, model, res_ids, add_context=add_context)
if post_process:
rendered = self._render_template_postprocess(rendered)
return rendered
def _render_lang(self, res_ids):
""" Given some record ids, return the lang for each record based on
lang field of template or through specific context-based key.
:param list res_ids: list of ids of records (all belonging to same model
defined by self.model)
:return dict: {res_id: lang code (i.e. en_US)}
"""
self.ensure_one()
if not isinstance(res_ids, (list, tuple)):
raise ValueError(_('Template rendering for language should be called with a list of IDs.'))
if self.env.context.get('template_preview_lang'):
return dict((res_id, self.env.context['template_preview_lang']) for res_id in res_ids)
else:
rendered_langs = self._render_template(self.lang, self.model, res_ids)
return dict((res_id, lang)
for res_id, lang in rendered_langs.items())
def _classify_per_lang(self, res_ids):
""" Given some record ids, return for computed each lang a contextualized
template and its subset of res_ids.
:param list res_ids: list of ids of records (all belonging to same model
defined by self.model)
:return dict: {lang: (template with lang=lang_code if specific lang computed
or template, res_ids targeted by that language}
"""
self.ensure_one()
lang_to_res_ids = {}
for res_id, lang in self._render_lang(res_ids).items():
lang_to_res_ids.setdefault(lang, []).append(res_id)
return dict(
(lang, (self.with_context(lang=lang) if lang else self, lang_res_ids))
for lang, lang_res_ids in lang_to_res_ids.items()
)
def _render_field(self, field, res_ids,
compute_lang=False, set_lang=False,
post_process=False):
""" Given some record ids, render a template located on field on all
records. ``field`` should be a field of self (i.e. ``body_html`` on
``mail.template``). res_ids are record IDs linked to ``model`` field
on self.
:param list res_ids: list of ids of records (all belonging to same model
defined by ``self.model``)
:param boolean compute_lang: compute language to render on translated
version of the template instead of default (probably english) one.
Language will be computed based on ``self.lang``;
:param string set_lang: force language for rendering. It should be a
valid lang code matching an activate res.lang. Checked only if
``compute_lang`` is False;
:param boolean post_process: perform a post processing on rendered result
(notably html links management). See``_render_template_postprocess``);
:return dict: {res_id: string of rendered template based on record}
"""
self.ensure_one()
if compute_lang:
templates_res_ids = self._classify_per_lang(res_ids)
elif set_lang:
templates_res_ids = {set_lang: (self.with_context(lang=set_lang), res_ids)}
else:
templates_res_ids = {self._context.get('lang'): (self, res_ids)}
return dict(
(res_id, rendered)
for lang, (template, tpl_res_ids) in templates_res_ids.items()
for res_id, rendered in template._render_template(
template[field], template.model, tpl_res_ids,
post_process=post_process
).items()
)