From cd5850d83d2579465db6f72ec04137b9a2779f92 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Tue, 28 Jun 2022 06:53:17 +0000 Subject: [PATCH] [REF] mail: use a datetime instead of a char for the mail scheduled date MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Purpose ======= Historically, we use a char field on the to match the field type on the . But even it's useful to have a char field on the mail template (which can contains QWeb code or Jinja previously to Odoo 15.0) it is not that useful for the to keep this char type, because the value is already rendered. Moreover this forces to have some code convert and store datetime values using the standard format and in UTC. We now correctly use a datetime field, as all other fields of that kind in Odoo. Task-2826699 (Mail: use datetime for scheduled_date mail field instead of char) Prepares Task-2207626 (Rating: Delay rating notification to ease feedback) Part-of: odoo/odoo#95623 Co-authored-by: Thibault Delavallée --- addons/mail/models/mail_mail.py | 14 +++++--------- addons/mail/models/mail_template.py | 1 - addons/test_mail/tests/test_mail_mail.py | 7 +++---- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py index afef9897340..02f8edbb129 100644 --- a/addons/mail/models/mail_mail.py +++ b/addons/mail/models/mail_mail.py @@ -85,7 +85,7 @@ class MailMail(models.Model): 'Auto Delete', help="This option permanently removes any track of email after it's been sent, including from the Technical menu in the Settings, in order to preserve storage space of your Odoo database.") to_delete = fields.Boolean('To Delete', help='If set, the mail will be deleted during the next Email Queue CRON run.') - scheduled_date = fields.Char('Scheduled Send Date', + scheduled_date = fields.Datetime('Scheduled Send Date', help="If set, the queue manager will send the email after the date. If not set, the email will be send as soon as possible. Unless a timezone is specified, it is considered as being in UTC timezone.") fetchmail_server_id = fields.Many2one('fetchmail.server', "Inbound Mail Server", readonly=True) @@ -132,10 +132,9 @@ class MailMail(models.Model): values['is_notification'] = True if values.get('scheduled_date'): parsed_datetime = self._parse_scheduled_datetime(values['scheduled_date']) - if parsed_datetime: - values['scheduled_date'] = parsed_datetime.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) - else: - values['scheduled_date'] = False + values['scheduled_date'] = parsed_datetime.replace(tzinfo=None) if parsed_datetime else False + else: + values['scheduled_date'] = False # void string crashes new_mails = super(MailMail, self).create(values_list) new_mails_w_attach = self @@ -150,10 +149,7 @@ class MailMail(models.Model): def write(self, vals): if vals.get('scheduled_date'): parsed_datetime = self._parse_scheduled_datetime(vals['scheduled_date']) - if parsed_datetime: - vals['scheduled_date'] = parsed_datetime.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) - else: - vals['scheduled_date'] = False + vals['scheduled_date'] = parsed_datetime.replace(tzinfo=None) if parsed_datetime else False res = super(MailMail, self).write(vals) if vals.get('attachment_ids'): for mail in self: diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index 0040412c4e2..e149c1271f3 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -4,7 +4,6 @@ import base64 import logging - from odoo import _, api, fields, models, tools, Command from odoo.exceptions import UserError from odoo.tools import is_html_empty diff --git a/addons/test_mail/tests/test_mail_mail.py b/addons/test_mail/tests/test_mail_mail.py index 646cbf86945..76d9ca4fcd2 100644 --- a/addons/test_mail/tests/test_mail_mail.py +++ b/addons/test_mail/tests/test_mail_mail.py @@ -231,7 +231,7 @@ class TestMailMail(TestMailCommon): (now + timedelta(hours=-3)).strftime("%H:%M:%S %d-%m-%Y") + " -0400", ] expected_datetimes = [ - False, '', False, + False, False, False, now, now - pytz.timezone('Australia/Brisbane').utcoffset(now), now - timedelta(days=1), now + timedelta(days=1), now + timedelta(days=1), now + timedelta(hours=-1), @@ -253,9 +253,8 @@ class TestMailMail(TestMailCommon): ]) for mail, expected_datetime, scheduled_datetime in zip(mails, expected_datetimes, scheduled_datetimes): - expected = expected_datetime.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) if expected_datetime else expected_datetime - self.assertEqual(mail.scheduled_date, expected, - 'Scheduled date: %s should be stored as %s, received %s' % (scheduled_datetime, expected, mail.scheduled_date)) + self.assertEqual(mail.scheduled_date, expected_datetime, + 'Scheduled date: %s should be stored as %s, received %s' % (scheduled_datetime, expected_datetime, mail.scheduled_date)) self.assertEqual(mail.state, 'outgoing') with freeze_time(now):