Fix allow_mail_archived_partner causing system notifications to send emails

This commit is contained in:
Kristof Bernaert
2026-01-26 04:33:59 +01:00
parent c94fba0f02
commit c175e33c03
5 changed files with 233 additions and 52 deletions
@@ -1,3 +1,6 @@
import logging
_logger = logging.getLogger(__name__)
from odoo import models, api
@@ -10,6 +13,8 @@ class MailComposeMessage(models.TransientModel):
Pre-fill email wizard with archived partners for sales orders.
For invoices, this is handled by account.invoice.send.
"""
_logger.debug("=== MAIL.COMPOSE.MESSAGE DEFAULT_GET ===")
res = super().default_get(fields)
model = self.env.context.get("active_model")
@@ -17,6 +22,8 @@ class MailComposeMessage(models.TransientModel):
# Only handle sales orders here - invoices use account.invoice.send
if model == "sale.order" and res_id:
_logger.debug(f"Processing sales order {res_id}")
# Find order with archived partners allowed
order = self.env["sale.order"].with_context(
active_test=False
@@ -24,6 +31,7 @@ class MailComposeMessage(models.TransientModel):
if order.exists() and order.partner_id:
res["partner_ids"] = [(6, 0, [order.partner_id.id])]
_logger.debug(f"Pre-filled archived partner {order.partner_id.id} for sales order")
return res
@@ -32,11 +40,14 @@ class MailComposeMessage(models.TransientModel):
Set context flags for manual email sends.
This ensures archived partners are allowed during email sending.
"""
_logger.debug("=== _prepare_mail_values ===")
model = self.env.context.get("active_model") or self.model
# Only for sales orders and account moves (invoices)
# Note: account.invoice.send should handle invoices, but keep this as fallback
if model in ["sale.order", "account.move"]:
_logger.debug(f"Setting context for {model} to allow archived partners")
return super(
MailComposeMessage,
self.with_context(
@@ -63,6 +74,7 @@ class MailComposeMessage(models.TransientModel):
# If it's a manual send and partner is archived, include them
if is_manual_send and partner and not partner.active:
_logger.debug(f"Including archived partner {partner.name} in email")
return {
"partner_id": partner.id,
"email": partner.email,
@@ -71,4 +83,21 @@ class MailComposeMessage(models.TransientModel):
}
# Default behavior for active partners or non-manual sends
return super()._prepare_recipient_values(partner)
return super()._prepare_recipient_values(partner)
def action_send_mail(self):
"""
Log when email is actually sent.
"""
_logger.info("🔥 MAIL.COMPOSE.MESSAGE action_send_mail")
_logger.info(f"Partner IDs: {self.partner_ids.ids}")
_logger.info(f"Context: {dict(self.env.context)}")
return super().action_send_mail()
def _action_send_mail(self, auto_commit=False):
"""
Another possible send method.
"""
_logger.info("🔥 MAIL.COMPOSE.MESSAGE _action_send_mail")
return super()._action_send_mail(auto_commit=auto_commit)