From 4dfec6a9c4010ee11579cf2d904dc45d1e4c7866 Mon Sep 17 00:00:00 2001 From: Marina Alapont Date: Tue, 7 May 2024 13:33:05 +0200 Subject: [PATCH] [IMP] mail_multicompany: add company field in incoming mail servers model This commit adds a company_id field to the fetchmail.server model. Then uses this field, in the mail.thread message_process overwritten method, to correctly set the company on the environment when fetching mails --- mail_multicompany/README.rst | 5 +- mail_multicompany/__manifest__.py | 6 +- mail_multicompany/models/__init__.py | 2 + mail_multicompany/models/fetchmail.py | 10 ++++ mail_multicompany/models/mail_thread.py | 31 ++++++++++ mail_multicompany/readme/CONFIGURE.rst | 1 + mail_multicompany/readme/DESCRIPTION.rst | 3 +- mail_multicompany/readme/USAGE.rst | 1 + mail_multicompany/security/mail_security.xml | 12 ++++ .../static/description/index.html | 5 +- .../tests/test_mail_multicompany.py | 58 +++++++++++++++++++ mail_multicompany/views/fetchmail_views.xml | 28 +++++++++ 12 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 mail_multicompany/models/fetchmail.py create mode 100644 mail_multicompany/models/mail_thread.py create mode 100644 mail_multicompany/views/fetchmail_views.xml diff --git a/mail_multicompany/README.rst b/mail_multicompany/README.rst index 95162bebd81..8bf56c27325 100644 --- a/mail_multicompany/README.rst +++ b/mail_multicompany/README.rst @@ -28,7 +28,8 @@ Email Gateway Multi company |badge1| |badge2| |badge3| |badge4| |badge5| -This module adds company_id to the models ir.mail_server and mail.message. Also inherits mail.message create function to set the company mail_server. +This module adds company_id to the models ir.mail_server, mail.message and fetchmail.server. Also inherits mail.message create function to set the company mail_server +and the mail.thread message_process function to set the correct self.env.company when fetching from the incoming mail servers through a scheduled action. **Table of contents** @@ -39,6 +40,7 @@ Configuration ============= * Go to 'Settings / Technical / Outgoing Mail Servers', and add the company. +* Go to 'Settings / Technical / Incoming Mail Servers', and add the company. Usage ===== @@ -46,6 +48,7 @@ Usage To use this module, you need to: * Send some email or message that comes out of Odoo. +* Fetch emails from your mail server into Odoo. Bug Tracker =========== diff --git a/mail_multicompany/__manifest__.py b/mail_multicompany/__manifest__.py index f98560fad8d..288d97e5e40 100644 --- a/mail_multicompany/__manifest__.py +++ b/mail_multicompany/__manifest__.py @@ -10,6 +10,10 @@ "website": "https://github.com/OCA/multi-company", "license": "AGPL-3", "depends": ["mail"], - "data": ["security/mail_security.xml", "views/ir_mail_server_view.xml"], + "data": [ + "security/mail_security.xml", + "views/ir_mail_server_view.xml", + "views/fetchmail_views.xml", + ], "installable": True, } diff --git a/mail_multicompany/models/__init__.py b/mail_multicompany/models/__init__.py index fca8fa21570..94e18fbb9e9 100644 --- a/mail_multicompany/models/__init__.py +++ b/mail_multicompany/models/__init__.py @@ -2,3 +2,5 @@ from . import ir_mail_server from . import mail_message +from . import fetchmail +from . import mail_thread diff --git a/mail_multicompany/models/fetchmail.py b/mail_multicompany/models/fetchmail.py new file mode 100644 index 00000000000..eed060b51ab --- /dev/null +++ b/mail_multicompany/models/fetchmail.py @@ -0,0 +1,10 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class FetchmailServer(models.Model): + + _inherit = "fetchmail.server" + + company_id = fields.Many2one("res.company", "Company") diff --git a/mail_multicompany/models/mail_thread.py b/mail_multicompany/models/mail_thread.py new file mode 100644 index 00000000000..13e43bb66d1 --- /dev/null +++ b/mail_multicompany/models/mail_thread.py @@ -0,0 +1,31 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging + +from odoo import api, models + +_logger = logging.getLogger(__name__) + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @api.model + def message_process( + self, + model, + message, + custom_values=None, + save_original=False, + strip_attachments=False, + thread_id=None, + ): + context_company = self.env.company + server_id = self.env.context.get("default_fetchmail_server_id") + server = self.env["fetchmail.server"].browse(server_id) + server_company = server.company_id + if server_company and server_company != context_company: + context_company = server_company + return super(MailThread, self.with_company(context_company)).message_process( + model, message, custom_values, save_original, strip_attachments, thread_id + ) diff --git a/mail_multicompany/readme/CONFIGURE.rst b/mail_multicompany/readme/CONFIGURE.rst index 4fb31b6e01e..f3197e85d7e 100644 --- a/mail_multicompany/readme/CONFIGURE.rst +++ b/mail_multicompany/readme/CONFIGURE.rst @@ -1 +1,2 @@ * Go to 'Settings / Technical / Outgoing Mail Servers', and add the company. +* Go to 'Settings / Technical / Incoming Mail Servers', and add the company. diff --git a/mail_multicompany/readme/DESCRIPTION.rst b/mail_multicompany/readme/DESCRIPTION.rst index 4b34854da22..9f6fd935802 100644 --- a/mail_multicompany/readme/DESCRIPTION.rst +++ b/mail_multicompany/readme/DESCRIPTION.rst @@ -1 +1,2 @@ -This module adds company_id to the models ir.mail_server and mail.message. Also inherits mail.message create function to set the company mail_server. +This module adds company_id to the models ir.mail_server, mail.message and fetchmail.server. Also inherits mail.message create function to set the company mail_server +and the mail.thread message_process function to set the correct self.env.company when fetching from the incoming mail servers through a scheduled action. diff --git a/mail_multicompany/readme/USAGE.rst b/mail_multicompany/readme/USAGE.rst index 734066e8cb1..729d36d56ba 100644 --- a/mail_multicompany/readme/USAGE.rst +++ b/mail_multicompany/readme/USAGE.rst @@ -1,3 +1,4 @@ To use this module, you need to: * Send some email or message that comes out of Odoo. +* Fetch emails from your mail server into Odoo. diff --git a/mail_multicompany/security/mail_security.xml b/mail_multicompany/security/mail_security.xml index 84fcc6fc10b..decb4680ffa 100644 --- a/mail_multicompany/security/mail_security.xml +++ b/mail_multicompany/security/mail_security.xml @@ -20,4 +20,16 @@ name="domain_force" >['|',('company_id','=',False),('company_id', 'in', company_ids)] + + fetchmail_server multi-company + + + ['|',('company_id','=',False),('company_id', 'in', company_ids)] + diff --git a/mail_multicompany/static/description/index.html b/mail_multicompany/static/description/index.html index a864edfe5e4..6cf1b139b9e 100644 --- a/mail_multicompany/static/description/index.html +++ b/mail_multicompany/static/description/index.html @@ -369,7 +369,8 @@

Email Gateway Multi company

!! source digest: sha256:c844f628e5f56e26d2ca9490465e79ad1344b3734906420f78571ba6e57c8300 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: AGPL-3 OCA/multi-company Translate me on Weblate Try me on Runboat

-

This module adds company_id to the models ir.mail_server and mail.message. Also inherits mail.message create function to set the company mail_server.

+

This module adds company_id to the models ir.mail_server, mail.message and fetchmail.server. Also inherits mail.message create function to set the company mail_server +and the mail.thread message_process function to set the correct self.env.company when fetching from the incoming mail servers through a scheduled action.

Table of contents

@@ -395,6 +397,7 @@

Usage

To use this module, you need to:

diff --git a/mail_multicompany/tests/test_mail_multicompany.py b/mail_multicompany/tests/test_mail_multicompany.py index 1e00f07f983..6eec687af96 100644 --- a/mail_multicompany/tests/test_mail_multicompany.py +++ b/mail_multicompany/tests/test_mail_multicompany.py @@ -25,6 +25,13 @@ def setUpClass(cls): ) cls.server1 = server_obj.create({"name": "server 1", "smtp_host": "teset.smtp"}) cls.server2 = server_obj.create({"name": "server 1", "smtp_host": "test.smtp"}) + cls.mail_thread = cls.env["mail.thread"] + cls.fetchmail_server = cls.env["fetchmail.server"].create( + { + "name": "Test Server", + "company_id": cls.company2.id, + } + ) def _create_message(self): return ( @@ -86,3 +93,54 @@ def test_02_mail_message_company_restriction(self): msg = self._create_message() self.assertEqual(msg.mail_server_id.id, self.server1.id) self.assertEqual(msg.email_from, "test.from@example.com") + + def test_message_process(self): + fetchmail_server = self.fetchmail_server + self.server2.write({"company_id": self.company2.id}) + + # Create a mail alias and a message that can be processed + # to see that the company context is correctly set + res_partner_model = self.env["ir.model"].search([("model", "=", "res.partner")]) + customer = self.env["res.partner"].create({"name": "Test Partner"}) + + self.env["mail.alias"].create( + { + "alias_name": "test_alias", + "alias_model_id": res_partner_model.id, + "alias_parent_model_id": res_partner_model.id, + "alias_parent_thread_id": customer.id, + } + ) + self.env["ir.config_parameter"].sudo().set_param( + "mail.catchall.domain", "my_domain.com" + ) + model = "res.partner" + message = """MIME-Version: 1.0 + Date: Thu, 27 Dec 2018 16:27:45 +0100 + Message-ID: + Subject: test message on test partner + From: Test Partner + To: test_alias@my_domain.com + Content-Type: multipart/alternative; boundary="000000000000a47519057e029630" + + --000000000000a47519057e029630 + Content-Type: text/plain; charset="UTF-8" + + + --000000000000a47519057e029630 + Content-Type: text/html; charset="UTF-8" + Content-Transfer-Encoding: quoted-printable + +
Message content
+ + --000000000000a47519057e029630-- + """ + context = {"default_fetchmail_server_id": fetchmail_server.id} + + # Call the message_process method + result_id = self.mail_thread.with_context(**context).message_process( + model, message + ) + result = self.env["res.partner"].browse(result_id) + # Add your assertions here to validate the result + self.assertEqual(result.message_ids[0].mail_server_id, self.server2) diff --git a/mail_multicompany/views/fetchmail_views.xml b/mail_multicompany/views/fetchmail_views.xml new file mode 100644 index 00000000000..b47415d1fba --- /dev/null +++ b/mail_multicompany/views/fetchmail_views.xml @@ -0,0 +1,28 @@ + + + + fetchmail.server.list.add.company + fetchmail.server + + + + + + + + + + fetchmail.server.form.add.company + fetchmail.server + + + + + + + +