diff --git a/mail_parent_recipient/__init__.py b/mail_parent_recipient/__init__.py new file mode 100644 index 00000000..cde864ba --- /dev/null +++ b/mail_parent_recipient/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/mail_parent_recipient/__manifest__.py b/mail_parent_recipient/__manifest__.py new file mode 100644 index 00000000..95e7fc5e --- /dev/null +++ b/mail_parent_recipient/__manifest__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Mail parent recipient", + "summary": "Sending the email in any case by the main partner", + "category": "Mail", + "license": "LGPL-3", + "version": "10.0.1.0.0", + "website": "https://github.com/OCA/social", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "pre_init_hook": "pre_init_hook", + "post_init_hook": "post_init_hook", + "post_load": "post_load", + "uninstall_hook": "uninstall_hook", + "depends": [ + 'mail', + ] +} diff --git a/mail_parent_recipient/models/__init__.py b/mail_parent_recipient/models/__init__.py new file mode 100644 index 00000000..2eb803a7 --- /dev/null +++ b/mail_parent_recipient/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import mail_template diff --git a/mail_parent_recipient/models/mail_template.py b/mail_parent_recipient/models/mail_template.py new file mode 100644 index 00000000..7c6c0487 --- /dev/null +++ b/mail_parent_recipient/models/mail_template.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import models, api + + +class MailTemplate(models.Model): + + _inherit = 'mail.template' + + @api.multi + def generate_recipients(self, results, res_ids): + """ + Modify the code that set the default recipient in an email to the first + partner having an email set following the parents tree. + If no parent with an email is found, + it reverts to the popup asking to set an email for the current partner + """ + results = super(MailTemplate, self).generate_recipients( + results, + res_ids + ) + for res_id, values in results.iteritems(): + partner_ids = values.get('partner_ids', []) + partners_with_emails = [] + for partner in self.env['res.partner'].sudo().browse(partner_ids): + current = partner + while current: + if current.email: + break + current = current.parent_id + partners_with_emails.append(current.id or partner.id) + + results[res_id]['partner_ids'] = partners_with_emails + return results diff --git a/mail_parent_recipient/readme/CONTRIBUTORS.rst b/mail_parent_recipient/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..0a9139f7 --- /dev/null +++ b/mail_parent_recipient/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Thomas Nowicki diff --git a/mail_parent_recipient/readme/CREDITS.rst b/mail_parent_recipient/readme/CREDITS.rst new file mode 100644 index 00000000..ea08384b --- /dev/null +++ b/mail_parent_recipient/readme/CREDITS.rst @@ -0,0 +1,4 @@ + +The development of this module has been financially supported by: + +* Camptocamp Thomas Nowicki diff --git a/mail_parent_recipient/readme/DESCRIPTION.rst b/mail_parent_recipient/readme/DESCRIPTION.rst new file mode 100644 index 00000000..d8c106f8 --- /dev/null +++ b/mail_parent_recipient/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This modules set the default recipient in an email to the first partner having +an email set following the parents tree. If no parent with an email is found, +it reverts to the popup asking to set an email for the current partner. diff --git a/mail_parent_recipient/readme/HISTORY.rst b/mail_parent_recipient/readme/HISTORY.rst new file mode 100644 index 00000000..e69de29b diff --git a/mail_parent_recipient/tests/__init__.py b/mail_parent_recipient/tests/__init__.py new file mode 100644 index 00000000..8b3803cf --- /dev/null +++ b/mail_parent_recipient/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_mail_parent_recipient diff --git a/mail_parent_recipient/tests/test_mail_parent_recipient.py b/mail_parent_recipient/tests/test_mail_parent_recipient.py new file mode 100644 index 00000000..e245e17f --- /dev/null +++ b/mail_parent_recipient/tests/test_mail_parent_recipient.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.addons.mail.tests.common import TestMail + + +class TestMailTemplate(TestMail): + + def setUp(self): + super(TestMailTemplate, self).setUp() + + self.Users = self.env['res.users'] + self.res_parter = self.env['res.partner'] + self.mail_template = self.env['mail.template'] + self.mail_comp_msg = self.env['mail.compose.message'] + + # Company + company_vals = { + 'name': 'company_name_test', + 'email': 'company.mail.test@company' + } + + self.company_test = self.res_parter.create(company_vals) + + # Partners test 1 without email + partner_no_mail_vals = { + 'name': 'partner_1', + 'parent_id': self.company_test.id, + } + + + # Partners test 2 with email + partner_with_mail_vals = { + 'name': 'partner_2', + 'email': 'partner.2.mail.test@company', + 'parent_id': self.company_test.id, + } + + self.partner_no_mail = self.res_parter.create(partner_no_mail_vals) + self.partner_with_mail = self.res_parter.create(partner_with_mail_vals) + + self.email_template = self.env['mail.template'].create({ + 'model_id': self.env['ir.model'].search([ + ('model', '=', 'mail.channel') + ], limit=1).id, + 'name': 'Pigs Template', + 'subject': '${object.name}', + 'body_html': '${object.description}', + 'user_signature': False, + 'partner_to': '${object.partner_id.id}', # TODO change + }) + + self.composer = self.mail_comp_msg.with_context({ + 'default_composition_mode': 'comment', + 'default_model': 'mail.channel', + 'default_use_template': True, + }).create({ + 'subject': 'Forget me subject', + 'body': 'Dummy body', + 'template_id': self.email_template.id + }) + + # def test_1_mail_send_to_partner_no_mail(self): + # """ + # Mail should only send with company mail + # even if is sended to partner_no_mail + # """ + # import pdb; pdb.set_trace() + # mail_id = self.email_template.send_mail(self.partner_no_mail.id) + # mail = self.env['mail.mail'].browse(mail_id) + # + # self.assertEqual(mail.recipient_ids, self.company_test.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id) + # self.assertEqual(len(mail.recipient_ids), 1) + # + # def test_2_mail_send_to_partner_with_mail(self): + # """ + # Mail should only send with company mail + # even if is sended to partner_with_mail + # """ + # mail_id = self.email_template.send_mail(self.partner_with_mail.id) + # mail = self.env['mail.mail'].browse(mail_id) + # + # self.assertEqual(mail.recipient_ids, self.company_test.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id) + # self.assertEqual(len(mail.recipient_ids), 1) + # + # def test_3_mail_send_to_company_test(self): + # """ + # Mail should only send with company mail + # """ + # mail_id = self.email_template.send_mail(self.company_test.id) + # mail = self.env['mail.mail'].browse(mail_id) + # + # self.assertEqual(mail.recipient_ids, self.company_test.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id) + # self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id) + # self.assertEqual(len(mail.recipient_ids), 1)