[IMP] mail: Make mail.compose.message code more readable

Purpose
=======

Remove the method 'render_template' in mail.compose.message as the indirection
is not useful. Call the method on the correct model (mail.template) directly.
This commit is contained in:
Yannick Tivisse
2018-10-02 12:35:34 +02:00
parent b2b9545cf3
commit 532d82a7a9
7 changed files with 15 additions and 19 deletions
+1 -1
View File
@@ -135,7 +135,7 @@ class Digest(models.Model):
return False
tip.user_ids = [4, user.id]
body = tools.html_sanitize(tip.tip_description)
tip_description = self.env['mail.template'].render_template(body, 'digest.tip', self.id)
tip_description = self.env['mail.template']._render_template(body, 'digest.tip', self.id)
return tip_description
def compute_kpis_actions(self, company, user):
+2 -2
View File
@@ -545,7 +545,7 @@ class Challenge(models.Model):
if challenge.visibility_mode == 'ranking':
lines_boards = challenge._get_serialized_challenge_lines(restrict_goals=subset_goals)
body_html = MailTemplates.with_context(challenge_lines=lines_boards).render_template(challenge.report_template_id.body_html, 'gamification.challenge', challenge.id)
body_html = MailTemplates.with_context(challenge_lines=lines_boards)._render_template(challenge.report_template_id.body_html, 'gamification.challenge', challenge.id)
# send to every follower and participant of the challenge
challenge.message_post(
@@ -566,7 +566,7 @@ class Challenge(models.Model):
if not lines:
continue
body_html = MailTemplates.sudo(user).with_context(challenge_lines=lines).render_template(
body_html = MailTemplates.sudo(user).with_context(challenge_lines=lines)._render_template(
challenge.report_template_id.body_html,
'gamification.challenge',
challenge.id)
+1 -1
View File
@@ -221,7 +221,7 @@ class Goal(models.Model):
template = self.env.ref('gamification.email_template_goal_reminder')\
.get_email_template(self.id)
body_html = self.env['mail.template'].with_context(template._context)\
.render_template(template.body_html, 'gamification.goal', self.id)
._render_template(template.body_html, 'gamification.goal', self.id)
self.env['mail.thread'].message_post(
body=body_html,
partner_ids=[self.user_id.partner_id.id],
+1 -1
View File
@@ -32,7 +32,7 @@ class MailTemplate(models.Model):
if related_model._name == 'account.invoice' and related_model.l10n_ch_isr_valid:
#We add an attachment containing the ISR
template = res_ids_to_templates[res_id]
report_name = 'ISR-' + self.render_template(template.report_name, template.model, res_id) + '.pdf'
report_name = 'ISR-' + self._render_template(template.report_name, template.model, res_id) + '.pdf'
pdf = self.env.ref('l10n_ch.l10n_ch_isr_report').render_qweb_pdf([res_id])[0]
pdf = base64.b64encode(pdf)
+4 -4
View File
@@ -289,7 +289,7 @@ class MailTemplate(models.Model):
return html
@api.model
def render_template(self, template_txt, model, res_ids, post_process=False):
def _render_template(self, template_txt, model, res_ids, post_process=False):
""" Render the given template text, replace mako expressions ``${expr}``
with the result of evaluating these expressions with an evaluation
context containing:
@@ -361,7 +361,7 @@ class MailTemplate(models.Model):
return results
self.ensure_one()
langs = self.render_template(self.lang, self.model, res_ids)
langs = self._render_template(self.lang, self.model, res_ids)
for res_id, lang in langs.items():
if lang:
template = self.with_context(lang=lang)
@@ -434,7 +434,7 @@ class MailTemplate(models.Model):
Template = Template.with_context(lang=template._context.get('lang'))
for field in fields:
Template = Template.with_context(safe=field in {'subject'})
generated_field_values = Template.render_template(
generated_field_values = Template._render_template(
getattr(template, field), template.model, template_res_ids,
post_process=(field == 'body_html'))
for res_id, field_value in generated_field_values.items():
@@ -465,7 +465,7 @@ class MailTemplate(models.Model):
if template.report_template:
for res_id in template_res_ids:
attachments = []
report_name = self.render_template(template.report_name, template.model, res_id)
report_name = self._render_template(template.report_name, template.model, res_id)
report = template.report_template
report_service = report.report_name
+4 -8
View File
@@ -458,10 +458,10 @@ class MailComposer(models.TransientModel):
multi_mode = False
res_ids = [res_ids]
subjects = self.render_template(self.subject, self.model, res_ids)
bodies = self.render_template(self.body, self.model, res_ids, post_process=True)
emails_from = self.render_template(self.email_from, self.model, res_ids)
replies_to = self.render_template(self.reply_to, self.model, res_ids)
subjects = self.env['mail.template']._render_template(self.subject, self.model, res_ids)
bodies = self.env['mail.template']._render_template(self.body, self.model, res_ids, post_process=True)
emails_from = self.env['mail.template']._render_template(self.email_from, self.model, res_ids)
replies_to = self.env['mail.template']._render_template(self.reply_to, self.model, res_ids)
default_recipients = {}
if not self.partner_ids:
default_recipients = self.env['mail.thread'].message_get_default_recipients(res_model=self.model, res_ids=res_ids)
@@ -520,7 +520,3 @@ class MailComposer(models.TransientModel):
values[res_id] = res_id_values
return multi_mode and values or values[res_ids[0]]
@api.model
def render_template(self, template, model, res_ids, post_process=False):
return self.env['mail.template'].render_template(template, model, res_ids, post_process=post_process)
+2 -2
View File
@@ -492,8 +492,8 @@ class Slide(models.Model):
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
for slide in self.filtered(lambda slide: slide.website_published and slide.channel_id.publish_template_id):
publish_template = slide.channel_id.publish_template_id
html_body = publish_template.with_context(base_url=base_url).render_template(publish_template.body_html, 'slide.slide', slide.id)
subject = publish_template.render_template(publish_template.subject, 'slide.slide', slide.id)
html_body = publish_template.with_context(base_url=base_url)._render_template(publish_template.body_html, 'slide.slide', slide.id)
subject = publish_template._render_template(publish_template.subject, 'slide.slide', slide.id)
slide.channel_id.message_post(
subject=subject,
body=html_body,