From 6424025b65abceb478e2dbc6c89cfded974ef59a Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Thu, 17 Sep 2020 13:47:04 +0000 Subject: [PATCH] [FIX] purchase: don't send preview email when PO not saved When PO not saved, no mail can be send. Sending a reminder preview will get traceback. In this commit, we check if the PO is saved before sending the mail. Also clean some useless code in the send_reminder_preview() and fix the issue when no json object is return to the widget because of on mail send. Task 2339431 PR #57961 closes odoo/odoo#59399 X-original-commit: 83f2cac4b8ba101d6a7736a8ff408a3fdb5422fe Signed-off-by: William Henrotin --- addons/purchase/models/purchase.py | 18 +++++++++--------- .../static/src/js/purchase_toaster_button.js | 4 +++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 0c3047cf9fa..0d21dafcd33 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -663,18 +663,18 @@ class PurchaseOrder(models.Model): order.with_context(is_reminder=True).message_post_with_template(template.id, email_layout_xmlid="mail.mail_notification_paynow", composition_mode='comment') def send_reminder_preview(self): - if not self.user_has_groups('purchase.group_send_reminder') and not self.receipt_reminder_email: + self.ensure_one() + if not self.user_has_groups('purchase.group_send_reminder'): return template = self.env.ref('purchase.email_template_edi_purchase_reminder', raise_if_not_found=False) - if template and self.env.user.email: - for order in self: - template.with_context(is_reminder=True).send_mail( - order.id, - force_send=True, - raise_exception=False, - email_values={'email_to': self.env.user.email, 'recipient_ids': []}, - notif_layout="mail.mail_notification_paynow") + if template and self.env.user.email and self.id: + template.with_context(is_reminder=True).send_mail( + self.id, + force_send=True, + raise_exception=False, + email_values={'email_to': self.env.user.email, 'recipient_ids': []}, + notif_layout="mail.mail_notification_paynow") return {'toast_message': _("A sample email has been sent to %s.") % self.env.user.email} @api.model diff --git a/addons/purchase/static/src/js/purchase_toaster_button.js b/addons/purchase/static/src/js/purchase_toaster_button.js index 15820983c44..542858188dc 100644 --- a/addons/purchase/static/src/js/purchase_toaster_button.js +++ b/addons/purchase/static/src/js/purchase_toaster_button.js @@ -29,7 +29,9 @@ odoo.define('purchase.ToasterButton', function (require) { model: this.model, args: [[this.id]], }).then(res => { - this.do_notify(false, res.toast_message); + if (res) { + this.do_notify(false, res.toast_message); + } }) }, });