Skip to content

Commit

Permalink
[FIX] web: MockServer: search_read: handle sort on many2one
Browse files Browse the repository at this point in the history
Before this commit, if the records had to be sorted according a
many2one field, and there was a sequence field on the many2one
comodel, the sequence was ignored. Now, the sequence is taken into
account like the actual server does.

Linked to odoo/enterprise/pull/7407

closes #42595

Related: odoo/enterprise#7407
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
  • Loading branch information
aab-odoo committed Jan 14, 2020
1 parent 397a683 commit 3316fd4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions addons/web/static/tests/helpers/mock_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,15 +998,30 @@ var MockServer = Class.extend({
return result;
});
if (args.sort) {
// deal with sort on multiple fields (i.e. only consider the first)
// warning: only consider first level of sort
args.sort = args.sort.split(',')[0];
var fieldName = args.sort.split(' ')[0];
var order = args.sort.split(' ')[1];
var sortField = self.data[args.model].fields[fieldName];
processedRecords.sort(function (r1, r2) {
if (r1[fieldName] < r2[fieldName]) {
var v1 = r1[fieldName];
var v2 = r2[fieldName];
if (sortField.type === 'many2one') {
var coRecords = self.data[sortField.relation].records;
if (self.data[sortField.relation].fields.sequence) {
// use sequence field of comodel to sort records
v1 = coRecords.find(r => r.id === v1[0]).sequence;
v2 = coRecords.find(r => r.id === v2[0]).sequence;
} else {
// sort by id
v1 = v1[0];
v2 = v2[0];
}
}
if (v1 < v2) {
return order === 'ASC' ? -1 : 1;
}
if (r1[fieldName] > r2[fieldName]) {
if (v1 > v2) {
return order === 'ASC' ? 1 : -1;
}
return 0;
Expand Down

0 comments on commit 3316fd4

Please sign in to comment.