diff --git a/mail_force_queue/README.rst b/mail_force_queue/README.rst new file mode 100644 index 00000000..ac996595 --- /dev/null +++ b/mail_force_queue/README.rst @@ -0,0 +1,62 @@ +.. image:: https://img.shields.io/badge/license-LGPL--3-blue.png + :target: https://www.gnu.org/licenses/lgpl + :alt: License: LGPL-3 + +================== +Email: force queue +================== + +Force outgoing emails to be queued instead of sent immediately. +Queued emails are sent by 'Email Queue Manager' cron. + +This also solves possible TransactionRollbackError while deleting outgoing email (see `https://github.com/odoo/odoo/issues/22148 +`_.) + + +Usage +===== + +Go to 'General Settings' and set 'Force Mail queue' + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/205/10.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smash it by providing detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Lorenzo Battistini + +Do not contact contributors directly about support or help with technical issues. + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/mail_force_queue/__init__.py b/mail_force_queue/__init__.py new file mode 100644 index 00000000..762c7686 --- /dev/null +++ b/mail_force_queue/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import models diff --git a/mail_force_queue/__manifest__.py b/mail_force_queue/__manifest__.py new file mode 100644 index 00000000..bb14d5de --- /dev/null +++ b/mail_force_queue/__manifest__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +{ + "name": "Email: force queue", + "summary": "Force outgoing emails to be queued", + "version": "10.0.0.2.0", + "category": "Discuss", + "website": "https://github.com/OCA/social", + "author": "Agile Business Group, Odoo Community Association (OCA)", + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": [ + "mail", + ], + "data": [ + "views/res_config_view.xml", + ], +} diff --git a/mail_force_queue/models/__init__.py b/mail_force_queue/models/__init__.py new file mode 100644 index 00000000..e7347371 --- /dev/null +++ b/mail_force_queue/models/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import mail_message +from . import res_company +from . import res_config diff --git a/mail_force_queue/models/mail_message.py b/mail_force_queue/models/mail_message.py new file mode 100644 index 00000000..19e48b34 --- /dev/null +++ b/mail_force_queue/models/mail_message.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import models, api + + +class Message(models.Model): + _inherit = 'mail.message' + + @api.model + def create(self, values): + if self.env.user.company_id.force_mail_queue: + mail_notify_force_send = self.env.context.get( + 'mail_notify_force_send', False) + return super(Message, self.with_context( + mail_notify_force_send=mail_notify_force_send)).create(values) + else: + return super(Message, self).create(values) diff --git a/mail_force_queue/models/res_company.py b/mail_force_queue/models/res_company.py new file mode 100644 index 00000000..cab4a3dd --- /dev/null +++ b/mail_force_queue/models/res_company.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import models, fields + + +class ResCompany(models.Model): + _inherit = "res.company" + + force_mail_queue = fields.Boolean("Force Mail queue") diff --git a/mail_force_queue/models/res_config.py b/mail_force_queue/models/res_config.py new file mode 100644 index 00000000..393e68e2 --- /dev/null +++ b/mail_force_queue/models/res_config.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import fields, models + + +class BaseConfigSettings(models.TransientModel): + _inherit = 'base.config.settings' + + force_mail_queue = fields.Boolean( + related="company_id.force_mail_queue", string="Force Mail queue", + help="Force outgoing emails to be queued instead of sent immediately. " + "Queued emails are sent by 'Email Queue Manager' cron") diff --git a/mail_force_queue/static/description/icon.png b/mail_force_queue/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/mail_force_queue/static/description/icon.png differ diff --git a/mail_force_queue/tests/__init__.py b/mail_force_queue/tests/__init__.py new file mode 100644 index 00000000..5fc4dc04 --- /dev/null +++ b/mail_force_queue/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import test_mail_notification diff --git a/mail_force_queue/tests/test_mail_notification.py b/mail_force_queue/tests/test_mail_notification.py new file mode 100644 index 00000000..32134a26 --- /dev/null +++ b/mail_force_queue/tests/test_mail_notification.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini - Agile Business Group +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +import odoo.tests.common as common + + +class TestMailNotification(common.TransactionCase): + def setUp(self): + super(TestMailNotification, self).setUp() + + self.partner_obj = self.env['res.partner'] + self.env.user.company_id.force_mail_queue = True + + def test_get_signature_footer(self): + vals = { + 'name': 'p1@example.com', + 'email': 'p1@example.com', + } + partner1 = self.partner_obj.create(vals) + + body = 'this is the body' + subject = 'this is the subject' + + res = partner1.message_post( + subject=subject, + body=body, + content_subtype="plaintext", + partner_ids=[partner1.id], + ) + mail = self.env['mail.mail'].search([ + ('mail_message_id', '=', res.id) + ]) + # default behaviour: outgoing mail queued + self.assertTrue(mail) + self.assertEqual(mail.state, 'outgoing') + + res = partner1.with_context(mail_notify_force_send=True).message_post( + subject=subject, + body=body, + content_subtype="plaintext", + partner_ids=[partner1.id], + ) + mail = self.env['mail.mail'].search([ + ('mail_message_id', '=', res.id) + ]) + # force send immediately: outgoing email is sent and deleted + self.assertFalse(mail) + + res = partner1.with_context(mail_notify_force_send=False).message_post( + subject=subject, + body=body, + content_subtype="plaintext", + partner_ids=[partner1.id], + ) + mail = self.env['mail.mail'].search([ + ('mail_message_id', '=', res.id) + ]) + # do not send immediately + self.assertTrue(mail) + self.assertEqual(mail.state, 'outgoing') diff --git a/mail_force_queue/views/res_config_view.xml b/mail_force_queue/views/res_config_view.xml new file mode 100644 index 00000000..5ba2e930 --- /dev/null +++ b/mail_force_queue/views/res_config_view.xml @@ -0,0 +1,16 @@ + + + + view_general_configuration_mail_force_queue + base.config.settings + + + +
+ +
+
+
+
+