[IMP] mail: correctly support scheduled_date from template when sending emails

Purpose of this commit is to correctly generate and support scheduled date
defined on template when using ``send_mail`` tool that send emails directly
from a MailTemplate.

It uses the parsing tool method defined on mail.mail in order to have a
datetime localized in UTC then set timezone agnostic as expected by the
ORM.

Task-2826699 (Mail: use datetime for scheduled_date mail field instead of char)

Part-of: odoo/odoo#95623
This commit is contained in:
Stéphane Debauche
2022-08-25 19:57:16 +02:00
committed by Thibault Delavallée
parent eb75a77646
commit aed52f3e0a
3 changed files with 45 additions and 6 deletions
+6
View File
@@ -204,6 +204,12 @@ class MailTemplate(models.Model):
values = results[res_id]
if values.get('body_html'):
values['body'] = tools.html_sanitize(values['body_html'])
# if asked in fields to return, parse generated date into tz agnostic UTC as expected by ORM
scheduled_date = values.pop('scheduled_date', None)
if 'scheduled_date' in fields and scheduled_date:
parsed_datetime = self.env['mail.mail']._parse_scheduled_datetime(scheduled_date)
values['scheduled_date'] = parsed_datetime.replace(tzinfo=None) if parsed_datetime else False
# technical settings
values.update(
mail_server_id=template.mail_server_id.id or False,
+36 -3
View File
@@ -2,18 +2,21 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
import datetime
from freezegun import freeze_time
from odoo.addons.test_mail.tests.common import TestMailCommon, TestRecipients
from odoo.tests import tagged
from odoo.tests import tagged, users
from odoo.tools import mute_logger
@tagged('mail_template', 'multi_lang')
class TestMailTemplate(TestMailCommon, TestRecipients):
class TestMailTemplateCommon(TestMailCommon, TestRecipients):
@classmethod
def setUpClass(cls):
super(TestMailTemplate, cls).setUpClass()
super(TestMailTemplateCommon, cls).setUpClass()
cls.test_record = cls.env['mail.test.lang'].with_context(cls._test_context).create({
'email_from': 'ignasse@example.com',
'name': 'Test',
@@ -61,6 +64,36 @@ class TestMailTemplate(TestMailCommon, TestRecipients):
# Force the attachments of the template to be in the natural order.
cls.test_template.invalidate_recordset(['attachment_ids'])
@tagged('mail_template', 'multi_lang')
class TestMailTemplate(TestMailTemplateCommon):
@mute_logger('odoo.addons.mail.models.mail_mail')
@users('employee')
def test_template_schedule_email(self):
""" Test scheduling email sending from template. """
now = datetime.datetime.now()
test_template = self.test_template.with_env(self.env)
# schedule the mail in 3 days
test_template.scheduled_date = '{{datetime.datetime.now() + datetime.timedelta(days=3)}}'
with freeze_time(now):
mail_id = test_template.send_mail(self.test_record.id)
mail = self.env['mail.mail'].sudo().browse(mail_id)
self.assertEqual(
mail.scheduled_date.replace(second=0, microsecond=0),
(now + datetime.timedelta(days=3)).replace(second=0, microsecond=0),
)
self.assertEqual(mail.state, 'outgoing')
# check a wrong format
test_template.scheduled_date = '{{"test " * 5}}'
with freeze_time(now):
mail_id = test_template.send_mail(self.test_record.id)
mail = self.env['mail.mail'].sudo().browse(mail_id)
self.assertFalse(mail.scheduled_date)
self.assertEqual(mail.state, 'outgoing')
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_template_send_email(self):
mail_id = self.test_template.send_mail(self.test_record.id)
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.test_mail.tests.test_mail_template import TestMailTemplate
from odoo.addons.test_mail.tests.test_mail_template import TestMailTemplateCommon
from odoo.tests import tagged
@tagged('mail_template')
class TestMailTemplateTools(TestMailTemplate):
@tagged('mail_template', 'multi_lang')
class TestMailTemplateTools(TestMailTemplateCommon):
def test_mail_template_preview_force_lang(self):
test_record = self.env['mail.test.lang'].browse(self.test_record.ids)