diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 6b23084fca7..72c5d7af029 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -110,7 +110,7 @@ class MailThread(models.AbstractModel): message_has_error_counter = fields.Integer( '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_attachment_count = fields.Integer('Attachment Count', compute='_compute_message_attachment_count') message_main_attachment_id = fields.Many2one(string="Main Attachment", comodel_name='ir.attachment') @api.one @@ -147,12 +147,6 @@ class MailThread(models.AbstractModel): # using read() below is much faster than followers.mapped('res_id') return [('id', 'in', [res['res_id'] for res in followers.read(['res_id'])])] - @api.multi - def _compute_related_attachment_count(self): - for record in self: - domain = [('res_id', '=', record.id), ('res_model', '=', self._name)] - record.related_attachment_count = self.env['ir.attachment'].search_count(domain) - @api.multi @api.depends('message_follower_ids') def _compute_is_follower(self): @@ -244,6 +238,16 @@ class MailThread(models.AbstractModel): def _search_message_has_error(self, operator, operand): return [('message_ids.has_error', operator, operand)] + @api.multi + def _compute_message_attachment_count(self): + read_group_var = self.env['ir.attachment'].read_group([('res_model', '=', self._name)], + fields=['res_id'], + groupby=['res_id']) + + attachment_count_dict = dict((d['res_id'], d['res_id_count']) for d in read_group_var) + for record in self: + record.message_attachment_count = attachment_count_dict.get(record.id, 0) + # ------------------------------------------------------ # CRUD overrides for automatic subscription and logging # ------------------------------------------------------ diff --git a/addons/mail/static/src/js/chatter.js b/addons/mail/static/src/js/chatter.js index 0814723caf0..780cd58deec 100644 --- a/addons/mail/static/src/js/chatter.js +++ b/addons/mail/static/src/js/chatter.js @@ -79,7 +79,7 @@ var Chatter = Widget.extend({ start: function () { this._$topbar = this.$('.o_chatter_topbar'); this.$('.o_topbar_right_area').append(QWeb.render('mail.chatter.Attachment.Button', { - count: this.record.data.related_attachment_count || 0, + count: this.record.data.message_attachment_count || 0, })); // render and append the buttons this._$topbar.prepend(QWeb.render('mail.chatter.Buttons', { @@ -270,7 +270,7 @@ var Chatter = Widget.extend({ if (self._reloadAfterPost(messageData)) { self.trigger_up('reload'); } else if (messageData.attachment_ids.length) { - self.trigger_up('reload', {fieldNames: ['related_attachment_count']}); + self.trigger_up('reload', {fieldNames: ['message_attachment_count']}); } }); }); @@ -364,7 +364,7 @@ var Chatter = Widget.extend({ * @private */ _updateAttachmentCounter: function () { - var count = this.record.data.related_attachment_count || 0; + var count = this.record.data.message_attachment_count || 0; this.$('.o_chatter_attachment_button_count').html(' ('+ count +')'); this.$('.o_chatter_button_attachment').toggleClass('o_hidden', !count); }, diff --git a/addons/mail/static/src/js/form_renderer.js b/addons/mail/static/src/js/form_renderer.js index e01bdf18633..21477f825c9 100644 --- a/addons/mail/static/src/js/form_renderer.js +++ b/addons/mail/static/src/js/form_renderer.js @@ -29,7 +29,7 @@ FormRenderer.include({ */ confirmChange: function (state, id, fields) { if (this.chatter) { - var chatterFields = ['related_attachment_count'].concat(_.values(this.mailFields)); + var chatterFields = ['message_attachment_count'].concat(_.values(this.mailFields)); var updatedMailFields = _.intersection(fields, chatterFields); if (updatedMailFields.length) { this.chatter.update(state, updatedMailFields); diff --git a/addons/mail/static/src/js/form_view.js b/addons/mail/static/src/js/form_view.js index 2eaed79e8c0..0e1623d0dcf 100644 --- a/addons/mail/static/src/js/form_view.js +++ b/addons/mail/static/src/js/form_view.js @@ -4,7 +4,7 @@ odoo.define('mail.FormView', function (require) { var FormView = require('web.FormView'); /** - * This file is used to add "related_attachment_count" to fieldsInfo so we can fetch its value for the + * This file is used to add "message_attachment_count" to fieldsInfo so we can fetch its value for the * chatter's attachment button without having it explicitly declared in the form view template. * */ @@ -17,7 +17,7 @@ FormView.include({ this._super.apply(this, arguments); if ('message_ids' in this.fieldsInfo[this.viewType]) { - this.fieldsInfo[this.viewType].related_attachment_count = {}; + this.fieldsInfo[this.viewType].message_attachment_count = {}; } }, }); diff --git a/addons/mail/static/tests/chatter_tests.js b/addons/mail/static/tests/chatter_tests.js index 379b128fe89..00705b0c26e 100644 --- a/addons/mail/static/tests/chatter_tests.js +++ b/addons/mail/static/tests/chatter_tests.js @@ -55,14 +55,14 @@ QUnit.module('Chatter', { type: 'selection', selection: [['overdue', 'Overdue'], ['today', 'Today'], ['planned', 'Planned']], }, - related_attachment_count: { + message_attachment_count: { string: 'Attachment count', type: 'integer', }, }, records: [{ id: 2, - related_attachment_count: 3, + message_attachment_count: 3, display_name: "first partner", foo: "HELLO", message_follower_ids: [],