Skip to content
Permalink
Browse files

[FIX] mail: set message_main_attachment_id when the attachment is add…

…ed 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 #31847

Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
  • Loading branch information...
len-odoo committed Mar 14, 2019
1 parent c68a97a commit a26496b6e79153212973c55f20431df82c3d827b
@@ -7,6 +7,14 @@
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.
@@ -1167,6 +1167,7 @@ def upload_attachment(self, callback, model, id, ufile):
'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)
@@ -506,6 +506,10 @@ def create(self, vals_list):
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:

9 comments on commit a26496b

@feelwhy

This comment has been minimized.

Copy link

feelwhy replied Mar 18, 2019

Hi, this commit leads to the error in the functioning of the widget 'many2many_binary' for any model which inherits mail.thread.
The problem is that the method 'register_as_main_attachment' browses over res_id exists, while in this widget it is always zero.

2019-03-18 14:39:01,007 20930 ERROR 12c_onedrive odoo.addons.web.controllers.main: Fail to upload attachment helps
Traceback (most recent call last):
File "/home/feel/Odoo/Odoo12/odoo/odoo/models.py", line 4688, in ensure_one
_id, = self._ids
ValueError: not enough values to unpack (expected 1, got 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/feel/Odoo/Odoo12/odoo/addons/web/controllers/main.py", line 1170, in upload_attachment
attachment._post_add_create()
File "/home/feel/Odoo/Odoo12/odoo/addons/mail/models/ir_attachment.py", line 16, in _post_add_create
record.register_as_main_attachment()
File "/home/feel/Odoo/Odoo12/odoo/addons/mail/models/ir_attachment.py", line 27, in register_as_main_attachment
related_record.message_main_attachment_id = self
File "/home/feel/Odoo/Odoo12/odoo/odoo/fields.py", line 995, in set
record.ensure_one()
File "/home/feel/Odoo/Odoo12/odoo/odoo/models.py", line 4691, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: knowsystem.article()

@feelwhy

This comment has been minimized.

Copy link

feelwhy replied Mar 18, 2019

As a solution:
super(IrAttachment, self)._post_add_create()
for record in self:
if record.res_id:
record.register_as_main_attachment()

@len-odoo

This comment has been minimized.

Copy link
Contributor Author

len-odoo replied Mar 18, 2019

Sorry about that, I'm going to push if related_record and hasattr(related_record, which is morally equivalent.

@feelwhy

This comment has been minimized.

Copy link

feelwhy replied Mar 18, 2019

Great! Thank you.

@len-odoo

This comment has been minimized.

Copy link
Contributor Author

len-odoo replied Mar 18, 2019

Well thank you for reporting the bug.
Do you need it to be pushed on our servers, or do you just have the problem on your own servers?

@feelwhy

This comment has been minimized.

Copy link

feelwhy replied Mar 18, 2019

We have a few clients which modules rely upon the widget. Some of those just pulled 12.0 to their servers, some are using the Odoo.sh services.

@len-odoo

This comment has been minimized.

Copy link
Contributor Author

len-odoo replied Mar 18, 2019

@len-odoo

This comment has been minimized.

Copy link
Contributor Author

len-odoo replied Mar 18, 2019

Ok then I make sure the fix is deployed ASAP on both.

@feelwhy

This comment has been minimized.

Copy link

feelwhy replied Mar 18, 2019

Thank you!

Please sign in to comment.
You can’t perform that action at this time.