Trợ giúp

1

Chatter currently orders messages by id. Is there a way to order items by date?

Avatar
foo

Odoo 11. In the chatter dialog beneath a record, all the logs (from Log Note) are ordered by id. This is defined on the mail.message model:


```

class Message(models.Model):

""" Messages model: system notification (replacing res.log notifications),

comments (OpenChatter discussion) and incoming emails. """

_name = 'mail.message'

_description = 'Message'

_order = 'id desc'

```

Is there a way to order this by date? Simply inheriting the model and setting the _order did not work. eg. this is what I thought would work:

```

class Message(models.Model):

_inherit = 'mail.message'

_order = 'date desc'

```

Considering chatter has some Javascript involvement, I imagine there may be somewhere to change this in the javascript layer?

Thank you in advance! 

Ảnh đại diện
Huỷ bỏ
1 Trả lời
1
Avatar
Adrien
Best Answer

Hi,

I managed to do that the following way:

1. What you want is to modify the function _fetchDocumentMessages in JS file chat_manager.js from native 'mail' addon, in ChatManager class. Instead of sort by message id, you want to sort by date :

_fetchDocumentMessages : function (ids, options) {
var loaded_msgs = _.filter(messages, function (message) {
return _.contains(ids, message.id);
});
var loaded_msg_ids = _.pluck(loaded_msgs, 'id');

options = options || {};
if (options.force_fetch || _.difference(ids.slice(0, LIMIT), loaded_msg_ids).length) {
var ids_to_load = _.difference(ids, loaded_msg_ids).slice(0, LIMIT);

return this._rpc({
model: 'mail.message',
method: 'message_format',
args: [ids_to_load],
context: session.user_context,
})
.then(function (msgs) {
var processed_msgs = [];
_.each(msgs, function (msg) {
processed_msgs.push(add_message(msg, {silent: true}));
});
return _.sortBy(loaded_msgs.concat(processed_msgs), function (msg) {
//CUSTOM HERE: sort by date instead of id in native
return msg.date;
});
});
} else {
return $.when(loaded_msgs);
}
},

2. Thing is, the way ChatManager is defined, you cannot properly inherit it in your custom module (I can't explain exactly why, i am not JS expert)... In this case I found out it is not possible to use a "include" as usually done to overwrite only a JS class method.

3. So I copied / paste the whole chat_manager.js file from 'mail' module to my custom module, and made the previous modification in my pasted file

4. Then i told odoo to replace the native file by mine, by putting in the xml (note the expr in xpath and 'replace' position):

<template id="assets_backend" name="sort message by date assets" inherit_id="web.assets_backend">
<!--unable to inherit javascript properly. So whole script is replaced here-->
<!--see https://www.odoo.com/fr_FR/forum/aide-1/question/how-inheritance-of-a-js-mail-chat-manager-130963-->
<xpath expr="//script[@src='/mail/static/src/js/chat_manager.js']" position="replace">
<script src="/your_custom_module/static/src/js/chat_manager.js" type="text/javascript"/>
</xpath>

</template>
Ảnh đại diện
Huỷ bỏ