diff options
| author | Thibault Delavallée <tde@odoo.com> | 2021-03-19 13:30:15 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2021-03-19 16:36:25 +0000 |
| commit | 353bc5de9fafbc1527c382614e02438ee799c8a4 (patch) | |
| tree | de28f9344bf2ae2dda0111c8ce16ba293a36a1a7 /addons/event | |
| parent | 73d09268922a7d242af85e36a3d81ed25ab4ca33 (diff) | |
[FIX] event: respect scheduled date when calling mail schedulers execute
In this commit we ensure calling ``execute`` method on communication schedulers
do not send emails or SMS if their scheduled date is still not achieved.
Currently only the cron method does it (see ``run``).
It is now safe to call ``execute`` directly on event communication schedulers.
Related to Task ID-2414658
COM PR odoo/odoo#68158
Diffstat (limited to 'addons/event')
| -rw-r--r-- | addons/event/models/event_mail.py | 19 | ||||
| -rw-r--r-- | addons/event/tests/test_event_mail_schedule.py | 15 |
2 files changed, 20 insertions, 14 deletions
diff --git a/addons/event/models/event_mail.py b/addons/event/models/event_mail.py index ee4c1094a47..9d9df8faf06 100644 --- a/addons/event/models/event_mail.py +++ b/addons/event/models/event_mail.py @@ -119,10 +119,11 @@ class EventMailScheduler(models.Model): if lines: mail.write({'mail_registration_ids': lines}) # execute scheduler on registrations - mail.mail_registration_ids.filtered(lambda reg: reg.scheduled_date and reg.scheduled_date <= now).execute() + mail.mail_registration_ids.execute() else: # Do not send emails if the mailing was scheduled before the event but the event is over - if not mail.mail_sent and (mail.interval_type != 'before_event' or mail.event_id.date_end > now) and mail.notification_type == 'mail': + if not mail.mail_sent and mail.scheduled_date <= now and mail.notification_type == 'mail' and \ + (mail.interval_type != 'before_event' or mail.event_id.date_end > now): mail.event_id.mail_attendees(mail.template_id.id) mail.write({'mail_sent': True}) return True @@ -195,10 +196,16 @@ class EventMailRegistration(models.Model): mail_sent = fields.Boolean('Mail Sent') def execute(self): - for mail in self: - if mail.registration_id.state in ['open', 'done'] and not mail.mail_sent and mail.scheduler_id.notification_type == 'mail': - mail.scheduler_id.template_id.send_mail(mail.registration_id.id) - mail.write({'mail_sent': True}) + now = fields.Datetime.now() + todo = self.filtered(lambda reg_mail: + not reg_mail.mail_sent and \ + reg_mail.registration_id.state in ['open', 'done'] and \ + (reg_mail.scheduled_date and reg_mail.scheduled_date <= now) and \ + reg_mail.scheduler_id.notification_type == 'mail' + ) + for reg_mail in todo: + reg_mail.scheduler_id.template_id.send_mail(reg_mail.registration_id.id) + todo.write({'mail_sent': True}) @api.depends('registration_id', 'scheduler_id.interval_unit', 'scheduler_id.interval_type') def _compute_scheduled_date(self): diff --git a/addons/event/tests/test_event_mail_schedule.py b/addons/event/tests/test_event_mail_schedule.py index 7ec60492b24..896a5873205 100644 --- a/addons/event/tests/test_event_mail_schedule.py +++ b/addons/event/tests/test_event_mail_schedule.py @@ -152,14 +152,13 @@ class TestMailSchedule(TestEventCommon, MockEmail): self.assertFalse(event_prev_scheduler.done) # execute event reminder scheduler explicitly, before scheduled date -> should not do anything - # FIXME: execute does not respect scheduled date - # now_start = event_date_begin + relativedelta(hours=-25) - # with freeze_time(now_start), self.mock_mail_gateway(): - # event_prev_scheduler.execute() - - # self.assertFalse(event_prev_scheduler.mail_sent) - # self.assertFalse(event_prev_scheduler.done) - # self.assertEqual(len(self._new_mails, 0)) + now_start = event_date_begin + relativedelta(hours=-25) + with freeze_time(now_start), self.mock_mail_gateway(): + event_prev_scheduler.execute() + + self.assertFalse(event_prev_scheduler.mail_sent) + self.assertFalse(event_prev_scheduler.done) + self.assertEqual(len(self._new_mails), 0) # execute cron to run schedulers now_start = event_date_begin + relativedelta(hours=-23) |
