From a98eb90d34fc576e4bac695dcba943ad89569cee Mon Sep 17 00:00:00 2001 From: trisdoan Date: Tue, 22 Oct 2024 09:56:46 +0700 Subject: [PATCH] [FIX] mail_composer_cc_bcc:send RFQ by email --- mail_composer_cc_bcc/models/mail_thread.py | 32 ++++++-- .../tests/test_mail_cc_bcc.py | 82 ++++++++++++++++++- 2 files changed, 104 insertions(+), 10 deletions(-) diff --git a/mail_composer_cc_bcc/models/mail_thread.py b/mail_composer_cc_bcc/models/mail_thread.py index 964987ac52..9bf15a7264 100644 --- a/mail_composer_cc_bcc/models/mail_thread.py +++ b/mail_composer_cc_bcc/models/mail_thread.py @@ -12,12 +12,15 @@ class MailThread(models.AbstractModel): def _message_create(self, values_list): context = self.env.context res = super()._message_create(values_list) - partners_cc = context.get("partner_cc_ids", None) - if partners_cc: - res.recipient_cc_ids = partners_cc - partners_bcc = context.get("partner_bcc_ids", None) - if partners_bcc: - res.recipient_bcc_ids = partners_bcc + for message in res: + if message.message_type == "notification": + continue + partners_cc = context.get("partner_cc_ids", None) + if partners_cc: + message.recipient_cc_ids = partners_cc + partners_bcc = context.get("partner_bcc_ids", None) + if partners_bcc: + message.recipient_bcc_ids = partners_bcc return res def _notify_by_email_get_base_mail_values(self, message, additional_values=None): @@ -26,10 +29,11 @@ def _notify_by_email_get_base_mail_values(self, message, additional_values=None) can be sent to those addresses. """ context = self.env.context - res = super()._notify_by_email_get_base_mail_values( message, additional_values=additional_values ) + if context.get("skip_adding_cc_bcc", False): + return res partners_cc = context.get("partner_cc_ids", None) if partners_cc: res["email_cc"] = format_emails(partners_cc) @@ -48,7 +52,8 @@ def _notify_get_recipients(self, message, msg_vals, **kwargs): rdata = super()._notify_get_recipients(message, msg_vals, **kwargs) context = self.env.context is_from_composer = context.get("is_from_composer", False) - if not is_from_composer: + skip_adding_cc_bcc = context.get("skip_adding_cc_bcc", False) + if not is_from_composer or skip_adding_cc_bcc: return rdata for pdata in rdata: pdata["type"] = "customer" @@ -96,6 +101,9 @@ def _notify_by_email_get_final_mail_values( recipient_ids, base_mail_values, additional_values=additional_values ) context = self.env.context + skip_adding_cc_bcc = context.get("skip_adding_cc_bcc", False) + if skip_adding_cc_bcc: + return res r_ids = list(recipient_ids) partners_cc = context.get("partner_cc_ids", None) if partners_cc: @@ -114,7 +122,8 @@ def _notify_get_recipients_classify( recipient_data, model_name, msg_vals=msg_vals ) is_from_composer = self.env.context.get("is_from_composer", False) - if not is_from_composer: + skip_adding_cc_bcc = self.env.context.get("skip_adding_cc_bcc", False) + if not is_from_composer or skip_adding_cc_bcc: return res ids = [] customer_data = None @@ -130,3 +139,8 @@ def _notify_get_recipients_classify( else: customer_data["recipients"] += ids return [customer_data] + + def _notify_thread(self, message, msg_vals=False, **kwargs): + if message.message_type == "notification": + self = self.with_context(skip_adding_cc_bcc=True) + return super(MailThread, self)._notify_thread(message, msg_vals, **kwargs) diff --git a/mail_composer_cc_bcc/tests/test_mail_cc_bcc.py b/mail_composer_cc_bcc/tests/test_mail_cc_bcc.py index 5e1403ea82..86e838db3e 100644 --- a/mail_composer_cc_bcc/tests/test_mail_cc_bcc.py +++ b/mail_composer_cc_bcc/tests/test_mail_cc_bcc.py @@ -5,9 +5,11 @@ import inspect from odoo import tools -from odoo.tests import Form +from odoo.tests import Form, tagged +from odoo.tests.common import TransactionCase from odoo.addons.mail.models.mail_mail import MailMail as upstream +from odoo.addons.mail.tests.common import MailCase from odoo.addons.mail.tests.test_mail_composer import TestMailComposer VALID_HASHES = [ @@ -179,3 +181,81 @@ def test_mail_without_cc_bcc(self): if subject == mail.get("subject"): sent_mails += 1 self.assertEqual(sent_mails, 1) + + +@tagged("-at_install", "post_install") +class TestMailComposerCcBccWithTracking(TransactionCase, MailCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env.ref("base.res_partner_address_31") + cls.partner_cc = cls.env.ref("base.partner_demo") + cls.partner_bcc = cls.env.ref("base.res_partner_main2") + cls.admin_user = cls.env.ref("base.user_admin") + + if "purchase.order" in cls.env: + cls.new_po = ( + cls.env["purchase.order"] + .create( + { + "partner_id": cls.partner.id, + } + ) + .with_context(mail_notrack=False) + ) + + def test_tracking_mail_without_cc_bcc(self): + if self.new_po: + self.cr.precommit.clear() + # create a PO + # user subscribe to tracking status of PO + self.new_po.message_subscribe( + partner_ids=self.admin_user.partner_id.ids, + subtype_ids=( + ( + self.env.ref("purchase.mt_rfq_sent") + | self.env.ref("purchase.mt_rfq_confirmed") + ).ids + ), + ) + + composer_ctx = self.new_po.action_rfq_send() + # send RFQ with cc/bcc + form = Form( + self.env["mail.compose.message"].with_context(**composer_ctx["context"]) + ) + composer = form.save() + composer.partner_ids = self.partner + composer.partner_cc_ids = self.partner_cc + composer.partner_bcc_ids = self.partner_bcc + + with self.mock_mail_gateway(), self.mock_mail_app(): + composer._action_send_mail() + self.flush_tracking() + self.assertEqual( + len(self._new_msgs), + 2, + "Expected a tracking message and a RFQ message", + ) + self.assertEqual( + self.ref("purchase.mt_rfq_sent"), + self._new_msgs[1].subtype_id.id, + "Expected a tracking message", + ) + + # RFQ email should include cc/bcc + rfq_message = self._new_msgs.filtered(lambda x: x.message_type == "comment") + self.assertEqual(len(rfq_message.notified_partner_ids), 3) + self.assertEqual(len(rfq_message.notification_ids), 3) + rfq_mail = rfq_message.mail_ids + self.assertEqual(len(rfq_mail.recipient_ids), 3) + + # tracking email should not include cc/bcc + tracking_message = self._new_msgs.filtered( + lambda x: x.message_type == "notification" + ) + tracking_field_mail = tracking_message.mail_ids + self.assertEqual(len(tracking_field_mail.recipient_ids), 1) + self.assertEqual( + tracking_field_mail.recipient_ids, self.admin_user.partner_id + )