summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorryv-odoo <ryv@odoo.com>2019-10-11 14:39:25 +0000
committerThibault Delavallée <tde@odoo.com>2019-12-02 10:51:02 +0000
commit47ff160b422ac317a5cdca19edfba42c4889656d (patch)
tree1658c666e1c8a2e1fd895c9a073795d632e38b50 /addons
parent2b4e172c6f686c0c577d4a1b1d99b65b721ed900 (diff)
[REF] mail: improve and refactor mail template preview
PURPOSE Improve mail template preview wizard by improve its views and displaying more rendered fields. It helps users knowing what will be sent to customers. SPECIFICATIONS Rename template model to mail_template_preview to better match the mail template namespace. Remove _inherit on mail.template and instead choose fields to compute and display. Indeed lot of fields are unnecessary or badly computed. Instead just add fields on the wizard that depends on mail template and will be computed based on mail_template_id field of wizard. Improve error management. LINKS Task 2085912 PR #38708 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Diffstat (limited to 'addons')
-rw-r--r--addons/mail/__manifest__.py2
-rw-r--r--addons/mail/views/mail_template_views.xml5
-rw-r--r--addons/mail/wizard/__init__.py2
-rw-r--r--addons/mail/wizard/email_template_preview.py58
-rw-r--r--addons/mail/wizard/email_template_preview_view.xml51
-rw-r--r--addons/mail/wizard/mail_template_preview.py86
-rw-r--r--addons/mail/wizard/mail_template_preview_views.xml61
7 files changed, 151 insertions, 114 deletions
diff --git a/addons/mail/__manifest__.py b/addons/mail/__manifest__.py
index 632771a25ff..5dc0f4425f1 100644
--- a/addons/mail/__manifest__.py
+++ b/addons/mail/__manifest__.py
@@ -14,6 +14,7 @@
'wizard/mail_compose_message_view.xml',
'wizard/mail_resend_cancel_views.xml',
'wizard/mail_resend_message_views.xml',
+ 'wizard/mail_template_preview_views.xml',
'views/mail_message_subtype_views.xml',
'views/mail_tracking_views.xml',
'views/mail_notification_views.xml',
@@ -34,7 +35,6 @@
'views/mail_alias_views.xml',
'views/res_users_views.xml',
'views/mail_templates.xml',
- 'wizard/email_template_preview_view.xml',
'views/mail_template_views.xml',
'views/ir_actions_views.xml',
'views/ir_model_views.xml',
diff --git a/addons/mail/views/mail_template_views.xml b/addons/mail/views/mail_template_views.xml
index 111a633d092..5e8ddf21d0b 100644
--- a/addons/mail/views/mail_template_views.xml
+++ b/addons/mail/views/mail_template_views.xml
@@ -29,9 +29,8 @@
<span class="o_stat_text">Context Action</span>
</div>
</button>
- <button class="oe_stat_button" name="%(wizard_email_template_preview)d" icon="fa-search-plus" string="Preview"
- type="action" target="new"
- context="{'template_id':active_id}"/>
+ <button class="oe_stat_button" name="%(mail_template_preview_action)d" icon="fa-search-plus" string="Preview"
+ type="action" target="new"/>
</div>
<div class="oe_title">
<label for="name" class="oe_edit_only"/>
diff --git a/addons/mail/wizard/__init__.py b/addons/mail/wizard/__init__.py
index 7e0df4a2d19..ee0fd32ca8a 100644
--- a/addons/mail/wizard/__init__.py
+++ b/addons/mail/wizard/__init__.py
@@ -5,6 +5,6 @@ from . import invite
from . import mail_compose_message
from . import mail_resend_cancel
from . import mail_resend_message
-from . import email_template_preview
+from . import mail_template_preview
from . import base_module_uninstall
from . import base_partner_merge
diff --git a/addons/mail/wizard/email_template_preview.py b/addons/mail/wizard/email_template_preview.py
deleted file mode 100644
index 57e0b4d3fec..00000000000
--- a/addons/mail/wizard/email_template_preview.py
+++ /dev/null
@@ -1,58 +0,0 @@
-# -*- coding: utf-8 -*-
-# Part of Odoo. See LICENSE file for full copyright and licensing details.
-
-from odoo import api, fields, models
-
-
-class TemplatePreview(models.TransientModel):
- _inherit = "mail.template"
- _name = "email_template.preview"
- _description = "Email Template Preview"
-
- @api.model
- def _get_records(self):
- """ Return Records of particular Email Template's Model """
- template_id = self._context.get('template_id')
- default_res_id = self._context.get('default_res_id')
- if not template_id:
- return []
- template = self.env['mail.template'].browse(int(template_id))
- records = self.env[template.model_id.model].search([], order="id desc", limit=10)
- 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 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', 'preview_lang')
- def on_change_res_id(self):
- if not self.res_id:
- return {}
- mail_values = {}
- if self._context.get('template_id'):
- template = self.env['mail.template'].browse(self._context['template_id'])
- self.name = template.name
- mail_values = template.with_context(template_preview_lang=self.preview_lang).generate_email(
- self.res_id, ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'scheduled_date'])
- 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
deleted file mode 100644
index bc3776b24ff..00000000000
--- a/addons/mail/wizard/email_template_preview_view.xml
+++ /dev/null
@@ -1,51 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<odoo>
- <data>
- <!-- Email Template Preview -->
- <record model="ir.ui.view" id="email_template_preview_form">
- <field name="name">email_template.preview.form</field>
- <field name="model">email_template.preview</field>
- <field name="arch" type="xml">
- <form string="Email Preview">
- <field name="model_id" invisible="1"/>
- <h3>Preview of <field name="name" readonly="1" nolabel="1" class="oe_inline"/></h3>
- Choose an example <field name="model_id" class="oe_inline" readonly="1"/> record:
- <field name="res_id" class="oe_inline" style="margin-left: 8px;"/>
- <p>Choose a language: <field name="preview_lang" class="oe_inline ml8"/></p>
- <group>
- <field name="subject" readonly="1"/>
- <field name="email_from" readonly="1"
- attrs="{'invisible':[('email_from','=',False)]}"/>
- <field name="partner_ids" widget="many2many_tags" readonly="1"/>
- <field name="email_to" readonly="1"
- attrs="{'invisible':[('email_to','=',False)]}"/>
- <field name="email_cc" readonly="1"
- attrs="{'invisible':[('email_cc','=',False)]}"/>
- <field name="reply_to" readonly="1"
- attrs="{'invisible':[('reply_to','=',False)]}"/>
- <field name="scheduled_date" readonly="1"
- attrs="{'invisible':[('scheduled_date','=',False)]}"/>
- </group>
- <field name="body_html" widget="html" readonly="1"
- nolabel="1" options='{"safe": True}'/>
- <field name="attachment_ids" widget="many2many_binary" readonly="1"/>
- <footer>
- <button string="Discard" class="btn-secondary" special="cancel"/>
- </footer>
- </form>
- </field>
- </record>
-
- <record id="wizard_email_template_preview" model="ir.actions.act_window">
- <field name="name">Template Preview</field>
- <field name="res_model">email_template.preview</field>
- <field name="binding_model_id" eval="False"/>
- <field name="type">ir.actions.act_window</field>
- <field name="view_mode">form</field>
- <field name="view_id" ref="email_template_preview_form"/>
- <field name="target">new</field>
- <field name="context">{'template_id':active_id}</field>
- </record>
-
- </data>
-</odoo>
diff --git a/addons/mail/wizard/mail_template_preview.py b/addons/mail/wizard/mail_template_preview.py
new file mode 100644
index 00000000000..7cedde9b5e5
--- /dev/null
+++ b/addons/mail/wizard/mail_template_preview.py
@@ -0,0 +1,86 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models
+from odoo.exceptions import UserError
+
+
+class MailTemplatePreview(models.TransientModel):
+ _name = 'mail.template.preview'
+ _description = 'Email Template Preview'
+ _MAIL_TEMPLATE_FIELDS = ['subject', 'body_html', 'email_from', 'email_to',
+ 'email_cc', 'reply_to', 'scheduled_date', 'attachment_ids']
+
+ @api.model
+ def _selection_target_model(self):
+ return [(model.model, model.name) for model in self.env['ir.model'].search([])]
+
+ @api.model
+ def _selection_languages(self):
+ return self.env['res.lang'].get_installed()
+
+ @api.model
+ def default_get(self, fields):
+ result = super(MailTemplatePreview, self).default_get(fields)
+ if not result.get('mail_template_id') or 'resource_ref' not in fields:
+ return result
+ mail_template = self.env['mail.template'].browse(result['mail_template_id'])
+ res = self.env[mail_template.model_id.model].search([], limit=1)
+ if res:
+ result['resource_ref'] = '%s,%s' % (mail_template.model_id.model, res.id)
+ return result
+
+ mail_template_id = fields.Many2one('mail.template', string='Related Mail Template', required=True)
+ model_id = fields.Many2one('ir.model', string='Targeted model', related="mail_template_id.model_id")
+ resource_ref = fields.Reference(string='Record', selection='_selection_target_model')
+ lang = fields.Selection(_selection_languages, string='Template Preview Language')
+ no_record = fields.Boolean('No Record', compute='_compute_no_record')
+ error_msg = fields.Char('Error Message', readonly=True)
+ # Fields same than the mail.template model, computed with resource_ref and lang
+ subject = fields.Char('Subject', compute='_compute_mail_template_fields')
+ email_from = fields.Char('From', compute='_compute_mail_template_fields', help="Sender address")
+ email_to = fields.Char('To', compute='_compute_mail_template_fields',
+ help="Comma-separated recipient addresses")
+ email_cc = fields.Char('Cc', compute='_compute_mail_template_fields', help="Carbon copy recipients")
+ reply_to = fields.Char('Reply-To', compute='_compute_mail_template_fields', help="Preferred response address")
+ scheduled_date = fields.Char('Scheduled Date', compute='_compute_mail_template_fields',
+ help="The queue manager will send the email after the date")
+ body_html = fields.Html('Body', compute='_compute_mail_template_fields', sanitize=False)
+ attachment_ids = fields.Many2many('ir.attachment', 'Attachments', compute='_compute_mail_template_fields')
+ # Extra fields info generated by generate_email
+ partner_ids = fields.Many2many('res.partner', string='Recipients', compute='_compute_mail_template_fields')
+
+ @api.depends('model_id')
+ def _compute_no_record(self):
+ for preview in self:
+ preview.no_record = (self.env[preview.model_id.model].search_count([]) == 0) if preview.model_id else True
+
+ @api.depends('lang', 'resource_ref')
+ def _compute_mail_template_fields(self):
+ """ Preview the mail template (body, subject, ...) depending of the language and
+ the record reference, more precisely the record id for the defined model of the mail template.
+ If no record id is selectable/set, the jinja placeholders won't be replace in the display information. """
+ copy_depends_values = {'lang': self.lang}
+ mail_template = self.mail_template_id.with_context(lang=self.lang)
+ try:
+ if not self.resource_ref:
+ self._set_mail_attributes()
+ else:
+ copy_depends_values['resource_ref'] = '%s,%s' % (self.resource_ref._name, self.resource_ref.id)
+ mail_values = mail_template.with_context(template_preview_lang=self.lang).generate_email(
+ self.resource_ref.id, self._MAIL_TEMPLATE_FIELDS)
+ self._set_mail_attributes(values=mail_values)
+ self.error_msg = False
+ except UserError as user_error:
+ self._set_mail_attributes()
+ self.error_msg = user_error.name
+ finally:
+ # Avoid to be change by a invalidate_cache call (in generate_mail), e.g. Quotation / Order report
+ for key, value in copy_depends_values.items():
+ self[key] = value
+
+ def _set_mail_attributes(self, values=None):
+ for field in self._MAIL_TEMPLATE_FIELDS:
+ field_value = values.get(field, False) if values else self.mail_template_id[field]
+ self[field] = field_value
+ self.partner_ids = values.get('partner_ids', False) if values else False
diff --git a/addons/mail/wizard/mail_template_preview_views.xml b/addons/mail/wizard/mail_template_preview_views.xml
new file mode 100644
index 00000000000..ab3d0bd55ed
--- /dev/null
+++ b/addons/mail/wizard/mail_template_preview_views.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+ <data>
+ <record id="mail_template_preview_view_form" model="ir.ui.view">
+ <field name="name">mail.template.preview.view.form</field>
+ <field name="model">mail.template.preview</field>
+ <field name="arch" type="xml">
+ <form string="Email Preview">
+ <h3>Preview of <field name="mail_template_id" readonly="1" nolabel="1" options="{'no_open' : True}"/></h3>
+ <div class="alert alert-danger" role="alert" attrs="{'invisible' : [('error_msg', '=', False)]}">
+ <field name="error_msg" />
+ </div>
+ <field name="no_record" invisible="1"/>
+ <div class="container">
+ <div class="row">
+ <span class="col-md-5 col-lg-4 col-sm-12 pl-0">Choose an example <field name="model_id" readonly="1"/> record:</span>
+ <div class="col-md-7 col-lg-6 col-sm-12 pl-0">
+ <field name="resource_ref" readonly="False"
+ options="{'hide_model': True, 'no_create': True, 'no_edit': True, 'no_open': True}"
+ attrs="{'invisible': [('no_record', '=', True)]}"/>
+ <b attrs="{'invisible': [('no_record', '=', False)]}" class="text-warning">No record for this model</b>
+ </div>
+ </div>
+ <div class="row">
+ <span class="col-md-5 col-lg-4 col-sm-12 pl-0">Force a language: </span>
+ <div class="col-md-7 col-lg-6 col-sm-12 pl-0">
+ <field name="lang"/>
+ </div>
+ </div>
+ </div>
+ <group>
+ <field name="subject"/>
+ <field name="email_from" attrs="{'invisible':[('email_from','=', False)]}"/>
+ <field name="partner_ids" widget="many2many_tags" attrs="{'invisible':[('partner_ids', '=', [])]}"/>
+ <field name="email_to" attrs="{'invisible':[('email_to','=', False)]}"/>
+ <field name="email_cc" attrs="{'invisible':[('email_cc','=', False)]}"/>
+ <field name="reply_to" attrs="{'invisible':[('reply_to','=', False)]}"/>
+ <field name="scheduled_date" attrs="{'invisible':[('scheduled_date','=', False)]}"/>
+ </group>
+ <field name="body_html" widget="html" nolabel="1" options='{"safe": True}'/>
+ <field name="attachment_ids" widget="many2many_binary"/>
+ <footer>
+ <button string="Close" class="btn-secondary" special="cancel"/>
+ </footer>
+ </form>
+ </field>
+ </record>
+
+ <record id="mail_template_preview_action" model="ir.actions.act_window">
+ <field name="name">Template Preview</field>
+ <field name="res_model">mail.template.preview</field>
+ <field name="binding_model_id" eval="False"/>
+ <field name="type">ir.actions.act_window</field>
+ <field name="view_mode">form</field>
+ <field name="view_id" ref="mail_template_preview_view_form"/>
+ <field name="target">new</field>
+ <field name="context">{'default_mail_template_id':active_id}</field>
+ </record>
+
+ </data>
+</odoo>