Skip to content

Commit

Permalink
[FIX] mail_composer_cc_bcc:send RFQ by email
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Dec 17, 2024
1 parent 4185924 commit c28a54c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
32 changes: 23 additions & 9 deletions mail_composer_cc_bcc/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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)
80 changes: 79 additions & 1 deletion mail_composer_cc_bcc/tests/test_mail_cc_bcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -186,3 +188,79 @@ 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")

def test_tracking_mail_without_cc_bcc(self):
if "purchase.order" in self.env:
self.new_po = (

Check warning on line 205 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L205

Added line #L205 was not covered by tests
self.env["purchase.order"]
.create(
{
"partner_id": self.partner.id,
}
)
.with_context(mail_notrack=False)
)
self.cr.precommit.clear()

Check warning on line 214 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L214

Added line #L214 was not covered by tests
# create a PO
# user subscribe to tracking status of PO
self.new_po.message_subscribe(

Check warning on line 217 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L217

Added line #L217 was not covered by tests
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()

Check warning on line 227 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L227

Added line #L227 was not covered by tests
# send RFQ with cc/bcc
form = Form(

Check warning on line 229 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L229

Added line #L229 was not covered by tests
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

Check warning on line 235 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L232-L235

Added lines #L232 - L235 were not covered by tests

with self.mock_mail_gateway(), self.mock_mail_app():
composer._action_send_mail()
self.flush_tracking()
self.assertEqual(

Check warning on line 240 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L237-L240

Added lines #L237 - L240 were not covered by tests
len(self._new_msgs),
2,
"Expected a tracking message and a RFQ message",
)
self.assertEqual(

Check warning on line 245 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L245

Added line #L245 was not covered by tests
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)

Check warning on line 256 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L252-L256

Added lines #L252 - L256 were not covered by tests

# tracking email should not include cc/bcc
tracking_message = self._new_msgs.filtered(

Check warning on line 259 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L259

Added line #L259 was not covered by tests
lambda x: x.message_type == "notification"
)
tracking_field_mail = tracking_message.mail_ids
self.assertEqual(len(tracking_field_mail.recipient_ids), 1)
self.assertEqual(

Check warning on line 264 in mail_composer_cc_bcc/tests/test_mail_cc_bcc.py

View check run for this annotation

Codecov / codecov/patch

mail_composer_cc_bcc/tests/test_mail_cc_bcc.py#L262-L264

Added lines #L262 - L264 were not covered by tests
tracking_field_mail.recipient_ids, self.admin_user.partner_id
)

0 comments on commit c28a54c

Please sign in to comment.