From 02a172f582dd134bf3a67d9c39ee4cabeef8e93f Mon Sep 17 00:00:00 2001 From: Kristof Bernaert Date: Mon, 26 Jan 2026 02:37:29 +0100 Subject: [PATCH] Fix allow_mail_archived_partner causing system notifications to send emails --- .../models/account_move_send.py | 48 +++++++++++++++++-- .../models/mail_compose_message.py | 13 ++++- .../models/mail_thread.py | 39 ++++++++++----- 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/allow_mail_archived_partner/models/account_move_send.py b/allow_mail_archived_partner/models/account_move_send.py index 020a878..14a9727 100755 --- a/allow_mail_archived_partner/models/account_move_send.py +++ b/allow_mail_archived_partner/models/account_move_send.py @@ -52,12 +52,54 @@ class AccountInvoiceSend(models.TransientModel): """ Pass context to mail composer to allow archived partners. """ - _logger.debug("Passing context to allow archived partners") + _logger.debug("=== _get_composer_values ===") + _logger.debug(f"Passing context to allow archived partners") + # FIX: Changed include_archived_partner to include_archived_partners (plural) return super( AccountInvoiceSend, self.with_context( active_test=False, - include_archived_partner=True, + include_archived_partners=True, # FIXED: plural 's' mail_notify_force=True, ) - )._get_composer_values(res_ids, template) \ No newline at end of file + )._get_composer_values(res_ids, template) + + def action_send_and_print(self): + """ + Override send action to add logging and ensure context is passed. + """ + _logger.debug("=== ACCOUNT.INVOICE.SEND action_send_and_print ===") + _logger.debug(f"Context before send: {dict(self.env.context)}") + _logger.debug(f"Partner IDs: {self.partner_ids.ids}") + + # Ensure context is passed when calling action + result = super( + AccountInvoiceSend, + self.with_context( + active_test=False, + include_archived_partners=True, + mail_notify_force=True, + force_email=True, # Ensure mail_thread.py recognizes this + mark_invoice_as_sent=True, # Ensure mail_thread.py recognizes this + ) + ).action_send_and_print() + + _logger.debug("Email send action completed") + return result + + # Also override the regular send action if it exists + def action_send(self): + """ + Override send action (without print) to ensure context. + """ + _logger.debug("=== ACCOUNT.INVOICE.SEND action_send ===") + return super( + AccountInvoiceSend, + self.with_context( + active_test=False, + include_archived_partners=True, + mail_notify_force=True, + force_email=True, + mark_invoice_as_sent=True, + ) + ).action_send() \ No newline at end of file diff --git a/allow_mail_archived_partner/models/mail_compose_message.py b/allow_mail_archived_partner/models/mail_compose_message.py index 1468970..80abfb2 100755 --- a/allow_mail_archived_partner/models/mail_compose_message.py +++ b/allow_mail_archived_partner/models/mail_compose_message.py @@ -83,4 +83,15 @@ class MailComposeMessage(models.TransientModel): } # Default behavior for active partners or non-manual sends - return super()._prepare_recipient_values(partner) \ No newline at end of file + return super()._prepare_recipient_values(partner) + + + def action_send_mail(self): + """ + Override send action to log and ensure context. + """ + _logger.debug("=== MAIL.COMPOSE.MESSAGE action_send_mail ===") + _logger.debug(f"Context: {dict(self.env.context)}") + _logger.debug(f"Partner IDs: {self.partner_ids.ids}") + + return super().action_send_mail() \ No newline at end of file diff --git a/allow_mail_archived_partner/models/mail_thread.py b/allow_mail_archived_partner/models/mail_thread.py index 9e0ca4f..e72d28b 100755 --- a/allow_mail_archived_partner/models/mail_thread.py +++ b/allow_mail_archived_partner/models/mail_thread.py @@ -8,30 +8,38 @@ class MailThread(models.AbstractModel): _inherit = "mail.thread" def _notify_thread(self, message, msg_vals=False, **kwargs): + _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 """ - # 1) Check if this is a MANUAL send (from invoice wizard or compose message) + _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 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.debug(f"MailThread._notify_get_recipients for {self._name}") _logger.debug(f"Is manual send: {is_manual_send}") - _logger.debug(f"Context: 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')}, " 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.debug(f"Manual send detected for {self._name}, allowing archived partners") - return super( + _logger.debug("Manual send - allowing archived partners with full context") + recipients = super( MailThread, self.with_context( active_test=False, @@ -39,8 +47,15 @@ class MailThread(models.AbstractModel): mail_notify_force=True, ) )._notify_get_recipients(message, msg_vals, **kwargs) + + _logger.debug(f"Number of recipients found: {len(recipients)}") + for i, recipient in enumerate(recipients): + _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 @@ -49,12 +64,12 @@ class MailThread(models.AbstractModel): ) if is_system_notification: - _logger.debug("Blocking system notification email") + _logger.debug("System notification - blocking emails, forcing inbox only") recipients = super()._notify_get_recipients(message, msg_vals, **kwargs) - # Force inbox only, no email for recipient in recipients: recipient["notif"] = "inbox" return recipients - # 4) Default behavior for non-manual, non-system sends + # 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