Skip to content

Commit

Permalink
Merge PR #616 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Sep 13, 2024
2 parents f73d0f2 + 4dfec6a commit 3eb2f92
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 4 deletions.
5 changes: 4 additions & 1 deletion mail_multicompany/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -39,13 +40,15 @@ Configuration
=============

* Go to 'Settings / Technical / Outgoing Mail Servers', and add the company.
* Go to 'Settings / Technical / Incoming Mail Servers', and add the company.

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
===========
Expand Down
6 changes: 5 additions & 1 deletion mail_multicompany/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 2 additions & 0 deletions mail_multicompany/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

from . import ir_mail_server
from . import mail_message
from . import fetchmail
from . import mail_thread
10 changes: 10 additions & 0 deletions mail_multicompany/models/fetchmail.py
Original file line number Diff line number Diff line change
@@ -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")
31 changes: 31 additions & 0 deletions mail_multicompany/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions mail_multicompany/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion mail_multicompany/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions mail_multicompany/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions mail_multicompany/security/mail_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@
name="domain_force"
>['|',('company_id','=',False),('company_id', 'in', company_ids)]</field>
</record>
<record model="ir.rule" id="fetchmail_server_rule">
<field name="name">fetchmail_server multi-company</field>
<field
name="model_id"
search="[('model','=','fetchmail.server')]"
model="ir.model"
/>
<field name="global" eval="True" />
<field
name="domain_force"
>['|',('company_id','=',False),('company_id', 'in', company_ids)]</field>
</record>
</odoo>
5 changes: 4 additions & 1 deletion mail_multicompany/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ <h1 class="title">Email Gateway Multi company</h1>
!! source digest: sha256:c844f628e5f56e26d2ca9490465e79ad1344b3734906420f78571ba6e57c8300
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/multi-company/tree/16.0/mail_multicompany"><img alt="OCA/multi-company" src="https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-mail_multicompany"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/multi-company&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>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.</p>
<p>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.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -388,13 +389,15 @@ <h1 class="title">Email Gateway Multi company</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<ul class="simple">
<li>Go to ‘Settings / Technical / Outgoing Mail Servers’, and add the company.</li>
<li>Go to ‘Settings / Technical / Incoming Mail Servers’, and add the company.</li>
</ul>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<p>To use this module, you need to:</p>
<ul class="simple">
<li>Send some email or message that comes out of Odoo.</li>
<li>Fetch emails from your mail server into Odoo.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
Expand Down
58 changes: 58 additions & 0 deletions mail_multicompany/tests/test_mail_multicompany.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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, "[email protected]")

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: <thisisanid1>
Subject: test message on test partner
From: Test Partner <[email protected]>
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
<div>Message content</div>
--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)
28 changes: 28 additions & 0 deletions mail_multicompany/views/fetchmail_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" ?>
<odoo>
<record id="view_email_server_tree_add_company" model="ir.ui.view">
<field name="name">fetchmail.server.list.add.company</field>
<field name="model">fetchmail.server</field>
<field name="inherit_id" ref="mail.view_email_server_tree" />
<field name="arch" type="xml">
<field name="state" position="after">
<field name="company_id" groups="base.group_multi_company" />
</field>
</field>
</record>

<record id="view_email_server_form_add_company" model="ir.ui.view">
<field name="name">fetchmail.server.form.add.company</field>
<field name="model">fetchmail.server</field>
<field name="inherit_id" ref="mail.view_email_server_form" />
<field name="arch" type="xml">
<field name="date" position="after">
<field
name="company_id"
groups="base.group_multi_company"
options="{'no_create': True}"
/>
</field>
</field>
</record>
</odoo>

0 comments on commit 3eb2f92

Please sign in to comment.