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() + 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) diff --git a/addons/event_sms/models/event_mail.py b/addons/event_sms/models/event_mail.py index 53562875f10..3e56a0552a7 100644 --- a/addons/event_sms/models/event_mail.py +++ b/addons/event_sms/models/event_mail.py @@ -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()