[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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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()):
|
||||
|
||||
@@ -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'):
|
||||
|
||||
@@ -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='<p>Test Post Performances with an inbox ping</p>',
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user