Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage domain on id in single value list as unique [PREVIEW] #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Manage domain on id in single value list as unique
* Do not validate domain and enforce uniqueness for empty fields unless they're required

Version 6.4.5 - 2022-09-06
* Bug fixes (see mercurial logs for details)

Expand Down
26 changes: 13 additions & 13 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2675,19 +2675,19 @@
unique_value: function(domain) {
if ((domain instanceof Array) &&
(domain.length == 1)) {
domain = domain[0];
var name = domain[0];
var value = domain[2];
var count = 0;
if (domain.length == 4 && name.endsWith('.id')) {
count = 1;
var model = domain[3];
value = [model, value];
}
if ((name.split('.').length - 1) == count &&
(domain[1] == '=')) {
return [true, domain[1], value];
}
let [name, operator, value, ...model] = domain[0];
if (operator == '=' ||
(operator == 'in' && value.length == 1)) {
value = operator == '=' ? value : value[0];
var count = 0;
if (model.length && name.endsWith('.id')) {
count = 1;
model = model[0];
value = [model, value];
}
if ((name.split('.').length - 1) == count) {
return [true, name, value];
}
}
return [false, null, null];
},
Expand Down
19 changes: 14 additions & 5 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,9 @@
return true;
}
var invalid = false;
this.get_state_attrs(record).domain_readonly = false;
var state_attrs = this.get_strate_attrs(record);
var is_required = Boolean(parseInt(state_attrs.required, 10));
state_attrs.domain_readonly = false;
var inversion = new Sao.common.DomainInversion();
var domain = inversion.simplify(this.validation_domains(record,
pre_validate));
Expand All @@ -1688,11 +1690,19 @@
} else if (Sao.common.compare(domain, [['id', '=', null]])) {
invalid = 'domain';
} else {
let [screen_domain, _] = this.get_domains(
record, pre_validate);
var uniques = inversion.unique_value(domain);
var unique = uniques[0];
var leftpart = uniques[1];
var value = uniques[2];
if (unique) {
let unique_from_screen = inversion.unique_value(
screen_domain)[0];
if (this._is_empty(record) &&
!is_required &&
!unique_from_screen) {
// Do nothing
} else if (unique) {
// If the inverted domain is so constraint that only one
// value is possible we should use it. But we must also pay
// attention to the fact that the original domain might be
Expand Down Expand Up @@ -1727,16 +1737,15 @@
}
if (setdefault && !pre_validate) {
this.set_client(record, value);
this.get_state_attrs(record).domain_readonly =
domain_readonly;
state_attrs.domain_readonly = domain_readonly;
}
}
if (!inversion.eval_domain(domain,
Sao.common.EvalEnvironment(record))) {
invalid = domain;
}
}
this.get_state_attrs(record).invalid = invalid;
state_attrs.invalid = invalid;
return !invalid;
}
});
Expand Down
10 changes: 10 additions & 0 deletions tests/sao.js
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,16 @@
QUnit.ok(compare(unique_value(domain), [true, '=', ['model', 1]]));
domain = [['a.b.id', '=', 1, 'model']];
QUnit.ok(compare(unique_value(domain), [false, null, null]));
domain = [['a', 'in', [1]]];
QUnit.ok(compare(unique_value(domain), [true, 'a', 1]));
domain = [['a', 'in', [1, 2]]];
QUnit.ok(compare(unique_value(domain), [false, null, null]));
domain = [['a', 'in', []]];
QUnit.ok(compare(unique_value(domain), [false, null, null]));
domain = [['a.b', 'in', [1]]];
QUnit.ok(compare(unique_value(domain), [false, null, null]));
domain = [['a.id', 'in', [1], 'model']];
QUnit.ok(compare(unique_value(domain), [true, 'a.id', ['model', 1]]));
});

QUnit.test('DomainInversion evaldomain', function() {
Expand Down