From c599b248395e0049566dea3883edf25dd9bfcd23 Mon Sep 17 00:00:00 2001 From: Kristof Bernaert Date: Mon, 26 Jan 2026 02:50:15 +0100 Subject: [PATCH] Fix allow_mail_archived_partner causing system notifications to send emails --- .../models/mail_thread.py | 86 ++++++------------- 1 file changed, 28 insertions(+), 58 deletions(-) diff --git a/allow_mail_archived_partner/models/mail_thread.py b/allow_mail_archived_partner/models/mail_thread.py index 0120252..e72d28b 100755 --- a/allow_mail_archived_partner/models/mail_thread.py +++ b/allow_mail_archived_partner/models/mail_thread.py @@ -7,49 +7,38 @@ from odoo import models class MailThread(models.AbstractModel): _inherit = "mail.thread" - _logger.info("=== MAILTHREAD CLASS LOADED (allow_mail_archived_partner) ===") - def _notify_thread(self, message, msg_vals=False, **kwargs): - """ - Log notification thread calls. - """ - _logger.info("=== MailThread._notify_thread ===") - _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)}") - + _logger.debug("=== MailThread._notify_thread ===") + _logger.debug(f"Message type: {getattr(message, 'message_type', 'Unknown')}") + _logger.debug(f"Message subject: {getattr(message, 'subject', 'No subject')}") + _logger.debug(f"Model: {self._name}") return super()._notify_thread(message, msg_vals=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.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"Full context: {dict(self.env.context)}") + _logger.debug("=== MailThread._notify_get_recipients ===") + _logger.debug(f"Model: {self._name}") + _logger.debug(f"Message type: {getattr(message, 'message_type', 'Unknown')}") + _logger.debug(f"Message subject: {getattr(message, 'subject', 'No subject')}") - # 1) Check if this is a MANUAL send (invoice wizard or compose message) + # 1) Check if this is a MANUAL send is_manual_send = ( self.env.context.get("mail_notify_force") or self.env.context.get("include_archived_partners") or - self.env.context.get("force_email") or # From invoice wizard - self.env.context.get("mark_invoice_as_sent") # From invoice wizard + self.env.context.get("force_email") or + self.env.context.get("mark_invoice_as_sent") ) - _logger.info(f"Is manual send? {is_manual_send}") - _logger.info(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')}") - _logger.info(f"Context flags - 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')}") + _logger.debug(f"Is manual send: {is_manual_send}") + _logger.debug(f"Context flags: mail_notify_force={self.env.context.get('mail_notify_force')}, " + f"include_archived={self.env.context.get('include_archived_partners')}, " + f"force_email={self.env.context.get('force_email')}") - # 2) For MANUAL sends, allow archived partners with full context + # 2) For MANUAL sends, allow archived partners if is_manual_send: - _logger.info("✓ MANUAL SEND DETECTED - Allowing archived partners") - - # Get recipients with context that allows archived partners + _logger.debug("Manual send - allowing archived partners with full context") recipients = super( MailThread, self.with_context( @@ -59,47 +48,28 @@ class MailThread(models.AbstractModel): ) )._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): - _logger.info(f"Recipient {i}: partner_id={recipient.get('partner_id')}, " - f"notif={recipient.get('notif')}, email={recipient.get('email')}, " - f"groups={recipient.get('groups', [])}") + _logger.debug(f"Recipient {i}: partner_id={recipient.get('partner_id')}, " + f"notif={recipient.get('notif')}, email={recipient.get('email')}") return recipients - # 3) BLOCK system notifications completely + # 3) BLOCK system notifications is_system_notification = ( getattr(message, "message_type", None) == "notification" or - (getattr(message, "author_id", False) and - message.author_id == self.env.ref("base.partner_root", raise_if_not_found=False)) or + getattr(message, "author_id", False) and + message.author_id == self.env.ref("base.partner_root", raise_if_not_found=False) or (msg_vals and msg_vals.get("message_type") == "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) - _logger.info(f"System notification recipients before blocking: {len(recipients)}") - - # Force inbox only, no email for recipient in recipients: recipient["notif"] = "inbox" - _logger.info(f"Blocked email for recipient: partner_id={recipient.get('partner_id')}") - return recipients - # 4) Default behavior for non-manual, non-system sends - _logger.info("○ DEFAULT BEHAVIOR - No special handling") - recipients = 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) \ No newline at end of file + # 4) Default behavior + _logger.debug("Default behavior - no special handling") + return super()._notify_get_recipients(message, msg_vals, **kwargs) \ No newline at end of file