[FIX] event: properly handle non-existent template

This commit modifies `execute` method to avoid `MissingError` on reading
`template_ref` field.

`template_ref` is a Reference field, which may have id of a deleted record.

https://github.com/odoo/odoo/blob/927890d0178c361ef8d80fe6a1ec2ffaefaf73fd/addons/event/models/event_mail.py#L53

Method `execute` is used by cron.

https://github.com/odoo/odoo/blob/fba13477843d4d36e3e7a9ddd4b0812c9726e20c/addons/event/data/ir_cron_data.xml#L4-L8
https://github.com/odoo/odoo/blob/927890d0178c361ef8d80fe6a1ec2ffaefaf73fd/addons/event/models/event_mail.py#L247-L256

Getting the `MissingError` means blocking processing the whole batch of tickets.

Fix it by ignoring such tickets and logging a warning

https://online.sentry.io/issues/4089773378

closes odoo/odoo#121199

X-original-commit: c61a8fbb7d4338ca362a3d2093e7aa2f973b289c
Signed-off-by: Ivan Elizaryev (iel) <iel@odoo.com>
This commit is contained in:
Ivan Yelizariev
2023-05-12 19:55:10 +02:00
committed by Ivan Rasputin
parent 874fa51dbe
commit 547274edd1
2 changed files with 53 additions and 3 deletions
+17 -3
View File
@@ -12,6 +12,8 @@ from dateutil.relativedelta import relativedelta
from odoo import api, fields, models, tools
from odoo.tools import exception_to_unicode
from odoo.tools.translate import _
from odoo.exceptions import MissingError
_logger = logging.getLogger(__name__)
@@ -283,6 +285,7 @@ class EventMailRegistration(models.Model):
(reg_mail.scheduled_date and reg_mail.scheduled_date <= now) and \
reg_mail.scheduler_id.notification_type == 'mail'
)
done = self.browse()
for reg_mail in todo:
organizer = reg_mail.scheduler_id.event_id.organizer_id
company = self.env.company
@@ -297,10 +300,21 @@ class EventMailRegistration(models.Model):
email_values = {
'author_id': author.id,
}
if not reg_mail.scheduler_id.template_ref.email_from:
template = None
try:
template = reg_mail.scheduler_id.template_ref.exists()
except MissingError:
pass
if not template:
_logger.warning("Cannot process ticket %s, because Mail Scheduler %s has reference to non-existent template", reg_mail.registration_id, reg_mail.scheduler_id)
continue
if not template.email_from:
email_values['email_from'] = author.email_formatted
reg_mail.scheduler_id.template_ref.send_mail(reg_mail.registration_id.id, email_values=email_values)
todo.write({'mail_sent': True})
template.send_mail(reg_mail.registration_id.id, email_values=email_values)
done |= reg_mail
done.write({'mail_sent': True})
@api.depends('registration_id', 'scheduler_id.interval_unit', 'scheduler_id.interval_type')
def _compute_scheduled_date(self):
+36
View File
@@ -33,6 +33,12 @@ class TestEventFlow(EventCase):
'name': 'TestReg1',
'event_id': test_event.id,
})
scheduler = self.env['event.mail'].sudo().search([
('event_id', '=', test_event.id),
('interval_type', '=', 'after_sub')
])
self.assertTrue(scheduler.mail_registration_ids.mail_sent)
self.assertEqual(test_reg1.state, 'open', 'Event: auto_confirmation of registration failed')
self.assertEqual(test_event.seats_reserved, 1, 'Event: wrong number of reserved seats after confirmed registration')
test_reg2 = self.env['event.registration'].with_user(self.user_eventuser).create({
@@ -105,3 +111,33 @@ class TestEventFlow(EventCase):
self.assertEqual(
test_reg1.state, 'draft',
'Event: new registration should not be confirmed with auto_confirmation parameter being False')
@mute_logger('odoo.addons.event.models.event_mail')
def test_event_missed_mail_template(self):
""" Check that error on mail sending is ignored if corresponding mail template was deleted """
test_event = self.env['event.event'].with_user(self.user_eventmanager).create({
'name': 'TestEvent',
'date_begin': datetime.datetime.now() + relativedelta(days=-1),
'date_end': datetime.datetime.now() + relativedelta(days=1),
'seats_max': 2,
'seats_limited': True,
})
self.assertFalse(test_event.auto_confirm)
# EventUser create registrations for this event
test_reg = self.env['event.registration'].with_user(self.user_eventuser).create({
'name': 'TestReg1',
'event_id': test_event.id,
})
scheduler = self.env['event.mail'].sudo().search([
('event_id', '=', test_event.id),
('interval_type', '=', 'after_sub')
])
# Imagine user deletes mail template for whatever reason
scheduler.template_ref.unlink()
# Mails should not be sent
test_reg.action_confirm()
self.assertFalse(scheduler.mail_registration_ids.mail_sent)