diff --git a/addons/sale_async_emails/__init__.py b/addons/sale_async_emails/__init__.py new file mode 100644 index 00000000000..d6210b1285d --- /dev/null +++ b/addons/sale_async_emails/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import models diff --git a/addons/sale_async_emails/__manifest__.py b/addons/sale_async_emails/__manifest__.py new file mode 100644 index 00000000000..76cf31f9c5b --- /dev/null +++ b/addons/sale_async_emails/__manifest__.py @@ -0,0 +1,14 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +{ + 'name': "Sales - Async Emails", + 'category': 'Sales/Sales', + 'summary': "Send order status emails asynchronously", + 'depends': ['sale'], + 'data': [ + 'data/ir_config_parameter.xml', + 'data/ir_cron.xml', + ], + 'auto_install': True, + 'license': 'LGPL-3', +} diff --git a/addons/sale_async_emails/data/ir_config_parameter.xml b/addons/sale_async_emails/data/ir_config_parameter.xml new file mode 100644 index 00000000000..633ff0deb7e --- /dev/null +++ b/addons/sale_async_emails/data/ir_config_parameter.xml @@ -0,0 +1,9 @@ + + + + + sale.async_emails + False + + + diff --git a/addons/sale_async_emails/data/ir_cron.xml b/addons/sale_async_emails/data/ir_cron.xml new file mode 100644 index 00000000000..57291dee7a8 --- /dev/null +++ b/addons/sale_async_emails/data/ir_cron.xml @@ -0,0 +1,16 @@ + + + + + Sales: Send pending emails + + code + model._cron_send_pending_emails() + + 1 + days + -1 + True + + + diff --git a/addons/sale_async_emails/models/__init__.py b/addons/sale_async_emails/models/__init__.py new file mode 100644 index 00000000000..83d45ea62a4 --- /dev/null +++ b/addons/sale_async_emails/models/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import sale_order diff --git a/addons/sale_async_emails/models/sale_order.py b/addons/sale_async_emails/models/sale_order.py new file mode 100644 index 00000000000..e5c54bbe457 --- /dev/null +++ b/addons/sale_async_emails/models/sale_order.py @@ -0,0 +1,45 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models +from odoo.tools.misc import str2bool + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + pending_email_template_id = fields.Many2one( + string="Pending Email Template", + help="The template of the pending email that must be sent asynchronously.", + comodel_name='mail.template', + ondelete='set null', + readonly=True, + ) + + def _send_order_notification_mail(self, mail_template): + """ Override of `sale` to reschedule order status emails to be sent asynchronously. """ + async_send = str2bool(self.env['ir.config_parameter'].sudo().get_param('sale.async_emails')) + cron = async_send and self.env.ref('sale_async_emails.cron', raise_if_not_found=False) + if async_send and cron and not self.env.context.get('is_async_email', False): + # Schedule the email to be sent asynchronously. + self.pending_email_template_id = mail_template + cron._trigger() + else: # We are in the cron job, or the user has disabled async emails. + super()._send_order_notification_mail(mail_template) # Send the email synchronously. + + @api.model + def _cron_send_pending_emails(self, auto_commit=True): + """ Find and send pending order status emails asynchronously. + + :param bool auto_commit: Whether the database cursor should be committed as soon as an email + is sent. Set to False in unit tests. + :return: None + """ + pending_email_orders = self.search([('pending_email_template_id', '!=', False)]) + for order in pending_email_orders: + order = order[0] # Avoid pre-fetching after each cache invalidation due to committing. + order.with_context(is_async_email=True)._send_order_notification_mail( + order.pending_email_template_id + ) # Asynchronously resume the email sending. + order.pending_email_template_id = None + if auto_commit: + self.env.cr.commit() # Save progress in case the cron is killed. diff --git a/addons/sale_async_emails/tests/__init__.py b/addons/sale_async_emails/tests/__init__.py new file mode 100644 index 00000000000..1e8180e07e5 --- /dev/null +++ b/addons/sale_async_emails/tests/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import test_sale_order diff --git a/addons/sale_async_emails/tests/test_sale_order.py b/addons/sale_async_emails/tests/test_sale_order.py new file mode 100644 index 00000000000..d9724d1a4bc --- /dev/null +++ b/addons/sale_async_emails/tests/test_sale_order.py @@ -0,0 +1,74 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from unittest.mock import patch + +from odoo.tests import tagged + +from odoo.addons.sale.tests.common import SaleCommon + + +@tagged('post_install', '-at_install') +class TestSaleOrder(SaleCommon): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.async_emails_cron = cls.env.ref('sale_async_emails.cron') + cls.confirmation_email_template = cls.sale_order._get_confirmation_template() + + def test_order_status_email_is_sent_asynchronously(self): + """ Test that the order status email is sent asynchronously when configured. """ + self.env['ir.config_parameter'].set_param('sale.async_emails', 'True') + + with patch( + 'odoo.addons.sale.models.sale_order.SaleOrder._send_order_notification_mail' + ) as sync_email_send_mock: + self.sale_order._send_order_notification_mail(self.confirmation_email_template) + self.assertTrue( + self.sale_order.pending_email_template_id, + msg="The email template should be saved on the sales order.", + ) + self.assertTrue( + self.env['ir.cron.trigger'].search_count( + [('cron_id', '=', self.async_emails_cron.id)] + ), + msg="The asynchronous email sending cron should be triggered.", + ) + self.assertEqual( + sync_email_send_mock.call_count, + 0, + msg="The email should not also be sent synchronously.", + ) + + def test_order_status_email_is_sent_synchronously_if_not_configured(self): + """ Test that the order status email is sent synchronously when nothing is configured. """ + self.env['ir.config_parameter'].set_param('sale.async_emails', 'False') + + with patch( + 'odoo.addons.sale.models.sale_order.SaleOrder._send_order_notification_mail' + ) as sync_email_send_mock: + self.sale_order._send_order_notification_mail(self.confirmation_email_template) + self.assertEqual( + sync_email_send_mock.call_count, + 1, + msg="The email should be sent synchronously when the system parameter is not set.", + ) + + def test_async_emails_cron_does_not_trigger_itself(self): + """ Test that the asynchronous email sending cron does not enter an infinite loop. """ + self.env['ir.config_parameter'].set_param('sale.async_emails', 'True') + self.sale_order.pending_email_template_id = self.confirmation_email_template + + with patch( + 'odoo.addons.sale.models.sale_order.SaleOrder._send_order_notification_mail' + ) as sync_email_send_mock: + self.env['sale.order']._cron_send_pending_emails(auto_commit=False) + self.assertFalse( + self.sale_order.pending_email_template_id, + msg="The email template should be removed from the sales order.", + ) + self.assertEqual( + sync_email_send_mock.call_count, + 1, + msg="The email should be sent synchronously when requested by the cron.", + )