Merge PR #997 into 15.0

Signed-off-by simahawk
This commit is contained in:
OCA-git-bot 2022-11-11 08:58:59 +00:00
commit d96713de48
14 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1 @@
wait for the bot

View File

@ -0,0 +1 @@
from . import models

View File

@ -0,0 +1,17 @@
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Mail parent recipient",
"summary": "Send email to parent partner if partner's email is empty",
"category": "Mail",
"license": "AGPL-3",
"version": "15.0.1.0.0",
"website": "https://github.com/OCA/social",
"author": "Camptocamp, Odoo Community Association (OCA)",
"application": False,
"installable": True,
"depends": [
"mail",
],
"data": ["views/res_config_views.xml"],
}

View File

@ -0,0 +1,2 @@
from . import mail_template
from . import res_config

View File

@ -0,0 +1,35 @@
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
class MailTemplate(models.Model):
_inherit = "mail.template"
def generate_recipients(self, results, res_ids):
"""Use partner's parent email as recipient.
Walk up the hierarchy of recipient partners via `parent_id`
and pick the 1st one having an email.
"""
results = super().generate_recipients(results, res_ids)
use_parent_address = (
self.env["ir.config_parameter"].sudo().get_param("mail.use_parent_address")
)
disabled = self.env.context.get("no_parent_mail_recipient")
if use_parent_address and not disabled:
for res_id, values in results.items():
partner_ids = values.get("partner_ids", [])
partners_with_emails = []
partners = self.env["res.partner"].sudo().browse(partner_ids)
for partner in partners:
if partner.email:
partners_with_emails.append(partner.id)
elif partner.commercial_partner_id.email:
if partner.commercial_partner_id.id not in partners.ids:
partners_with_emails.append(
partner.commercial_partner_id.id
)
results[res_id]["partner_ids"] = partners_with_emails
return results

View File

@ -0,0 +1,16 @@
# Copyright 2022 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
use_parent_mail_address = fields.Boolean(
config_parameter="mail.use_parent_address",
help="""
When checked, for partner without eamil the system will try
to use the email address of the partner's parent.
""",
)

View File

@ -0,0 +1,2 @@
* Thomas Nowicki <thomas.nowicki@camptocamp.com>
* Thierry Ducrest <thierry.ducrest@camptocamp.com>

View File

@ -0,0 +1,3 @@
The development of this module has been financially supported by:
* Camptocamp

View File

@ -0,0 +1,6 @@
This module will check the recipients of an outgoing email.
If a recipient has no email address set, it will use the parent's partner
(if it has an email address) as recipient.
A setting named `Use Parent Mail Address` is available in
the `Discuss` section of the general settings page.

View File

@ -0,0 +1 @@
from . import test_mail_parent_recipient

View File

@ -0,0 +1,79 @@
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.addons.mail.tests.common import MailCommon
class TestMailTemplate(MailCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.env["ir.config_parameter"].set_param("mail.use_parent_address", True)
cls.res_partner = cls.env["res.partner"]
cls.company_test = cls.res_partner.create(
{
"name": "company_name_test",
"email": "company.mail.test@company",
}
)
cls.partner_no_mail = cls.res_partner.create(
{
"name": "partner_1",
"parent_id": cls.company_test.id,
}
)
cls.partner_with_mail = cls.res_partner.create(
{
"name": "partner_2",
"email": "partner.2.mail.test@company",
"parent_id": cls.company_test.id,
}
)
cls.record = cls.env.ref("base.partner_demo")
cls.email_template = (
cls.env["mail.template"]
.with_context(test_parent_mail_recipient=True)
.create(
{
"model_id": cls.env["ir.model"]
.search([("model", "=", "mail.channel")], limit=1)
.id,
"name": "Pigs Template",
"subject": "${object.name}",
"body_html": "${object.description}",
"partner_to": "",
}
)
)
def _get_email_recipient_for(self, partner_to_send_ids):
self.email_template.partner_to = ",".join(
[str(partner_id) for partner_id in partner_to_send_ids]
)
values = self.email_template.generate_email(self.record.id, ["partner_to"])
return values["partner_ids"]
def test_mail_send_to_partner_no_mail(self):
"""Check recipient without email, comapny email is used."""
recipients = self._get_email_recipient_for(self.partner_no_mail.ids)
self.assertEqual(recipients, self.company_test.ids)
def test_mail_send_to_partner_with_mail(self):
"""Check recipient has email, nothing is changed."""
recipients = self._get_email_recipient_for(self.partner_with_mail.ids)
self.assertEqual(recipients, self.partner_with_mail.ids)
def test_mail_send_to_company_test(self):
"""Check company email is used."""
recipients = self._get_email_recipient_for(self.company_test.ids)
self.assertEqual(recipients, self.company_test.ids)
def test_mail_send_to_company_and_partner_no_mail(self):
"""Check a partner is not add twice in recipient list."""
recipients = self._get_email_recipient_for(
[self.partner_no_mail.id, self.company_test.id]
)
self.assertEqual(recipients, self.company_test.ids)

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_configuration_mail_parent_recipient" model="ir.ui.view">
<field name="name">mail.parent.recipient.config.settings</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="mail.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@id='emails']" position='inside'>
<div class="col-12 col-lg-6 o_setting_box" id="mail_parent_recipient_setting">
<div class="o_setting_left_pane">
<field name="use_parent_mail_address" />
</div>
<div class="o_setting_right_pane">
<label for="use_parent_mail_address" />
<div class="text-muted" id="mail_parent_recipient">
Use partner's parent email if recipient partner has not email set.
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>

View File

@ -0,0 +1 @@
../../../../mail_parent_recipient

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)