跳至內容
選單
此問題已被標幟
9 回覆
6417 瀏覽次數

In the old forum version there are some scripts like the posted on this question by @Temur

https://www.odoo.com/forum/help-1/question/how-to-import-odoo-module-87139#answer_92013

That allow you to see the votes of your answer or questions in the Odoo Forum by copy and pasting the JS function in the browser JS console and call the function passing the Post ID.

How to do the same for the new version of Odoo Forum in v9 using the new JS API?

For references here is one of the codes that do the trick

var get_likes_by_post = function(post_id) {
    openerp.jsonRpc('/web/dataset/call_kw', 'call', {
        model: 'forum.post.vote',
        method: 'search_read',
        args: [[['post_id','=',post_id]], ['user_id', 'vote']],
        kwargs: { context: openerp.website.get_context()}
    }).then(function(result) { 
        function vote(x) { 
            this.user_name = x.user_id[1];
            this.user_id = x.user_id[0];
            this.vote = parseInt(x.vote);
        }
        res = [];
        _.forEach(result, function(x){ res.push(new vote(x)); }); 
        console.table(res, ['vote','user_id','user_name']);
    })
}

Use it like:

get_likes_by_post(87139)


頭像
捨棄
作者 最佳答案

Here is the code update for the former script that works for Odoo Forum v9 using the new JS API

var get_likes_by_post = function(post_id) {

odoo.__DEBUG__.services['web.ajax'].jsonRpc('/web/dataset/call_kw', 'call', {

model: 'forum.post.vote',

method: 'search_read',

args: [[['post_id','=',post_id]], ['user_id', 'vote']],

kwargs: { context: odoo.__DEBUG__.services['web_editor.base'].get_context()}

}).then(function(result) {

function vote(x) {

this.user_name = x.user_id[1];

this.user_id = x.user_id[0];

this.vote = parseInt(x.vote);

}

res = [];

_.forEach(result, function(x){ res.push(new vote(x)); });

console.table(res, ['vote','user_id','user_name']);

})

}

Use it like the same:

get_likes_by_post(94628)

*** Update ***

Another handy script that you could find helpful to find the negative votes

var get_negative_votes = function(user_id) {

odoo.__DEBUG__.services['web.ajax'].jsonRpc('/web/dataset/call_kw', 'call', {

model: 'forum.post.vote',

method: 'search_read',

args: [[['recipient_id','=',user_id],['vote', '=', '-1']], ['user_id', 'post_id']],

kwargs: { context: odoo.__DEBUG__.services['web_editor.base'].get_context()}

}).then(function(result) {

function vote(x) {

var self = this;

this.user_name = x.user_id[1];

this.user_id = x.user_id[0];

this.post_id = x.post_id[0];

odoo.__DEBUG__.services['web.ajax'].jsonRpc('/web/dataset/call_kw', 'call', {

model: 'forum.post',

method: 'read',

args: [this.post_id, ['name', 'parent_id', 'content_link']],

kwargs: { context: odoo.__DEBUG__.services['web_editor.base'].get_context()}

}).then(function(post) {

console.log(post);

self.post_url = post.content_link;

if(post.parent_id){

odoo.__DEBUG__.services['web.ajax'].jsonRpc('/web/dataset/call_kw', 'call', {

model: 'forum.post',

method: 'read',

args: [post.parent_id[0], ['name', 'content_link']],

kwargs: { context: odoo.__DEBUG__.services['web_editor.base'].get_context()}

}).then(function(post2) {

console.log(post2);

})

}

})

}

res = [];

_.forEach(result, function(x){ res.push(new vote(x)); });

console.table(res, ['post_url','post_id','user_id','user_name']);

})

};

//call it passing the user id, like:

get_negative_votes(162916);

頭像
捨棄

Thank you very much for all your valuable contributions, keep up posting!

作者

Thanks for appreciate this, it really helps to keep going on

最佳答案


And here is one more snippet:


 var get_likes_by_user = function(user_id, separate_posts){

separate_posts = typeof separate_posts !== 'undefined' ? separate_posts : false;

odoo.__DEBUG__.services['web.ajax'].jsonRpc('/web/dataset/call_kw', 'call', {

model: 'forum.post.vote',

method: 'search_read',

args: [[['recipient_id','=',user_id]], ['post_id', 'vote', 'user_id']],

kwargs: { context: odoo.__DEBUG__.services['web_editor.base'].get_context()}

}).then(function(results) {

//_.each(results, function(result) { console.log('Post: ' + result.post_id[0] + ' | ' + result.vote + " by " + result.user_id[1]) });

function vote(x) {

this.user_name = x.user_id[1];

this.user_id = x.user_id[0];

this.vote = parseInt(x.vote);

this.post_id = x.post_id[0];

this.post = x.post_id;

}

res = [];

_.forEach(results, function(x){ res.push(new vote(x)); });

res.sort(function(a,b){

if (a.post_id == b.post_id)

return 0;

if (a.post_id < b.post_id)

return -1;

else

return 1;

});

if (separate_posts){

while(res.length){

post_votes = [];

last_post_id = res[0].post_id;

while (res.length && res[0].post_id == last_post_id) {

post_votes.push(res.shift());

}

console.log('\n\nPost: ' + post_votes[0].post);

console.table(post_votes, ['vote','user_id','user_name']);

}}

else {

console.table(res, ['post_id','vote','user_id','user_name']);

}

})

}


Examples:

get_likes_by_user(342190)
OR

get_likes_by_user(342190, 1)
- here second parameter is for separate posts...

Note: originally above snippets were posted somewhere on this forum, I've slightly modified the original but I would like to credit its author and/or provide link to the original post but I do not remember the exact thread it was posted in, if someone does, feel free to add link here.

頭像
捨棄
作者

Nice

作者

Check my answer update for how to get negative votes info

Nice. But I suggest to use

args: [[['recipient_id','=',user_id],['vote', 'in', ['-1','0']]], ['user_id', 'post_id']],

instead of

args: [[['recipient_id','=',user_id],['vote', '=', '-1']], ['user_id', 'post_id']],
作者

what is the meaning of 0 value of vote in that domain?

相關帖文 回覆 瀏覽次數 活動
3
2月 25
4598
0
2月 25
1066
1
6月 24
1886
1
5月 24
1488
0
2月 24
1252