Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions mail_post_defer/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class MailThread(models.AbstractModel):
def message_post(self, **kwargs):
"""Post messages using queue by default."""
_self = self
force_send = self.env.context.get("mail_notify_force_send") or kwargs.get(
"force_send", False
force_send = (
self.env.context.get("mail_notify_force_send")
or self.env.context.get("force_send")
or kwargs.get("force_send", False)
)
kwargs.setdefault("force_send", force_send)
if not force_send:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: instead of the above change, just add this:

Suggested change
if not force_send:
if not force_send or self.env.context.get("force_send"):

Explanation: The context is already kept in sub-calls. We just need to honor the context if present, but we don't need to alter the kwarg.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

I believe this change is necessary.

_notify_record_by_email reads context only for
mail_notify_force_send, not force_send:

force_send = self.env.context.get('mail_notify_force_send', force_send)

This PR reads force_send from context early so that
kwargs.setdefault propagates True to
_notify_record_by_email, and emails.send() is
actually called.

Please let me know if my understanding is correct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose @yajo meant if not force_send and not self.env.context.get("force_send"):, but it still doesn't look like it would work in _notify_record_by_email, since the force_send kwarg would be set to False and would prevail. I think the approach of this PR is correct.

Expand Down
24 changes: 24 additions & 0 deletions mail_post_defer/tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ def test_forced_context(self):
fields_values={"scheduled_date": False},
)

def test_forced_context_with_template(self):
"""A forced send via context is sent directly when using a template."""
with self.mock_mail_gateway():
mail_template = self.env["mail.template"].create(
{
"model_id": self.env.ref("base.model_res_partner").id,
"body_html": "<p>test body</p>",
"partner_to": f"{self.partner_employee.id}",
}
)
self.partner_portal.with_context(
force_send=True
).message_post_with_template(
mail_template.id,
composition_mode="comment",
)
self.assertMailMail(
self.partner_employee,
"sent",
author=self.env.user.partner_id,
content="test body",
fields_values={"scheduled_date": False},
)

def test_no_msg_edit(self):
"""Cannot update messages.

Expand Down
Loading