Files
odoo_source/addons/event_sms/models/event_mail.py
T
Thibault Delavallée 2520bfe625 [REF] event: manually update completion of mail schedulers
Purpose of this commit is to remove the "done" field computation. Indeed
it is based on either

  * mail_sent field if scheduler is global to the event (before or after
    event). This computation is light as this field changes only once
    when emails are scheduled and sent;
  * status of event registrations compared to all sent communication on
    those registrations. This is costly as adding a new registration changes

In this commit we therefore

  * rename ``done`` to ``mail_done`` to ease grep and understanding;
  * remove ``mail_sent`` as it is integrated within ``done``;
  * manually update ``mail_done`` when updating schedulers instead of doing
    it through a compute method;

Some code cleaning is performed to make it clearer and easier to understand.

LINKS

Task ID-2414658
COM PR odoo/odoo#63093
UPG PR odoo/upgrade#2014
2021-03-24 11:18:47 +00:00

78 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class EventTypeMail(models.Model):
_inherit = 'event.type.mail'
notification_type = fields.Selection(selection_add=[
('sms', 'SMS')
], ondelete={'sms': 'set default'})
sms_template_id = fields.Many2one(
'sms.template', string='SMS Template',
domain=[('model', '=', 'event.registration')], ondelete='restrict',
help='This field contains the template of the SMS that will be automatically sent')
@api.model
def _get_event_mail_fields_whitelist(self):
return super(EventTypeMail, self)._get_event_mail_fields_whitelist() + ['sms_template_id']
class EventMailScheduler(models.Model):
_inherit = 'event.mail'
notification_type = fields.Selection(selection_add=[
('sms', 'SMS')
], ondelete={'sms': 'set default'})
sms_template_id = fields.Many2one(
'sms.template', string='SMS Template',
domain=[('model', '=', 'event.registration')], ondelete='restrict',
help='This field contains the template of the SMS that will be automatically sent')
def execute(self):
for scheduler in self:
now = fields.Datetime.now()
if scheduler.interval_type != 'after_sub' and scheduler.notification_type == 'sms':
# before or after event -> one shot email
if scheduler.mail_done:
continue
# no template -> ill configured, skip and avoid crash
if not scheduler.sms_template_id:
continue
# Do not send SMS if the communication was scheduled before the event but the event is over
if scheduler.scheduled_date <= now and (scheduler.interval_type != 'before_event' or scheduler.event_id.date_end > now):
self.env['event.registration']._message_sms_schedule_mass(
template=scheduler.sms_template_id,
active_domain=[('event_id', '=', scheduler.event_id.id), ('state', '!=', 'cancel')],
mass_keep_log=True
)
scheduler.update({
'mail_done': True,
'mail_count_done': scheduler.event_id.seats_reserved + scheduler.event_id.seats_used,
})
return super(EventMailScheduler, self).execute()
class EventMailRegistration(models.Model):
_inherit = 'event.mail.registration'
def execute(self):
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()