From a26496b6e79153212973c55f20431df82c3d827b Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Thu, 14 Mar 2019 12:32:31 +0000 Subject: [PATCH] [FIX] mail: set message_main_attachment_id when the attachment is added via widget Create a record. Add an attachment, using the widget (aptly named 'add an attachment'). It is not set as message_main_attachment_id. If you add the message through 'log note', then it is. We add a hook to make sure that it is set as message_main_attachment_id when added through the widget. opw 1950403 closes odoo/odoo#31847 Signed-off-by: Nans Lefebvre (len) --- addons/mail/models/ir_attachment.py | 8 ++++++++ addons/web/controllers/main.py | 1 + odoo/addons/base/models/ir_attachment.py | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/addons/mail/models/ir_attachment.py b/addons/mail/models/ir_attachment.py index 99263c683c5..acc14b2b6f9 100644 --- a/addons/mail/models/ir_attachment.py +++ b/addons/mail/models/ir_attachment.py @@ -7,6 +7,14 @@ from odoo import api, fields, models class IrAttachment(models.Model): _inherit = 'ir.attachment' + @api.multi + def _post_add_create(self): + """ Overrides behaviour when the attachment is created through the controller + """ + super(IrAttachment, self)._post_add_create() + for record in self: + record.register_as_main_attachment() + def register_as_main_attachment(self): """ Registers this attachment as the main one of the model it is attached to. diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 74c936ed73b..3e531f26da4 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1167,6 +1167,7 @@ class Binary(http.Controller): 'res_model': model, 'res_id': int(id) }) + attachment._post_add_create() except Exception: args.append({'error': _("Something horrible happened")}) _logger.exception("Fail to upload attachment %s" % ufile.filename) diff --git a/odoo/addons/base/models/ir_attachment.py b/odoo/addons/base/models/ir_attachment.py index 535605d9fe7..56b5a1d9f13 100644 --- a/odoo/addons/base/models/ir_attachment.py +++ b/odoo/addons/base/models/ir_attachment.py @@ -506,6 +506,10 @@ class IrAttachment(models.Model): self.browse().check('write', values=values) return super(IrAttachment, self).create(vals_list) + @api.multi + def _post_add_create(self): + pass + @api.one def generate_access_token(self): if self.access_token: