Fix allow_mail_archived_partner causing system notifications to send emails

This commit is contained in:
Kristof Bernaert
2026-01-26 04:27:39 +01:00
parent ba330eadf7
commit 7325633d49
4 changed files with 16 additions and 73 deletions
@@ -1,6 +1,3 @@
import logging
_logger = logging.getLogger(__name__)
from odoo import models, api, fields from odoo import models, api, fields
@@ -18,8 +15,9 @@ class AccountInvoiceSend(models.TransientModel):
@api.model @api.model
def default_get(self, fields): def default_get(self, fields):
_logger.error("🔥 ACCOUNT.INVOICE.SEND default_get") """
Pre-fill invoice email wizard with archived partners.
"""
res = super().default_get(fields) res = super().default_get(fields)
active_ids = self.env.context.get('active_ids', []) active_ids = self.env.context.get('active_ids', [])
@@ -31,16 +29,13 @@ class AccountInvoiceSend(models.TransientModel):
partners = moves.mapped('partner_id').filtered(lambda p: p.email) partners = moves.mapped('partner_id').filtered(lambda p: p.email)
if partners: if partners:
res['partner_ids'] = [(6, 0, partners.ids)] res['partner_ids'] = [(6, 0, partners.ids)]
_logger.error(f"🔥 Set partner_ids: {res['partner_ids']}")
return res return res
def action_send_and_print(self): def action_send_and_print(self):
_logger.error("🔥🔥🔥 ACCOUNT.INVOICE.SEND action_send_and_print CALLED 🔥🔥🔥") """
_logger.error(f"🔥 Wizard ID: {self.id}") Pass context to allow archived partners during email sending.
_logger.error(f"🔥 Partner IDs: {self.partner_ids.ids}") # This returns a list of IDs """
# Call parent with context AND ensure partner_ids are passed
return super( return super(
AccountInvoiceSend, AccountInvoiceSend,
self.with_context( self.with_context(
@@ -49,15 +44,13 @@ class AccountInvoiceSend(models.TransientModel):
mail_notify_force=True, mail_notify_force=True,
force_email=True, force_email=True,
mark_invoice_as_sent=True, mark_invoice_as_sent=True,
# Pass simple list of IDs, NOT ORM tuple format
invoice_partner_ids=self.partner_ids.ids if self.partner_ids else [],
) )
).action_send_and_print() ).action_send_and_print()
def _get_composer_values(self, res_ids, template): def _get_composer_values(self, res_ids, template):
_logger.error("🔥 ACCOUNT.INVOICE.SEND _get_composer_values") """
Pass context to mail composer to allow archived partners.
# Also pass context to composer """
return super( return super(
AccountInvoiceSend, AccountInvoiceSend,
self.with_context( self.with_context(
@@ -1,6 +1,3 @@
import logging
_logger = logging.getLogger(__name__)
from odoo import models, api from odoo import models, api
@@ -13,8 +10,6 @@ class MailComposeMessage(models.TransientModel):
Pre-fill email wizard with archived partners for sales orders. Pre-fill email wizard with archived partners for sales orders.
For invoices, this is handled by account.invoice.send. For invoices, this is handled by account.invoice.send.
""" """
_logger.debug("=== MAIL.COMPOSE.MESSAGE DEFAULT_GET ===")
res = super().default_get(fields) res = super().default_get(fields)
model = self.env.context.get("active_model") model = self.env.context.get("active_model")
@@ -22,8 +17,6 @@ class MailComposeMessage(models.TransientModel):
# Only handle sales orders here - invoices use account.invoice.send # Only handle sales orders here - invoices use account.invoice.send
if model == "sale.order" and res_id: if model == "sale.order" and res_id:
_logger.debug(f"Processing sales order {res_id}")
# Find order with archived partners allowed # Find order with archived partners allowed
order = self.env["sale.order"].with_context( order = self.env["sale.order"].with_context(
active_test=False active_test=False
@@ -31,7 +24,6 @@ class MailComposeMessage(models.TransientModel):
if order.exists() and order.partner_id: if order.exists() and order.partner_id:
res["partner_ids"] = [(6, 0, [order.partner_id.id])] res["partner_ids"] = [(6, 0, [order.partner_id.id])]
_logger.debug(f"Pre-filled archived partner {order.partner_id.id} for sales order")
return res return res
@@ -40,14 +32,11 @@ class MailComposeMessage(models.TransientModel):
Set context flags for manual email sends. Set context flags for manual email sends.
This ensures archived partners are allowed during email sending. This ensures archived partners are allowed during email sending.
""" """
_logger.debug("=== _prepare_mail_values ===")
model = self.env.context.get("active_model") or self.model model = self.env.context.get("active_model") or self.model
# Only for sales orders and account moves (invoices) # Only for sales orders and account moves (invoices)
# Note: account.invoice.send should handle invoices, but keep this as fallback # Note: account.invoice.send should handle invoices, but keep this as fallback
if model in ["sale.order", "account.move"]: if model in ["sale.order", "account.move"]:
_logger.debug(f"Setting context for {model} to allow archived partners")
return super( return super(
MailComposeMessage, MailComposeMessage,
self.with_context( self.with_context(
@@ -74,7 +63,6 @@ class MailComposeMessage(models.TransientModel):
# If it's a manual send and partner is archived, include them # If it's a manual send and partner is archived, include them
if is_manual_send and partner and not partner.active: if is_manual_send and partner and not partner.active:
_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,
@@ -83,21 +71,4 @@ class MailComposeMessage(models.TransientModel):
} }
# Default behavior for active partners or non-manual sends # Default behavior for active partners or non-manual sends
return super()._prepare_recipient_values(partner) return super()._prepare_recipient_values(partner)
def action_send_mail(self):
"""
Log when email is actually sent.
"""
_logger.info("🔥 MAIL.COMPOSE.MESSAGE action_send_mail")
_logger.info(f"Partner IDs: {self.partner_ids.ids}")
_logger.info(f"Context: {dict(self.env.context)}")
return super().action_send_mail()
def _action_send_mail(self, auto_commit=False):
"""
Another possible send method.
"""
_logger.info("🔥 MAIL.COMPOSE.MESSAGE _action_send_mail")
return super()._action_send_mail(auto_commit=auto_commit)
@@ -1,6 +1,3 @@
import logging
_logger = logging.getLogger(__name__)
from odoo import models from odoo import models
@@ -10,8 +7,7 @@ class MailTemplate(models.Model):
def generate_email(self, res_ids, fields=None): def generate_email(self, res_ids, fields=None):
""" """
Only allow archived partners when *explicitly* sending through a flow Only allow archived partners when *explicitly* sending through a flow
that sets: that sets context['mail_notify_force'] = True.
context['mail_notify_force'] = True
This prevents system notifications from inheriting your relaxed rules. This prevents system notifications from inheriting your relaxed rules.
""" """
@@ -24,4 +20,4 @@ class MailTemplate(models.Model):
) )
).generate_email(res_ids, fields) ).generate_email(res_ids, fields)
return super().generate_email(res_ids, fields) return super().generate_email(res_ids, fields)
@@ -1,6 +1,3 @@
import logging
_logger = logging.getLogger(__name__)
from odoo import models, api from odoo import models, api
@@ -10,13 +7,9 @@ class ResPartner(models.Model):
@api.model @api.model
def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
""" """
SIMPLE: Just remove active filters when context says to include archived. Remove active filters when context indicates manual email send
(to allow finding archived partners).
""" """
_logger.error("🔥 RES.PARTNER._search - Checking for archived context")
_logger.error(f"🔥 Context force_email: {self.env.context.get('force_email')}")
_logger.error(f"🔥 Context mark_invoice_as_sent: {self.env.context.get('mark_invoice_as_sent')}")
_logger.error(f"🔥 Context mail_notify_force: {self.env.context.get('mail_notify_force')}")
# Check for manual email context # Check for manual email context
include_archived = ( include_archived = (
self.env.context.get("force_email") or self.env.context.get("force_email") or
@@ -24,11 +17,8 @@ class ResPartner(models.Model):
self.env.context.get("mail_notify_force") self.env.context.get("mail_notify_force")
) )
_logger.error(f"🔥 Should include archived? {include_archived}")
if include_archived: if include_archived:
_logger.error("🔥 Removing active filters for archived partners") # Remove active filters to include archived partners
# Simple: remove active filters
args = [ args = [
arg for arg in args arg for arg in args
if not (isinstance(arg, (list, tuple)) and if not (isinstance(arg, (list, tuple)) and
@@ -36,12 +26,5 @@ class ResPartner(models.Model):
arg[0] == "active") arg[0] == "active")
] ]
self = self.with_context(active_test=False) self = self.with_context(active_test=False)
_logger.error(f"🔥 Search args after: {args}")
result = super()._search(args, offset, limit, order, count, access_rights_uid) return super()._search(args, offset, limit, order, count, access_rights_uid)
if not count:
result_ids = list(result)
_logger.error(f"🔥 Search returned {len(result_ids)} results")
return result