From e58471b148f74e29d7f4e8113f26f265fa531fb4 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 31 Dec 2021 01:36:41 +0000 Subject: [PATCH] [FIX] mail, mass_mailing: fix attachment ownership Attachments that are uploaded on a record that isn't saved yet are created with res_id set to 0. In the case of attachments linked through a m2m rather than the usual (res_model, res_id), it means they may not be readable afer the creation of the record, except by their creator. Re-attaching them to the mailing / template at the end of the create() call fixes the ownership. Fixes #81935 closes odoo/odoo#82120 X-original-commit: 05fb705f2729ffdf07ab36accda20415a44e1aa6 Signed-off-by: Olivier Dony --- addons/mail/models/mail_template.py | 9 +++++++++ addons/mass_mailing/models/mailing.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index 710dbcb8e04..b516e6d2ca1 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -79,6 +79,15 @@ class MailTemplate(models.Model): # CRUD # ------------------------------------------------------------ + @api.model_create_multi + def create(self, vals_list): + templates = super().create(vals_list) + # fix attachment ownership + for template in templates: + if template.attachment_ids: + template.attachment_ids.write({'res_model': self._name, 'res_id': template.id}) + return templates + def unlink(self): self.unlink_action() return super(MailTemplate, self).unlink() diff --git a/addons/mass_mailing/models/mailing.py b/addons/mass_mailing/models/mailing.py index 40414588a1f..15f981c006c 100644 --- a/addons/mass_mailing/models/mailing.py +++ b/addons/mass_mailing/models/mailing.py @@ -417,6 +417,12 @@ class MassMailing(models.Model): if mailing.ab_testing_enabled and not mailing.campaign_id ] self.env['utm.campaign'].create(campaign_vals) + + # fix attachment ownership + for mailing in mailings: + if mailing.attachment_ids: + mailing.attachment_ids.write({'res_model': self._name, 'res_id': mailing.id}) + return mailings def write(self, values):