Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
# account_move_send.py
|
||||||
from odoo import models, api
|
from odoo import models, api
|
||||||
|
|
||||||
|
|
||||||
@@ -7,55 +8,24 @@ class AccountInvoiceSend(models.TransientModel):
|
|||||||
@api.model
|
@api.model
|
||||||
def default_get(self, fields):
|
def default_get(self, fields):
|
||||||
"""
|
"""
|
||||||
Override default_get to pre-fill archived partners.
|
Pre-fill invoice email wizard with archived partner.
|
||||||
This is often called BEFORE _get_default_mail_partner_ids.
|
|
||||||
"""
|
"""
|
||||||
res = super().default_get(fields)
|
res = super().default_get(fields)
|
||||||
|
|
||||||
# Check if we're in the right context
|
|
||||||
active_model = self.env.context.get('active_model')
|
|
||||||
active_ids = self.env.context.get('active_ids', [])
|
active_ids = self.env.context.get('active_ids', [])
|
||||||
|
|
||||||
if active_model == 'account.move' and active_ids:
|
if active_ids:
|
||||||
# Get the moves with archived partners allowed
|
# Find invoices WITH archived partners
|
||||||
moves = self.env['account.move'].with_context(
|
invoices = self.env['account.move'].with_context(
|
||||||
active_test=False
|
active_test=False
|
||||||
).browse(active_ids)
|
).browse(active_ids)
|
||||||
|
|
||||||
# Collect all unique partners from the moves
|
partner_ids = []
|
||||||
partner_ids = set()
|
for inv in invoices:
|
||||||
for move in moves:
|
if inv.partner_id:
|
||||||
if move.partner_id and move.partner_id.email:
|
partner_ids.append(inv.partner_id.id)
|
||||||
partner_ids.add(move.partner_id.id)
|
|
||||||
|
|
||||||
if partner_ids:
|
if partner_ids:
|
||||||
res['partner_ids'] = [(6, 0, list(partner_ids))]
|
res['partner_ids'] = [(6, 0, partner_ids)]
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@api.model
|
|
||||||
def _get_default_mail_partner_ids(self, move, mail_template, mail_lang):
|
|
||||||
"""
|
|
||||||
Also fix this method to include archived partners.
|
|
||||||
"""
|
|
||||||
wiz = self.with_context(
|
|
||||||
active_test=False,
|
|
||||||
include_archived_partners=True,
|
|
||||||
)
|
|
||||||
return super(
|
|
||||||
AccountInvoiceSend,
|
|
||||||
wiz
|
|
||||||
)._get_default_mail_partner_ids(move, mail_template, mail_lang)
|
|
||||||
|
|
||||||
def _get_mail_composer_values(self, move, template, partner_ids):
|
|
||||||
"""
|
|
||||||
Ensure context is passed to mail composer.
|
|
||||||
"""
|
|
||||||
return super(
|
|
||||||
AccountInvoiceSend,
|
|
||||||
self.with_context(
|
|
||||||
active_test=False,
|
|
||||||
include_archived_partners=True,
|
|
||||||
mail_notify_force=True,
|
|
||||||
)
|
|
||||||
)._get_mail_composer_values(move, template, partner_ids)
|
|
||||||
@@ -1,24 +1,77 @@
|
|||||||
|
import logging
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
from odoo import models, api
|
from odoo import models, api
|
||||||
|
|
||||||
|
|
||||||
class MailComposeMessage(models.TransientModel):
|
class MailComposeMessage(models.TransientModel):
|
||||||
_inherit = "mail.compose.message"
|
_inherit = "mail.compose.message"
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def default_get(self, fields):
|
||||||
|
"""
|
||||||
|
Pre-fill the email wizard with archived partner's email.
|
||||||
|
This makes the archived partner appear in the "To" field.
|
||||||
|
"""
|
||||||
|
res = super().default_get(fields)
|
||||||
|
|
||||||
|
model = self.env.context.get("active_model")
|
||||||
|
res_id = self.env.context.get("active_id")
|
||||||
|
|
||||||
|
# For invoices
|
||||||
|
if model == "account.move" and res_id:
|
||||||
|
invoice = self.env["account.move"].with_context(
|
||||||
|
active_test=False # CRITICAL: Find archived partner
|
||||||
|
).browse(res_id)
|
||||||
|
|
||||||
|
if invoice.exists() and invoice.partner_id:
|
||||||
|
res["partner_ids"] = [(6, 0, [invoice.partner_id.id])]
|
||||||
|
_logger.debug(f"Pre-filled archived partner {invoice.partner_id.name} for invoice")
|
||||||
|
|
||||||
|
# For sales orders
|
||||||
|
elif model == "sale.order" and res_id:
|
||||||
|
order = self.env["sale.order"].with_context(
|
||||||
|
active_test=False # CRITICAL: Find archived partner
|
||||||
|
).browse(res_id)
|
||||||
|
|
||||||
|
if order.exists() and order.partner_id:
|
||||||
|
res["partner_ids"] = [(6, 0, [order.partner_id.id])]
|
||||||
|
_logger.debug(f"Pre-filled archived partner {order.partner_id.name} for sales order")
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
def _prepare_mail_values(self, res_ids):
|
||||||
|
"""
|
||||||
|
Set context to allow archived partners during email sending.
|
||||||
|
This ensures the email can actually be sent to archived partners.
|
||||||
|
"""
|
||||||
|
model = self.env.context.get("active_model") or self.model
|
||||||
|
|
||||||
|
if model in ["sale.order", "account.move"]:
|
||||||
|
_logger.debug(f"Setting context for {model} to allow archived partners")
|
||||||
|
return super(
|
||||||
|
MailComposeMessage,
|
||||||
|
self.with_context(
|
||||||
|
active_test=False,
|
||||||
|
include_archived_partners=True,
|
||||||
|
mail_notify_force=True,
|
||||||
|
)
|
||||||
|
)._prepare_mail_values(res_ids)
|
||||||
|
|
||||||
|
return super()._prepare_mail_values(res_ids)
|
||||||
|
|
||||||
def _prepare_recipient_values(self, partner):
|
def _prepare_recipient_values(self, partner):
|
||||||
"""
|
"""
|
||||||
Handle archived partners when context allows.
|
Handle archived partners when context allows.
|
||||||
|
This ensures archived partner data is properly formatted for email.
|
||||||
"""
|
"""
|
||||||
# Check if we should include archived partners
|
|
||||||
# Look for context flags from account.invoice.send OR direct context
|
|
||||||
include_archived = (
|
include_archived = (
|
||||||
self.env.context.get("include_archived_partners") or
|
self.env.context.get("include_archived_partners") or
|
||||||
self.env.context.get("mail_notify_force") or
|
self.env.context.get("mail_notify_force")
|
||||||
(self.env.context.get("active_model") in ["account.move", "sale.order"] and
|
|
||||||
not getattr(partner, 'active', True))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if include_archived and partner and not partner.active:
|
if include_archived and partner and not partner.active:
|
||||||
# Return complete data for archived partner
|
_logger.debug(f"Including archived partner {partner.name} in email")
|
||||||
return {
|
return {
|
||||||
"partner_id": partner.id,
|
"partner_id": partner.id,
|
||||||
"email": partner.email,
|
"email": partner.email,
|
||||||
|
|||||||
@@ -8,43 +8,40 @@ class MailThread(models.AbstractModel):
|
|||||||
_inherit = "mail.thread"
|
_inherit = "mail.thread"
|
||||||
|
|
||||||
def _notify_thread(self, message, msg_vals=False, **kwargs):
|
def _notify_thread(self, message, msg_vals=False, **kwargs):
|
||||||
"""
|
|
||||||
Do NOT force notifications, do NOT set mail_notification=True,
|
|
||||||
do NOT globally disable active_test here.
|
|
||||||
|
|
||||||
The parent method decides whether a message should notify.
|
|
||||||
"""
|
|
||||||
return super()._notify_thread(message, msg_vals=msg_vals, **kwargs)
|
return super()._notify_thread(message, msg_vals=msg_vals, **kwargs)
|
||||||
|
|
||||||
def _notify_get_recipients(self, message, msg_vals, **kwargs):
|
def _notify_get_recipients(self, message, msg_vals, **kwargs):
|
||||||
"""
|
"""
|
||||||
CRITICAL RULES:
|
FIXED: Allow archived partners ONLY for explicit manual sends
|
||||||
- Never turn system notifications into outgoing emails.
|
|
||||||
- Never fabricate recipients when Odoo returns none.
|
|
||||||
- Never force notif='email'.
|
|
||||||
|
|
||||||
If archived partners should be allowed, that must be opt-in
|
|
||||||
via context['include_archived_partners'] and only for explicit user sends,
|
|
||||||
not tracking/system messages.
|
|
||||||
"""
|
"""
|
||||||
# 1) Never email system notifications (tracking, auto messages, etc.)
|
# 1) BLOCK system notifications completely
|
||||||
# These are the ones that caused "Re: Draft Bill ..." and odoobot@example.com.
|
is_system_notification = (
|
||||||
if getattr(message, "message_type", None) == "notification":
|
getattr(message, "message_type", None) == "notification" or
|
||||||
return super()._notify_get_recipients(message, msg_vals, **kwargs)
|
getattr(message, "author_id", False) and
|
||||||
|
message.author_id == self.env.ref("base.partner_root", raise_if_not_found=False)
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_system_notification:
|
||||||
|
_logger.debug("Blocking system notification email")
|
||||||
|
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs)
|
||||||
|
# Force inbox only, no email
|
||||||
|
for recipient in recipients:
|
||||||
|
recipient["notif"] = "inbox"
|
||||||
|
return recipients
|
||||||
|
|
||||||
# 2) Never auto-notify supplier bills. Vendor bills created from email/EDI
|
# 2) Check if this is an EXPLICIT manual send
|
||||||
# must not trigger replies to vendors via chatter notifications.
|
is_explicit_send = (
|
||||||
# (If you DO want it for customer invoices, leave those alone.)
|
self.env.context.get("mail_notify_force") or
|
||||||
if self._name == "account.move":
|
self.env.context.get("include_archived_partners")
|
||||||
# self can be multi; if any are supplier bills, just don't customize at all
|
)
|
||||||
# (safer than partial overriding recipient lists)
|
|
||||||
if any(m.move_type == "in_invoice" for m in self):
|
# 3) For explicit sends, allow archived partners
|
||||||
return super()._notify_get_recipients(message, msg_vals, **kwargs)
|
if is_explicit_send:
|
||||||
|
_logger.debug(f"Explicit send detected for {self._name}, allowing archived partners")
|
||||||
# 3) Default behavior first
|
return super(
|
||||||
recipients = super()._notify_get_recipients(message, msg_vals, **kwargs)
|
MailThread,
|
||||||
|
self.with_context(active_test=False)
|
||||||
# 4) Do NOT fabricate recipients if empty.
|
)._notify_get_recipients(message, msg_vals, **kwargs)
|
||||||
# If you need archived partner support, handle it in explicit sending flows
|
|
||||||
# (compose + template), not here.
|
# 4) Default behavior for non-explicit sends
|
||||||
return recipients
|
return super()._notify_get_recipients(message, msg_vals, **kwargs)
|
||||||
Reference in New Issue
Block a user