Fix allow_mail_archived_partner causing system notifications to send emails

This commit is contained in:
Kristof Bernaert
2026-01-26 01:33:32 +01:00
parent c5a92d81df
commit a0c3704121
2 changed files with 61 additions and 54 deletions
@@ -4,60 +4,26 @@ from odoo import models, api
class MailComposeMessage(models.TransientModel):
_inherit = "mail.compose.message"
@api.model
def default_get(self, fields):
"""
For sales orders and other models using generic composer.
"""
res = super().default_get(fields)
model = self.env.context.get("active_model")
res_id = self.env.context.get("active_id")
# For sales orders
if model == "sale.order" and res_id:
order = self.env["sale.order"].with_context(
active_test=False
).browse(res_id)
if order.exists() and order.partner_id and order.partner_id.email:
res["partner_ids"] = [(6, 0, [order.partner_id.id])]
return res
def _prepare_mail_values(self, res_ids):
"""
For explicit user sends from sales orders.
"""
model = self.env.context.get("active_model") or self.model
if model in ["sale.order", "account.move"]:
return super(
MailComposeMessage,
self.with_context(
active_test=False,
include_archived_partners=True,
mail_notify_force=True,
)
)._prepare_mail_values(res_ids)
return super()._prepare_mail_values(res_ids)
def _prepare_recipient_values(self, partner):
"""
For sales orders and generic composers.
Handle archived partners when context allows.
"""
model = self.env.context.get("active_model") or self.model
if (
model in ["sale.order", "account.move"]
and self.env.context.get("include_archived_partners")
and partner
):
# Use parent method with proper context
return super(
MailComposeMessage,
self.with_context(active_test=False)
)._prepare_recipient_values(partner)
# Check if we should include archived partners
# Look for context flags from account.invoice.send OR direct context
include_archived = (
self.env.context.get("include_archived_partners") or
self.env.context.get("mail_notify_force") or
(self.env.context.get("active_model") in ["account.move", "sale.order"] and
not getattr(partner, 'active', True))
)
if include_archived and partner and not partner.active:
# Return complete data for archived partner
return {
"partner_id": partner.id,
"email": partner.email,
"name": partner.name,
"lang": partner.lang or self.env.context.get("lang") or "en_US",
}
return super()._prepare_recipient_values(partner)