Skip to content

Commit

Permalink
[REF] mail: set message_main_attachment_id in message_post_after_hook…
Browse files Browse the repository at this point in the history
… function instead of directly in message_post

This kind of pos-processing should always be done in the hook method.
  • Loading branch information
oco-odoo committed Sep 21, 2018
1 parent a100478 commit 5c6673c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions addons/mail/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,12 +2058,6 @@ def message_post(self, body='', subject=None,
# - HACK TDE FIXME: Chatter: attachments linked to the document (not done JS-side), load the message
attachment_ids = self._message_post_process_attachments(attachments, kwargs.pop('attachment_ids', []), values)
values['attachment_ids'] = attachment_ids
if attachment_ids and self.ids and not self.message_main_attachment_id:
all_attachments = self.env['ir.attachment'].browse([attachment_tuple[1] for attachment_tuple in attachment_ids])
prioritary_attachments = all_attachments.filtered(lambda x: x.mimetype.endswith('pdf')) \
or all_attachments.filtered(lambda x: x.mimetype.startswith('image')) \
or all_attachments
self.write({'message_main_attachment_id': prioritary_attachments[0].id})

# Avoid warnings about non-existing fields
for x in ('from', 'to', 'cc'):
Expand All @@ -2082,6 +2076,14 @@ def _message_post_after_hook(self, message, msg_vals, model_description=False, m
""" Hook to add custom behavior after having posted the message. Both
message and computed value are given, to try to lessen query count by
using already-computed values instead of having to rebrowse things. """
# Set main attachment field if necessary
attachment_ids = msg_vals['attachment_ids']
if attachment_ids and self.ids and not self.message_main_attachment_id:
all_attachments = self.env['ir.attachment'].browse([attachment_tuple[1] for attachment_tuple in attachment_ids])
prioritary_attachments = all_attachments.filtered(lambda x: x.mimetype.endswith('pdf')) \
or all_attachments.filtered(lambda x: x.mimetype.startswith('image')) \
or all_attachments
self.write({'message_main_attachment_id': prioritary_attachments[0].id})
# Notify recipients of the newly-created message (Inbox / Email + channels)
if msg_vals.get('moderation_status') != 'pending_moderation':
message._notify(
Expand Down

0 comments on commit 5c6673c

Please sign in to comment.