Fix allow_mail_archived_partner causing system notifications to send emails

This commit is contained in:
Kristof Bernaert
2026-01-26 02:50:15 +01:00
parent cb52e822d7
commit c599b24839
@@ -7,49 +7,38 @@ from odoo import models
class MailThread(models.AbstractModel): class MailThread(models.AbstractModel):
_inherit = "mail.thread" _inherit = "mail.thread"
_logger.info("=== MAILTHREAD CLASS LOADED (allow_mail_archived_partner) ===")
def _notify_thread(self, message, msg_vals=False, **kwargs): def _notify_thread(self, message, msg_vals=False, **kwargs):
""" _logger.debug("=== MailThread._notify_thread ===")
Log notification thread calls. _logger.debug(f"Message type: {getattr(message, 'message_type', 'Unknown')}")
""" _logger.debug(f"Message subject: {getattr(message, 'subject', 'No subject')}")
_logger.info("=== MailThread._notify_thread ===") _logger.debug(f"Model: {self._name}")
_logger.info(f"Model: {self._name}")
_logger.info(f"Message type: {getattr(message, 'message_type', 'Unknown')}")
_logger.info(f"Message subject: {getattr(message, 'subject', 'No subject')}")
_logger.info(f"Context: {dict(self.env.context)}")
return super()._notify_thread(message, msg_vals=msg_vals, **kwargs) return super()._notify_thread(message, msg_vals=msg_vals, **kwargs)
def _notify_get_recipients(self, message, msg_vals, **kwargs): def _notify_get_recipients(self, message, msg_vals, **kwargs):
""" """
Allow archived partners ONLY for explicit manual sends. Allow archived partners ONLY for explicit manual sends
""" """
_logger.info("=== MailThread._notify_get_recipients ===") _logger.debug("=== MailThread._notify_get_recipients ===")
_logger.info(f"Model: {self._name}") _logger.debug(f"Model: {self._name}")
_logger.info(f"Message type: {getattr(message, 'message_type', 'Unknown')}") _logger.debug(f"Message type: {getattr(message, 'message_type', 'Unknown')}")
_logger.info(f"Message subject: {getattr(message, 'subject', 'No subject')}") _logger.debug(f"Message subject: {getattr(message, 'subject', 'No subject')}")
_logger.info(f"Full context: {dict(self.env.context)}")
# 1) Check if this is a MANUAL send (invoice wizard or compose message) # 1) Check if this is a MANUAL send
is_manual_send = ( is_manual_send = (
self.env.context.get("mail_notify_force") or self.env.context.get("mail_notify_force") or
self.env.context.get("include_archived_partners") or self.env.context.get("include_archived_partners") or
self.env.context.get("force_email") or # From invoice wizard self.env.context.get("force_email") or
self.env.context.get("mark_invoice_as_sent") # From invoice wizard self.env.context.get("mark_invoice_as_sent")
) )
_logger.info(f"Is manual send? {is_manual_send}") _logger.debug(f"Is manual send: {is_manual_send}")
_logger.info(f"Context flags - mail_notify_force: {self.env.context.get('mail_notify_force')}") _logger.debug(f"Context flags: mail_notify_force={self.env.context.get('mail_notify_force')}, "
_logger.info(f"Context flags - include_archived_partners: {self.env.context.get('include_archived_partners')}") f"include_archived={self.env.context.get('include_archived_partners')}, "
_logger.info(f"Context flags - force_email: {self.env.context.get('force_email')}") f"force_email={self.env.context.get('force_email')}")
_logger.info(f"Context flags - mark_invoice_as_sent: {self.env.context.get('mark_invoice_as_sent')}")
# 2) For MANUAL sends, allow archived partners with full context # 2) For MANUAL sends, allow archived partners
if is_manual_send: if is_manual_send:
_logger.info("✓ MANUAL SEND DETECTED - Allowing archived partners") _logger.debug("Manual send - allowing archived partners with full context")
# Get recipients with context that allows archived partners
recipients = super( recipients = super(
MailThread, MailThread,
self.with_context( self.with_context(
@@ -59,47 +48,28 @@ class MailThread(models.AbstractModel):
) )
)._notify_get_recipients(message, msg_vals, **kwargs) )._notify_get_recipients(message, msg_vals, **kwargs)
_logger.info(f"Number of recipients found: {len(recipients)}") _logger.debug(f"Number of recipients found: {len(recipients)}")
for i, recipient in enumerate(recipients): for i, recipient in enumerate(recipients):
_logger.info(f"Recipient {i}: partner_id={recipient.get('partner_id')}, " _logger.debug(f"Recipient {i}: partner_id={recipient.get('partner_id')}, "
f"notif={recipient.get('notif')}, email={recipient.get('email')}, " f"notif={recipient.get('notif')}, email={recipient.get('email')}")
f"groups={recipient.get('groups', [])}")
return recipients return recipients
# 3) BLOCK system notifications completely # 3) BLOCK system notifications
is_system_notification = ( is_system_notification = (
getattr(message, "message_type", None) == "notification" or getattr(message, "message_type", None) == "notification" or
(getattr(message, "author_id", False) and getattr(message, "author_id", False) and
message.author_id == self.env.ref("base.partner_root", raise_if_not_found=False)) or message.author_id == self.env.ref("base.partner_root", raise_if_not_found=False) or
(msg_vals and msg_vals.get("message_type") == "notification") (msg_vals and msg_vals.get("message_type") == "notification")
) )
if is_system_notification: if is_system_notification:
_logger.info("✗ SYSTEM NOTIFICATION - Blocking emails, forcing inbox only") _logger.debug("System notification - blocking emails, forcing inbox only")
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs) recipients = super()._notify_get_recipients(message, msg_vals, **kwargs)
_logger.info(f"System notification recipients before blocking: {len(recipients)}")
# Force inbox only, no email
for recipient in recipients: for recipient in recipients:
recipient["notif"] = "inbox" recipient["notif"] = "inbox"
_logger.info(f"Blocked email for recipient: partner_id={recipient.get('partner_id')}")
return recipients return recipients
# 4) Default behavior for non-manual, non-system sends # 4) Default behavior
_logger.info("○ DEFAULT BEHAVIOR - No special handling") _logger.debug("Default behavior - no special handling")
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs) return super()._notify_get_recipients(message, msg_vals, **kwargs)
_logger.info(f"Default recipients found: {len(recipients)}")
return recipients
def _message_post(self, **kwargs):
"""
Log message post calls to trace email flow.
"""
_logger.info("=== MailThread._message_post ===")
_logger.info(f"Model: {self._name}")
_logger.info(f"Kwargs: { {k: v for k, v in kwargs.items() if k != 'body'} }")
_logger.info(f"Context: {dict(self.env.context)}")
return super()._message_post(**kwargs)