From 0be5b3700863f8ab2e95a82d6669372eedc9775f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 15 Sep 2022 12:23:12 +0000 Subject: [PATCH] [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 --- addons/loyalty/models/loyalty_card.py | 1 - addons/mail/models/mail_thread.py | 7 +++++++ addons/mail/wizard/mail_compose_message.py | 1 + .../purchase/models/mail_compose_message.py | 2 -- addons/purchase/models/purchase.py | 5 ++++- addons/sale/models/res_company.py | 2 +- addons/sale/models/sale_order.py | 5 ++++- addons/sale/wizard/mail_compose_message.py | 2 -- addons/website_sale/__init__.py | 1 - addons/website_sale/models/sale_order.py | 17 +++++++++++++++++ addons/website_sale/wizard/__init__.py | 4 ---- .../wizard/mail_compose_message.py | 19 ------------------- 12 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 addons/website_sale/wizard/__init__.py delete mode 100644 addons/website_sale/wizard/mail_compose_message.py diff --git a/addons/loyalty/models/loyalty_card.py b/addons/loyalty/models/loyalty_card.py index 1c387dbe546..239165b7d52 100644 --- a/addons/loyalty/models/loyalty_card.py +++ b/addons/loyalty/models/loyalty_card.py @@ -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 { diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 4424728cb47..e2190c93afc 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -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(). diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 6ab5f0f019f..da77bf22510 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -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 diff --git a/addons/purchase/models/mail_compose_message.py b/addons/purchase/models/mail_compose_message.py index 7d98e99ce47..d5d529ed6f2 100644 --- a/addons/purchase/models/mail_compose_message.py +++ b/addons/purchase/models/mail_compose_message.py @@ -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) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 186b58a023f..d602659f4ec 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -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 diff --git a/addons/sale/models/res_company.py b/addons/sale/models/res_company.py index 239e8107d24..969184aad9e 100644 --- a/addons/sale/models/res_company.py +++ b/addons/sale/models/res_company.py @@ -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, diff --git a/addons/sale/models/sale_order.py b/addons/sale/models/sale_order.py index ed2c8d32d26..526587c712c 100644 --- a/addons/sale/models/sale_order.py +++ b/addons/sale/models/sale_order.py @@ -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 diff --git a/addons/sale/wizard/mail_compose_message.py b/addons/sale/wizard/mail_compose_message.py index 6af644c0f8a..6e989df230c 100644 --- a/addons/sale/wizard/mail_compose_message.py +++ b/addons/sale/wizard/mail_compose_message.py @@ -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) diff --git a/addons/website_sale/__init__.py b/addons/website_sale/__init__.py index c33baebbd0a..428e641bf1d 100644 --- a/addons/website_sale/__init__.py +++ b/addons/website_sale/__init__.py @@ -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): diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index b56df2d431d..608bd3a7ae4 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -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). """ diff --git a/addons/website_sale/wizard/__init__.py b/addons/website_sale/wizard/__init__.py deleted file mode 100644 index 2cd60f0085a..00000000000 --- a/addons/website_sale/wizard/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# -*- coding: utf-8 -*- -# Part of Odoo. See LICENSE file for full copyright and licensing details. - -from . import mail_compose_message diff --git a/addons/website_sale/wizard/mail_compose_message.py b/addons/website_sale/wizard/mail_compose_message.py deleted file mode 100644 index 313722191d8..00000000000 --- a/addons/website_sale/wizard/mail_compose_message.py +++ /dev/null @@ -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)