Currently ``mail.render.mixin`` offers rendering tools, some of them being
based on a ``model`` field. It allows to know which model to use to fetch
records on which we perform rendering. However this field is not defined at
mixin level but in inheriting models without being clearly implemented that
way (see ``mail.template`` or ``sms.template`` models).
In order to clean this mixin it is now defined at mixin level, using a
not stored computed field allowing to define how to find this model. Sub
models are updated accordingly.
Other cleaning is done in the render mixin
* rename ``_render_template_qweb`` to ``_render_template_qweb_view``
to indicate it works on views, not on raw qweb templates;
* extract some common available variables for rendering in a method then
called / upated for jinja and qweb views;
* correctly set same rendering context for jinja and qweb views rendering;
* allow to propagate an additional context from _render_field to sub
rendering methods;
* allow to propagate options through rendering methods (notably for escaping
or safe attributes in jinja);
* allow to specify engine used to render lang;
* clean or update some docstrings;
Some tests are also added, as render mixin lacks some more detailed test to
ensure various use cases will be correctly migrated to other engines like
QWeb.
Task ID-2534550 (Template usage improvement)
Task ID-2477164 (Composer mixin)
Prepares Task ID-27033 (QWeb in templates)
COM PR odoo/odoo#70889
ENT PR odoo/enterprise#18352
Co-Authored-By: Stéphane Debauche <std@odoo.com>
Co-Authored-By: Thibault Delavallée <tde@odoo.com>
77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
|
|
|
|
class SMSTemplate(models.Model):
|
|
"Templates for sending SMS"
|
|
_name = "sms.template"
|
|
_inherit = ['mail.render.mixin']
|
|
_description = 'SMS Templates'
|
|
|
|
@api.model
|
|
def default_get(self, fields):
|
|
res = super(SMSTemplate, self).default_get(fields)
|
|
if not fields or 'model_id' in fields and not res.get('model_id') and res.get('model'):
|
|
res['model_id'] = self.env['ir.model']._get(res['model']).id
|
|
return res
|
|
|
|
name = fields.Char('Name', translate=True)
|
|
model_id = fields.Many2one(
|
|
'ir.model', string='Applies to', required=True,
|
|
domain=['&', ('is_mail_thread_sms', '=', True), ('transient', '=', False)],
|
|
help="The type of document this template can be used with", ondelete='cascade')
|
|
model = fields.Char('Related Document Model', related='model_id.model', index=True, store=True, readonly=True)
|
|
body = fields.Char('Body', translate=True, required=True)
|
|
# Use to create contextual action (same as for email template)
|
|
sidebar_action_id = fields.Many2one('ir.actions.act_window', 'Sidebar action', readonly=True, copy=False,
|
|
help="Sidebar action to make this template available on records "
|
|
"of the related document model")
|
|
|
|
# Overrides of mail.render.mixin
|
|
@api.depends('model')
|
|
def _compute_render_model(self):
|
|
for template in self:
|
|
template.render_model = template.model
|
|
|
|
# ------------------------------------------------------------
|
|
# CRUD
|
|
# ------------------------------------------------------------
|
|
|
|
@api.returns('self', lambda value: value.id)
|
|
def copy(self, default=None):
|
|
default = dict(default or {},
|
|
name=_("%s (copy)", self.name))
|
|
return super(SMSTemplate, self).copy(default=default)
|
|
|
|
def unlink(self):
|
|
self.sudo().mapped('sidebar_action_id').unlink()
|
|
return super(SMSTemplate, self).unlink()
|
|
|
|
def action_create_sidebar_action(self):
|
|
ActWindow = self.env['ir.actions.act_window']
|
|
view = self.env.ref('sms.sms_composer_view_form')
|
|
|
|
for template in self:
|
|
button_name = _('Send SMS (%s)', template.name)
|
|
action = ActWindow.create({
|
|
'name': button_name,
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'sms.composer',
|
|
# Add default_composition_mode to guess to determine if need to use mass or comment composer
|
|
'context': "{'default_template_id' : %d, 'sms_composition_mode': 'guess', 'default_res_ids': active_ids, 'default_res_id': active_id}" % (template.id),
|
|
'view_mode': 'form',
|
|
'view_id': view.id,
|
|
'target': 'new',
|
|
'binding_model_id': template.model_id.id,
|
|
})
|
|
template.write({'sidebar_action_id': action.id})
|
|
return True
|
|
|
|
def action_unlink_sidebar_action(self):
|
|
for template in self:
|
|
if template.sidebar_action_id:
|
|
template.sidebar_action_id.unlink()
|
|
return True
|