From eda806a40e3ee1f7aaf8ba7cb3624892b0d07c25 Mon Sep 17 00:00:00 2001 From: Axay Javiya Date: Fri, 18 Aug 2017 07:13:35 +0000 Subject: [PATCH] [IMP] mail: add an option to preview templates in a specific language Purpose: allow to check email preview in a specific language in order to see the result in a given translation context. Specifications * added preview_lang field to change language of template * set default language from the template by evaluating expression if lang field of template is set, This commit is related to task ID: 30245 closes odoo/odoo#19165 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_template.py | 19 ++++++++++++------- addons/mail/wizard/email_template_preview.py | 16 ++++++++++++---- .../wizard/email_template_preview_view.xml | 1 + 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index 7ed1b6a53ea..53dd31f5264 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -360,13 +360,18 @@ class MailTemplate(models.Model): return results self.ensure_one() - 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) - else: - template = self - results[res_id] = template + if self.env.context.get('template_preview_lang'): + lang = self.env.context.get('template_preview_lang') + for res_id in res_ids: + results[res_id] = self.with_context(lang=lang) + else: + 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) + else: + template = self + results[res_id] = template return multi_mode and results or results[res_ids[0]] diff --git a/addons/mail/wizard/email_template_preview.py b/addons/mail/wizard/email_template_preview.py index eb02b082b4e..95b9f9208c5 100644 --- a/addons/mail/wizard/email_template_preview.py +++ b/addons/mail/wizard/email_template_preview.py @@ -21,22 +21,30 @@ class TemplatePreview(models.TransientModel): records |= records.browse(default_res_id) return records.name_get() + @api.model + def _get_languages(self): + return self.env['res.lang'].get_installed() + @api.model def default_get(self, fields): result = super(TemplatePreview, self).default_get(fields) + template = self._context.get('template_id') and self.env['mail.template'].browse(self._context['template_id']) or False if 'res_id' in fields and not result.get('res_id'): records = self._get_records() result['res_id'] = records and records[0][0] or False # select first record as a Default - if self._context.get('template_id') and 'model_id' in fields and not result.get('model_id'): - result['model_id'] = self.env['mail.template'].browse(self._context['template_id']).model_id.id + if template and 'model_id' in fields and not result.get('model_id'): + result['model_id'] = template.model_id.id + if template and 'preview_lang' in fields and not result.get('preview_lang') and result.get('res_id'): + result['preview_lang'] = template.lang and template.generate_email(result['res_id'], ['lang'])['lang'] or template._context.get('lang') return result res_id = fields.Selection(_get_records, 'Sample Document') partner_ids = fields.Many2many('res.partner', string='Recipients') attachment_ids = fields.Many2many(string='Attachments', store=False) + preview_lang = fields.Selection(_get_languages, string='Template Preview Language') - @api.onchange('res_id') + @api.onchange('res_id', 'preview_lang') @api.multi def on_change_res_id(self): if not self.res_id: @@ -45,6 +53,6 @@ class TemplatePreview(models.TransientModel): if self._context.get('template_id'): template = self.env['mail.template'].browse(self._context['template_id']) self.name = template.name - mail_values = template.generate_email(self.res_id) + mail_values = template.with_context(template_preview_lang=self.preview_lang).generate_email(self.res_id) for field in ['email_from', 'email_to', 'email_cc', 'reply_to', 'subject', 'body_html', 'partner_to', 'partner_ids', 'attachment_ids']: setattr(self, field, mail_values.get(field, False)) diff --git a/addons/mail/wizard/email_template_preview_view.xml b/addons/mail/wizard/email_template_preview_view.xml index bbf2e1811da..3b3b52e9067 100644 --- a/addons/mail/wizard/email_template_preview_view.xml +++ b/addons/mail/wizard/email_template_preview_view.xml @@ -11,6 +11,7 @@

Preview of

Choose an example record: +

Choose a language: