From 3c2a92e82077b9d5994903e25739028f2fe468ee Mon Sep 17 00:00:00 2001 From: Olivier Colson Date: Mon, 3 Sep 2018 10:15:58 +0200 Subject: [PATCH] [IMP] mail: add main attachment support on mail.thread This is done by adding a new field on mail.thread objects and setting it when posting a message with attachments on a model inheriting from mail.thread. Was part of PR #26740 Was part of task #1880089 --- addons/mail/models/__init__.py | 1 + addons/mail/models/ir_attachment.py | 19 +++++++++++++++++++ addons/mail/models/mail_message.py | 2 ++ addons/mail/models/mail_thread.py | 12 +++++++++--- addons/test_mail/tests/test_performance.py | 4 ++-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 addons/mail/models/ir_attachment.py diff --git a/addons/mail/models/__init__.py b/addons/mail/models/__init__.py index 152f638370c..5319c310790 100644 --- a/addons/mail/models/__init__.py +++ b/addons/mail/models/__init__.py @@ -21,6 +21,7 @@ from . import res_config_settings from . import update from . import ir_action_act_window from . import ir_actions +from . import ir_attachment from . import ir_autovacuum from . import ir_model from . import ir_ui_view diff --git a/addons/mail/models/ir_attachment.py b/addons/mail/models/ir_attachment.py new file mode 100644 index 00000000000..99263c683c5 --- /dev/null +++ b/addons/mail/models/ir_attachment.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models + + +class IrAttachment(models.Model): + _inherit = 'ir.attachment' + + def register_as_main_attachment(self): + """ Registers this attachment as the main one of the model it is + attached to. + """ + self.ensure_one() + related_record = self.env[self.res_model].browse(self.res_id) + # message_main_attachment_id field can be empty, that's why we compare to False; + # we are just checking that it exists on the model before writing it + if hasattr(related_record, 'message_main_attachment_id'): + related_record.message_main_attachment_id = self diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 15d744d4466..fa298bf3c5a 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -391,9 +391,11 @@ class Message(models.Model): for notification in message.notification_ids.filtered(filter_notification): customer_email_data.append((partner_tree[notification.res_partner_id.id][0], partner_tree[notification.res_partner_id.id][1], notification.email_status)) + main_attachment = message.model and message.res_id and getattr(self.env[message.model].browse(message.res_id), 'message_main_attachment_id') attachment_ids = [] for attachment in message.attachment_ids: if attachment.id in attachments_tree: + attachments_tree[attachment.id]['is_main'] = main_attachment == attachment attachment_ids.append(attachments_tree[attachment.id]) tracking_value_ids = [] for tracking_value_id in message_to_tracking.get(message_id, list()): diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 3273c2026eb..6b23084fca7 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -111,6 +111,7 @@ class MailThread(models.AbstractModel): 'Number of error', compute='_compute_message_has_error', help="Number of messages with delivery error") related_attachment_count = fields.Integer('Attachment Count', compute='_compute_related_attachment_count') + message_main_attachment_id = fields.Many2one(string="Main Attachment", comodel_name='ir.attachment') @api.one @api.depends('message_follower_ids') @@ -868,8 +869,8 @@ class MailThread(models.AbstractModel): """ Generic wrapper on ``_notify_get_reply_to`` checking mail.thread inheritance and allowing to call model-specific implementation in a one liner. This method should not be overridden. """ - if records and hasattr(records, '_notify_get_reply_to'): - return records._notify_get_reply_to(default=default, company=company, doc_names=doc_names) + if records and hasattr(records, '_notify_get_reply_to'): + return records._notify_get_reply_to(default=default, company=company, doc_names=doc_names) return self._notify_get_reply_to(default=default, records=records, company=company, doc_names=doc_names) @api.multi @@ -1956,7 +1957,6 @@ class MailThread(models.AbstractModel): to the related document. Should only be set by Chatter. :return int: ID of newly created mail.message """ - if attachments is None: attachments = {} if self.ids and not self.ensure_one(): @@ -2054,6 +2054,12 @@ class MailThread(models.AbstractModel): # - 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'): diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 7b170ef2c31..eebabca82b4 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -271,7 +271,7 @@ class TestAdvMailPerformance(TransactionCase): def test_message_post_one_inbox_notification(self): record = self.env['mail.test.simple'].create({'name': 'Test'}) - with self.assertQueryCount(__system__=31, emp=42): # com runbot 30 - 41 // test_mail only: 31 - 42 + with self.assertQueryCount(__system__=31, emp=43): # com runbot 30 - 42 // test_mail only: 31 - 43 record.message_post( body='

Test Post Performances with an inbox ping

', partner_ids=self.user_test.partner_id.ids, @@ -504,7 +504,7 @@ class TestHeavyMailPerformance(TransactionCase): customer_id = self.customer.id user_id = self.user_portal.id - with self.assertQueryCount(__system__=155, emp=190): # com runbot: 155 - 190 // test_mail only: 155 - 190 + with self.assertQueryCount(__system__=156, emp=190): # com runbot: 156 - 190 // test_mail only: 156 - 190 rec = self.env['mail.test.full'].create({ 'name': 'Test', 'umbrella_id': umbrella_id,