Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -28,85 +28,76 @@ class MailThread(models.AbstractModel):
|
||||
|
||||
def _notify_get_recipients(self, message, msg_vals, **kwargs):
|
||||
"""
|
||||
Allow archived partners for ANY manual email send.
|
||||
Force inclusion of archived partners for manual sends.
|
||||
"""
|
||||
_logger.error("🔥🔥🔥🔥🔥 MailThread._notify_get_recipients CALLED 🔥🔥🔥🔥🔥")
|
||||
_logger.error(f"🔥 Model: {self._name}")
|
||||
_logger.error(f"🔥 Message type: {getattr(message, 'message_type', 'Unknown')}")
|
||||
_logger.error(f"🔥 Message subject: {getattr(message, 'subject', 'No subject')}")
|
||||
_logger.error(f"🔥 Full context: {dict(self.env.context)}")
|
||||
|
||||
# Check MULTIPLE indicators of manual send
|
||||
# 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") or # From invoice wizard
|
||||
# Additional checks for email sends
|
||||
(self._name == "account.move" and
|
||||
self.env.context.get("default_composition_mode") == "comment") or
|
||||
(msg_vals and msg_vals.get("message_type") == "email")
|
||||
self.env.context.get("force_email") or
|
||||
self.env.context.get("mark_invoice_as_sent")
|
||||
)
|
||||
|
||||
_logger.error(f"🔥 Is manual send? {is_manual_send}")
|
||||
_logger.error(f"🔥 All 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')}, "
|
||||
f"mark_invoice_as_sent={self.env.context.get('mark_invoice_as_sent')}")
|
||||
_logger.error(f"🔥 Message partner_ids: {getattr(message, 'partner_ids', None)}")
|
||||
|
||||
# For MANUAL sends, allow archived partners with full context
|
||||
if is_manual_send:
|
||||
_logger.error("🔥🔥🔥 ✓ MANUAL SEND DETECTED - Allowing archived partners 🔥🔥🔥")
|
||||
_logger.error(f"🔥 Calling super() with context: active_test=False, include_archived_partners=True, mail_notify_force=True")
|
||||
# For manual sends, we need to handle archived partners
|
||||
if is_manual_send and hasattr(message, 'partner_ids') and message.partner_ids:
|
||||
_logger.error("🔥🔥🔥 MANUAL SEND WITH PARTNERS - Handling archived partners 🔥🔥🔥")
|
||||
|
||||
# Use context that allows archived partners
|
||||
# Get the partner IDs from the message
|
||||
partner_ids = message.partner_ids.ids
|
||||
_logger.error(f"🔥 Message has partners: {partner_ids}")
|
||||
|
||||
# Force the lookup of THESE partners (the recipients), not the author
|
||||
# We need to ensure these partners are found even if archived
|
||||
|
||||
# Call parent with context that allows archived partners
|
||||
recipients = super(
|
||||
MailThread,
|
||||
self.with_context(
|
||||
active_test=False,
|
||||
include_archived_partners=True,
|
||||
mail_notify_force=True,
|
||||
# Add the partner IDs we want to look up
|
||||
force_notification_partner_ids=partner_ids,
|
||||
)
|
||||
)._notify_get_recipients(message, msg_vals, **kwargs)
|
||||
|
||||
_logger.error(f"🔥 Number of recipients found: {len(recipients)}")
|
||||
|
||||
# If no recipients found, create them manually
|
||||
if len(recipients) == 0 and partner_ids:
|
||||
_logger.error("🔥 No recipients found, creating manually")
|
||||
recipients = []
|
||||
for partner_id in partner_ids:
|
||||
partner = self.env['res.partner'].with_context(
|
||||
active_test=False
|
||||
).browse(partner_id)
|
||||
|
||||
if partner.exists() and partner.email:
|
||||
recipients.append({
|
||||
'id': partner.id,
|
||||
'partner_id': partner.id,
|
||||
'email': partner.email,
|
||||
'name': partner.name,
|
||||
'notif': 'email', # Force email notification
|
||||
'lang': partner.lang or 'en_US',
|
||||
'type': 'customer',
|
||||
'is_follower': False,
|
||||
'groups': [],
|
||||
'notifications': [],
|
||||
})
|
||||
_logger.error(f"🔥 Created recipient for archived partner: {partner.id} - {partner.email}")
|
||||
|
||||
for i, recipient in enumerate(recipients):
|
||||
_logger.error(f"🔥 Recipient {i}: partner_id={recipient.get('partner_id')}, "
|
||||
f"notif={recipient.get('notif')}, email={recipient.get('email')}, "
|
||||
f"groups={recipient.get('groups', [])}")
|
||||
f"notif={recipient.get('notif')}, email={recipient.get('email')}")
|
||||
|
||||
return recipients
|
||||
|
||||
# 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
|
||||
(msg_vals and msg_vals.get("message_type") == "notification") or
|
||||
getattr(message, "subtype_id", False) and
|
||||
getattr(message.subtype_id, "internal", False)
|
||||
)
|
||||
|
||||
if is_system_notification:
|
||||
_logger.error("🔥🔥🔥 ✗ SYSTEM NOTIFICATION - Blocking emails, forcing inbox only 🔥🔥🔥")
|
||||
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs)
|
||||
_logger.error(f"🔥 System notification recipients before blocking: {len(recipients)}")
|
||||
|
||||
# Force inbox only, no email
|
||||
for recipient in recipients:
|
||||
recipient["notif"] = "inbox"
|
||||
_logger.error(f"🔥 Blocked email for recipient: partner_id={recipient.get('partner_id')}")
|
||||
|
||||
return recipients
|
||||
|
||||
# Default behavior for non-manual, non-system sends
|
||||
_logger.error("🔥🔥🔥 ○ DEFAULT BEHAVIOR - No special handling 🔥🔥🔥")
|
||||
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs)
|
||||
_logger.error(f"🔥 Default recipients found: {len(recipients)}")
|
||||
for i, recipient in enumerate(recipients):
|
||||
_logger.error(f"🔥 Default recipient {i}: partner_id={recipient.get('partner_id')}, "
|
||||
f"notif={recipient.get('notif')}")
|
||||
return recipients
|
||||
|
||||
|
||||
def _message_post(self, **kwargs):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user