Jinja as a templating engine was problematic in differents respect:
- introduce external dependency to Odoo (less controll)
- add another templating mechanism in the stack
- specific feature in qweb cannot be reused
- difficulty in rendering easily editable templates
- more knowledge required with no betterment
By replacing jinja with qweb we can now build tools to edit a qweb
that will work with the previously jinja encoded document
(essentially `mail.template` records).
There is a catch however. Some email fields (eg. email_to) used jinja
syntax for rendering dynamic variables (ie. ${object.something} and
${object.something_that_should_not_be_escaped | safe}).
We still want user to use dynamic variables for some char fields (eg.
subject, from, to, ...). We made a new rendering engine called
"inline_template" that will render an expression enclosed by `{{` and
`}}`.
To be able to edit the templates from the backend interface, a
plugin to the Odoo editor has been made for seamlessly edit the
document.
This qweb plugin includes:
- make dynamic variables (eg. `<t t-out="variable"/>`) not editable
(for preventing the user to shoot himself in the foot)
- group and hide related logical branching (ie. t-if, t-elif, and t-else)
in order to see only one at once
- a floating select input to switch visibility of a particular logical
branching
Task-27033
X-original-commit: odoo/odoo@68182baff4
Part-of: odoo/odoo#77377
67 lines
3.0 KiB
Python
67 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import fields, models, _
|
|
from odoo.addons.phone_validation.tools import phone_validation
|
|
|
|
|
|
class MassSMSTest(models.TransientModel):
|
|
_name = 'mailing.sms.test'
|
|
_description = 'Test SMS Mailing'
|
|
|
|
def _default_numbers(self):
|
|
return self.env.user.partner_id.phone_sanitized or ""
|
|
|
|
numbers = fields.Text(string='Number(s)', required=True,
|
|
default=_default_numbers, help='Carriage-return-separated list of phone numbers')
|
|
mailing_id = fields.Many2one('mailing.mailing', string='Mailing', required=True, ondelete='cascade')
|
|
|
|
def action_send_sms(self):
|
|
self.ensure_one()
|
|
|
|
numbers = [number.strip() for number in self.numbers.splitlines()]
|
|
sanitize_res = phone_validation.phone_sanitize_numbers_w_record(numbers, self.env.user)
|
|
sanitized_numbers = [info['sanitized'] for info in sanitize_res.values() if info['sanitized']]
|
|
invalid_numbers = [number for number, info in sanitize_res.items() if info['code']]
|
|
|
|
record = self.env[self.mailing_id.mailing_model_real].search([], limit=1)
|
|
body = self.mailing_id.body_plaintext
|
|
if record:
|
|
# Returns a proper error if there is a syntax error with qweb
|
|
body = self.env['mail.render.mixin']._render_template(body, self.mailing_id.mailing_model_real, record.ids)[record.id]
|
|
|
|
# res_id is used to map the result to the number to log notifications as IAP does not return numbers...
|
|
# TODO: clean IAP to make it return a clean dict with numbers / use custom keys / rename res_id to external_id
|
|
sent_sms_list = self.env['sms.api']._send_sms_batch([{
|
|
'res_id': number,
|
|
'number': number,
|
|
'content': body,
|
|
} for number in sanitized_numbers])
|
|
|
|
error_messages = {}
|
|
if any(sent_sms.get('state') != 'success' for sent_sms in sent_sms_list):
|
|
error_messages = self.env['sms.api']._get_sms_api_error_messages()
|
|
|
|
notification_messages = []
|
|
if invalid_numbers:
|
|
notification_messages.append(_('The following numbers are not correctly encoded: %s',
|
|
', '.join(invalid_numbers)))
|
|
|
|
for sent_sms in sent_sms_list:
|
|
if sent_sms.get('state') == 'success':
|
|
notification_messages.append(
|
|
_('Test SMS successfully sent to %s', sent_sms.get('res_id')))
|
|
elif sent_sms.get('state'):
|
|
notification_messages.append(
|
|
_('Test SMS could not be sent to %s:<br>%s',
|
|
sent_sms.get('res_id'),
|
|
error_messages.get(sent_sms['state'], _("An error occurred.")))
|
|
)
|
|
|
|
if notification_messages:
|
|
self.mailing_id._message_log(body='<ul>%s</ul>' % ''.join(
|
|
['<li>%s</li>' % notification_message for notification_message in notification_messages]
|
|
))
|
|
|
|
return True
|