[MOV] mail, loyalty, purchase, (website_)sale: move model-specific composer code to model
PURPOSE
Purpose of this task is to cleanup attachment management done in generic mail
models overrides and move it in account as model overrides.
SPECIFICATIONS
Update addons
* in purchase and sale: context key update should be done at message_post
level, especially this is not used in mass mailing mode;
* in website_sale: code to update sale order to avoid sending recovery
emails several times belongs to a "post hook" method on sale order model;
* in loyalty: remove a deprecated / unsupported 'mark_coupon_as_sent' key;
Task-2792146 (Mail: Move model-dependent code from composer / template)
Part-of: odoo/odoo#106658
This commit is contained in:
@@ -86,7 +86,6 @@ class LoyaltyCard(models.Model):
|
||||
default_template_id=default_template and default_template.id,
|
||||
default_composition_mode='comment',
|
||||
default_email_layout_xmlid='mail.mail_notification_light',
|
||||
mark_coupon_as_sent=True,
|
||||
force_email=True,
|
||||
)
|
||||
return {
|
||||
|
||||
@@ -1905,6 +1905,13 @@ class MailThread(models.AbstractModel):
|
||||
""" Hook to add custom behavior after having posted the message. Both
|
||||
message and computed value are given, to try to lessen query count by
|
||||
using already-computed values instead of having to rebrowse things. """
|
||||
return
|
||||
|
||||
def _message_mail_after_hook(self, mails):
|
||||
""" Hook to add custom behavior after having sent an mass mailing.
|
||||
|
||||
:param mail.mail mails: mail.mail records about to be sent"""
|
||||
return
|
||||
|
||||
def _process_attachments_for_post(self, attachments, attachment_ids, message_values):
|
||||
""" Preprocess attachments for MailTread.message_post() or MailMail.create().
|
||||
|
||||
@@ -329,6 +329,7 @@ class MailComposer(models.TransientModel):
|
||||
|
||||
result_mails_su += batch_mails_sudo
|
||||
if wizard.composition_mode == 'mass_mail':
|
||||
ActiveModel.browse(res_ids)._message_mail_after_hook(batch_mails_sudo)
|
||||
batch_mails_sudo.send(auto_commit=auto_commit)
|
||||
|
||||
return result_mails_su, result_messages
|
||||
|
||||
@@ -10,6 +10,4 @@ class MailComposeMessage(models.TransientModel):
|
||||
def _action_send_mail(self, auto_commit=False):
|
||||
if self.model == 'purchase.order':
|
||||
self = self.with_context(mailing_document_based=True)
|
||||
if self.env.context.get('mark_rfq_as_sent'):
|
||||
self = self.with_context(mail_notify_author=self.env.user.partner_id in self.partner_ids)
|
||||
return super(MailComposeMessage, self)._action_send_mail(auto_commit=auto_commit)
|
||||
|
||||
@@ -333,7 +333,10 @@ class PurchaseOrder(models.Model):
|
||||
def message_post(self, **kwargs):
|
||||
if self.env.context.get('mark_rfq_as_sent'):
|
||||
self.filtered(lambda o: o.state == 'draft').write({'state': 'sent'})
|
||||
return super(PurchaseOrder, self.with_context(mail_post_autofollow=self.env.context.get('mail_post_autofollow', True))).message_post(**kwargs)
|
||||
po_ctx = {'mail_post_autofollow': self.env.context.get('mail_post_autofollow', True)}
|
||||
if self.env.context.get('mark_rfq_as_sent'):
|
||||
po_ctx['mail_notify_author'] = self.env.user.partner_id.id in kwargs.get('partner_ids') or []
|
||||
return super(PurchaseOrder, self.with_context(**po_ctx)).message_post(**kwargs)
|
||||
|
||||
def _notify_get_recipients_groups(self, msg_vals=None):
|
||||
""" Tweak 'view document' button for portal customers, calling directly
|
||||
|
||||
@@ -96,7 +96,7 @@ class ResCompany(models.Model):
|
||||
mark_so_as_sent=True,
|
||||
default_email_layout_xmlid='mail.mail_notification_layout_with_responsible_signature',
|
||||
proforma=self.env.context.get('proforma', False),
|
||||
force_email=True, mail_notify_author=True
|
||||
force_email=True,
|
||||
).create({
|
||||
'res_id': sample_sales_order.id,
|
||||
'template_id': template and template.id or False,
|
||||
|
||||
@@ -1190,7 +1190,10 @@ class SaleOrder(models.Model):
|
||||
def message_post(self, **kwargs):
|
||||
if self.env.context.get('mark_so_as_sent'):
|
||||
self.filtered(lambda o: o.state == 'draft').with_context(tracking_disable=True).write({'state': 'sent'})
|
||||
return super(SaleOrder, self.with_context(mail_post_autofollow=self.env.context.get('mail_post_autofollow', True))).message_post(**kwargs)
|
||||
so_ctx = {'mail_post_autofollow': self.env.context.get('mail_post_autofollow', True)}
|
||||
if self.env.context.get('mark_so_as_sent'):
|
||||
so_ctx['mail_notify_author'] = self.env.user.partner_id.id in kwargs.get('partner_ids') or []
|
||||
return super(SaleOrder, self.with_context(**so_ctx)).message_post(**kwargs)
|
||||
|
||||
def _notify_get_recipients_groups(self, msg_vals=None):
|
||||
""" Give access button to users and portal customer as portal is integrated
|
||||
|
||||
@@ -10,6 +10,4 @@ class MailComposeMessage(models.TransientModel):
|
||||
def _action_send_mail(self, auto_commit=False):
|
||||
if self.model == 'sale.order':
|
||||
self = self.with_context(mailing_document_based=True)
|
||||
if self.env.context.get('mark_so_as_sent'):
|
||||
self = self.with_context(mail_notify_author=self.env.user.partner_id in self.partner_ids)
|
||||
return super(MailComposeMessage, self)._action_send_mail(auto_commit=auto_commit)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from odoo import api, SUPERUSER_ID, _
|
||||
from . import controllers
|
||||
from . import models
|
||||
from . import wizard
|
||||
from . import report
|
||||
|
||||
def _post_init_hook(cr, registry):
|
||||
|
||||
@@ -381,6 +381,23 @@ class SaleOrder(models.Model):
|
||||
sent_orders |= order
|
||||
sent_orders.write({'cart_recovery_email_sent': True})
|
||||
|
||||
def _message_mail_after_hook(self, mails):
|
||||
""" After sending recovery cart emails, update orders to avoid sending
|
||||
it again. """
|
||||
if self.env.context.get('website_sale_send_recovery_email'):
|
||||
self.filtered([
|
||||
('cart_recovery_email_sent', '=', False),
|
||||
('is_abandoned_cart', '=', True)
|
||||
]).cart_recovery_email_sent = True
|
||||
return super()._message_mail_after_hook(mails)
|
||||
|
||||
def _message_post_after_hook(self, message, msg_vals):
|
||||
""" After sending recovery cart emails, update orders to avoid sending
|
||||
it again. """
|
||||
if self.env.context.get('website_sale_send_recovery_email'):
|
||||
self.cart_recovery_email_sent = True
|
||||
return super(SaleOrder, self)._message_post_after_hook(message, msg_vals)
|
||||
|
||||
def _notify_get_recipients_groups(self, msg_vals=None):
|
||||
""" In case of cart recovery email, update link to redirect directly
|
||||
to the cart (like ``mail_template_sale_cart_recovery`` template). """
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import mail_compose_message
|
||||
@@ -1,19 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class MailComposeMessage(models.TransientModel):
|
||||
_inherit = 'mail.compose.message'
|
||||
|
||||
def _action_send_mail(self, auto_commit=False):
|
||||
context = self._context
|
||||
# Task-2792146: will move to model-based method
|
||||
if context.get('website_sale_send_recovery_email') and self.model == 'sale.order' and context.get('active_ids'):
|
||||
self.env['sale.order'].search([
|
||||
('id', 'in', context.get('active_ids')),
|
||||
('cart_recovery_email_sent', '=', False),
|
||||
('is_abandoned_cart', '=', True)
|
||||
]).write({'cart_recovery_email_sent': True})
|
||||
return super(MailComposeMessage, self)._action_send_mail(auto_commit=auto_commit)
|
||||
Reference in New Issue
Block a user