Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -7,92 +7,63 @@ from odoo import models, api, fields
|
||||
class AccountInvoiceSend(models.TransientModel):
|
||||
_inherit = "account.invoice.send"
|
||||
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND CLASS LOADED ===")
|
||||
|
||||
# OVERRIDE THE FIELD DEFINITION to remove domain filter
|
||||
partner_ids = fields.Many2many(
|
||||
'res.partner',
|
||||
string='Recipients',
|
||||
help='Contacts of the invoice that will receive the email.',
|
||||
# REMOVE any domain that filters by active=True
|
||||
context={'active_test': False}, # Allow archived in searches
|
||||
context={'active_test': False},
|
||||
check_company=True,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND CREATE ===")
|
||||
_logger.info(f"Create vals: {vals}")
|
||||
return super().create(vals)
|
||||
|
||||
def write(self, vals):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND WRITE ===")
|
||||
_logger.info(f"Write vals: {vals}")
|
||||
return super().write(vals)
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
"""
|
||||
Pre-fill invoice email wizard with archived partners.
|
||||
"""
|
||||
_logger.debug("=== ACCOUNT.INVOICE.SEND DEFAULT_GET ===")
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND DEFAULT_GET ===")
|
||||
_logger.info(f"Fields: {fields}")
|
||||
|
||||
res = super().default_get(fields)
|
||||
_logger.debug(f"Super result partner_ids: {res.get('partner_ids')}")
|
||||
|
||||
active_ids = self.env.context.get('active_ids', [])
|
||||
_logger.debug(f"Active IDs: {active_ids}")
|
||||
|
||||
if active_ids:
|
||||
# Find invoices with archived partners allowed
|
||||
moves = self.env['account.move'].with_context(
|
||||
active_test=False
|
||||
).browse(active_ids)
|
||||
|
||||
partner_ids = []
|
||||
for move in moves:
|
||||
if move.partner_id:
|
||||
partner_ids.append(move.partner_id.id)
|
||||
_logger.debug(f"Found partner {move.partner_id.id} for invoice {move.id}")
|
||||
|
||||
if partner_ids:
|
||||
res['partner_ids'] = [(6, 0, partner_ids)]
|
||||
_logger.debug(f"SET partner_ids: {res['partner_ids']}")
|
||||
_logger.info(f"Result: {res}")
|
||||
|
||||
return res
|
||||
|
||||
def _get_composer_values(self, res_ids, template):
|
||||
"""
|
||||
Pass context to mail composer 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_partners=True, # FIXED: plural 's'
|
||||
mail_notify_force=True,
|
||||
)
|
||||
)._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}")
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND ACTION_SEND_AND_PRINT ===")
|
||||
_logger.info(f"Self ID: {self.id}")
|
||||
_logger.info(f"Partner IDs: {self.partner_ids.ids}")
|
||||
|
||||
# Ensure context is passed when calling action
|
||||
# Call parent with ALL context flags
|
||||
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
|
||||
force_email=True,
|
||||
mark_invoice_as_sent=True,
|
||||
)
|
||||
).action_send_and_print()
|
||||
|
||||
_logger.debug("Email send action completed")
|
||||
_logger.info(f"Action result: {result}")
|
||||
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 ===")
|
||||
# Also try overriding _process_send_and_print which might be the actual method
|
||||
def _process_send_and_print(self, invoice, template):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND _PROCESS_SEND_AND_PRINT ===")
|
||||
_logger.info(f"Invoice: {invoice.id}, Template: {template.id}")
|
||||
|
||||
return super(
|
||||
AccountInvoiceSend,
|
||||
self.with_context(
|
||||
@@ -102,4 +73,4 @@ class AccountInvoiceSend(models.TransientModel):
|
||||
force_email=True,
|
||||
mark_invoice_as_sent=True,
|
||||
)
|
||||
).action_send()
|
||||
)._process_send_and_print(invoice, template)
|
||||
@@ -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