From abc45b19d02dffebfeb867feeb3051c6c751d0a0 Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Thu, 4 Apr 2019 08:14:04 +0000 Subject: [PATCH] [FIX] mail: remove attachment as main at unlink If an user unlinks an attachment, it doesn't remove it as message_main_attachment_id. opw 1950403 closes odoo/odoo#32347 Signed-off-by: Nans Lefebvre (len) --- addons/mail/models/ir_attachment.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/addons/mail/models/ir_attachment.py b/addons/mail/models/ir_attachment.py index ee51ed8382e..527b22409d6 100644 --- a/addons/mail/models/ir_attachment.py +++ b/addons/mail/models/ir_attachment.py @@ -15,6 +15,19 @@ class IrAttachment(models.Model): for record in self: record.register_as_main_attachment(force=False) + @api.multi + def unlink(self): + self.remove_as_main_attachment() + super(IrAttachment, self).unlink() + + @api.multi + def remove_as_main_attachment(self): + for attachment in self: + related_record = self.env[attachment.res_model].browse(attachment.res_id) + if related_record and hasattr(related_record, 'message_main_attachment_id'): + if related_record.message_main_attachment_id == attachment: + related_record.message_main_attachment_id = False + def register_as_main_attachment(self, force=True): """ Registers this attachment as the main one of the model it is attached to.