Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -7,38 +7,49 @@ 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):
|
||||
_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}")
|
||||
"""
|
||||
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)}")
|
||||
|
||||
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.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')}")
|
||||
_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)}")
|
||||
|
||||
# 1) Check if this is a MANUAL send
|
||||
# 1) Check if this is a MANUAL send (invoice wizard or compose message)
|
||||
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
|
||||
self.env.context.get("mark_invoice_as_sent")
|
||||
self.env.context.get("force_email") or # From invoice wizard
|
||||
self.env.context.get("mark_invoice_as_sent") # From invoice wizard
|
||||
)
|
||||
|
||||
_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')}")
|
||||
_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')}")
|
||||
|
||||
# 2) For MANUAL sends, allow archived partners
|
||||
# 2) For MANUAL sends, allow archived partners with full context
|
||||
if is_manual_send:
|
||||
_logger.debug("Manual send - allowing archived partners with full context")
|
||||
_logger.info("✓ MANUAL SEND DETECTED - Allowing archived partners")
|
||||
|
||||
# Get recipients with context that allows archived partners
|
||||
recipients = super(
|
||||
MailThread,
|
||||
self.with_context(
|
||||
@@ -48,28 +59,47 @@ class MailThread(models.AbstractModel):
|
||||
)
|
||||
)._notify_get_recipients(message, msg_vals, **kwargs)
|
||||
|
||||
_logger.debug(f"Number of recipients found: {len(recipients)}")
|
||||
_logger.info(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')}")
|
||||
_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', [])}")
|
||||
|
||||
return recipients
|
||||
|
||||
# 3) BLOCK system notifications
|
||||
# 3) BLOCK system notifications completely
|
||||
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.debug("System notification - blocking emails, forcing inbox only")
|
||||
_logger.info("✗ 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
|
||||
_logger.debug("Default behavior - no special handling")
|
||||
return super()._notify_get_recipients(message, msg_vals, **kwargs)
|
||||
# 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)
|
||||
@@ -7,16 +7,20 @@ from odoo import models, api
|
||||
class ResPartner(models.Model):
|
||||
_inherit = "res.partner"
|
||||
|
||||
_logger.info("=== RES.PARTNER CLASS LOADED (allow_mail_archived_partner) ===")
|
||||
|
||||
@api.model
|
||||
def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
|
||||
"""
|
||||
Allow archived partners when context permits.
|
||||
"""
|
||||
_logger.debug("=== RES.PARTNER _search ===")
|
||||
_logger.debug(f"Search args before: {args}")
|
||||
_logger.debug(f"Context include_archived: {self.env.context.get('include_archived_partners')}")
|
||||
_logger.debug(f"Context mail_notify_force: {self.env.context.get('mail_notify_force')}")
|
||||
_logger.debug(f"Context force_email: {self.env.context.get('force_email')}")
|
||||
_logger.info("=== ResPartner._search ===")
|
||||
_logger.info(f"Search args: {args}")
|
||||
_logger.info(f"Context include_archived_partners: {self.env.context.get('include_archived_partners')}")
|
||||
_logger.info(f"Context mail_notify_force: {self.env.context.get('mail_notify_force')}")
|
||||
_logger.info(f"Context force_email: {self.env.context.get('force_email')}")
|
||||
_logger.info(f"Context mark_invoice_as_sent: {self.env.context.get('mark_invoice_as_sent')}")
|
||||
_logger.info(f"Full context active_test: {self.env.context.get('active_test')}")
|
||||
|
||||
# Check if we should include archived partners
|
||||
include_archived = (
|
||||
@@ -27,7 +31,7 @@ class ResPartner(models.Model):
|
||||
)
|
||||
|
||||
if include_archived:
|
||||
_logger.debug("Context flags found - allowing archived partners")
|
||||
_logger.info("✓ CONTEXT FLAGS FOUND - Allowing archived partners")
|
||||
|
||||
# Remove both active and partner_share filters
|
||||
filtered_args = []
|
||||
@@ -35,25 +39,25 @@ class ResPartner(models.Model):
|
||||
if isinstance(arg, (list, tuple)) and len(arg) == 3:
|
||||
# Skip active filters
|
||||
if arg[0] == "active":
|
||||
_logger.debug(f"Removing active filter: {arg}")
|
||||
_logger.info(f" Removing active filter: {arg}")
|
||||
continue
|
||||
# Skip partner_share filter for manual sends to archived partners
|
||||
# Skip partner_share filter for manual sends
|
||||
if arg[0] == "partner_share" and arg[1] == "=" and arg[2] is True:
|
||||
_logger.debug(f"Removing partner_share filter: {arg}")
|
||||
_logger.info(f" Removing partner_share filter: {arg}")
|
||||
continue
|
||||
|
||||
filtered_args.append(arg)
|
||||
|
||||
args = filtered_args
|
||||
self = self.with_context(active_test=False)
|
||||
_logger.debug(f"Search args after: {args}")
|
||||
_logger.info(f"Search args after cleanup: {args}")
|
||||
|
||||
result = super()._search(args, offset, limit, order, count, access_rights_uid)
|
||||
|
||||
if not count:
|
||||
result_ids = list(result)
|
||||
_logger.debug(f"Search returned {len(result_ids)} results")
|
||||
_logger.info(f"Search returned {len(result_ids)} results")
|
||||
if result_ids:
|
||||
_logger.debug(f"First result IDs: {result_ids[:5]}")
|
||||
_logger.info(f"First 5 result IDs: {result_ids[:5]}")
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user