[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

X-original-commit: 353bc5de9fafbc1527c382614e02438ee799c8a4
This commit is contained in:
Thibault Delavallée
2021-03-19 18:38:30 +00:00
parent 401e037850
commit b41fcc8458
3 changed files with 36 additions and 18 deletions
+13 -6
View File
@@ -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):
@@ -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()
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))
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)
+17 -5
View File
@@ -36,7 +36,9 @@ class EventMailScheduler(models.Model):
now = fields.Datetime.now()
if mail.interval_type != 'after_sub':
# Do not send SMS if the communication 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 == 'sms' and mail.sms_template_id:
if not mail.mail_sent and mail.scheduled_date <= now and mail.notification_type == 'sms' and \
(mail.interval_type != 'before_event' or mail.event_id.date_end > now) and \
mail.sms_template_id:
self.env['event.registration']._message_sms_schedule_mass(
template=mail.sms_template_id,
active_domain=[('event_id', '=', mail.event_id.id), ('state', '!=', 'cancel')],
@@ -50,8 +52,18 @@ class EventMailRegistration(models.Model):
_inherit = 'event.mail.registration'
def execute(self):
for record in self:
if record.registration_id.state in ['open', 'done'] and not record.mail_sent and record.scheduler_id.notification_type == 'sms':
record.registration_id._message_sms_schedule_mass(template=record.scheduler_id.sms_template_id, mass_keep_log=True)
record.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 == 'sms'
)
for reg_mail in todo:
reg_mail.registration_id._message_sms_schedule_mass(
template=reg_mail.scheduler_id.sms_template_id,
mass_keep_log=True
)
todo.write({'mail_sent': True})
return super(EventMailRegistration, self).execute()