8000 [FIX] web: domain selector: hide within if allowExpressions = False · odoo/odoo@ba510c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba510c1

Browse files
committed
[FIX] web: domain selector: hide within if allowExpressions = False
The domain field passes a new prop allowExpressions to the domain selector. If that prop is false (default true), the within operator is not proposed for selection for date/datetime fields. This is done to prevent users to introduce expressions in their domains when they are not supported. X-original-commit: e22a5a3
1 parent b077410 commit ba510c1

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

addons/web/static/src/core/domain_selector/domain_selector.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ export class DomainSelector extends Component {
3030
className: { type: String, optional: true },
3131
defaultConnector: { type: [{ value: "&" }, { value: "|" }], optional: true },
3232
isDebugMode: { type: Boolean, optional: true },
33+
allowExpressions: { type: Boolean, optional: true },
3334
readonly: { type: Boolean, optional: true },
3435
update: { type: Function, optional: true },
3536
debugUpdate: { type: Function, optional: true },
3637
};
3738
static defaultProps = {
3839
isDebugMode: false,
40+
allowExpressions: true,
3941
readonly: true,
4042
update: () => {},
4143
};
@@ -107,11 +109,15 @@ export class DomainSelector extends Component {
107109
}
108110

109111
getDefaultOperator(fieldDef) {
110-
return getDomainDisplayedOperators(fieldDef)[0];
112+
return getDomainDisplayedOperators(fieldDef, {
113+
allowExpressions: this.props.allowExpressions,
114+
})[0];
111115
}
112116

113117
getOperatorEditorInfo(fieldDef) {
114-
const operators = getDomainDisplayedOperators(fieldDef);
118+
const operators = getDomainDisplayedOperators(fieldDef, {
119+
allowExpressions: this.props.allowExpressions,
120+
});
115121
return getOperatorEditorInfo(operators, fieldDef);
116122
}
117123

addons/web/static/src/core/domain_selector/domain_selector_operator_editor.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function getDomainDisplayedOperators(fieldDef) {
1+
export function getDomainDisplayedOperators(fieldDef, params = {}) {
22
if (!fieldDef) {
33
fieldDef = {};
44
}
@@ -37,7 +37,18 @@ export function getDomainDisplayedOperators(fieldDef) {
3737
];
3838
case "date":
3939
case "datetime":
40-
return ["=", "!=", ">", ">=", "<", "<=", "between", "within", "set", "not_set"];
40+
return [
41+
"=",
42+
"!=",
43+
">",
44+
">=",
45+
"<",
46+
"<=",
47+
"between",
48+
...("allowExpressions" in params && !params.allowExpressions ? [] : ["within"]),
49+
"set",
50+
"not_set",
51+
];
4152
case "integer":
4253
case "float":
4354
case "monetary":

addons/web/static/src/views/fields/domain/domain_field.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
debugUpdate.bind="debugUpdate"
5151
update.bind="update"
5252
isDebugMode="!!env.debug"
53+
allowExpressions="allowExpressions(props)"
5354
className="props.readonly ? 'o_read_mode' : 'o_edit_mode'"
5455
/>
5556
</div>

addons/web/static/tests/core/domain_field.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getConditionText,
1818
getCurrentPath,
1919
getCurrentValue,
20+
getOperatorOptions,
2021
} from "@web/../tests/core/tree_editor/condition_tree_editor_test_helpers";
2122
import {
2223
contains,
@@ -1061,7 +1062,7 @@ test("folded domain field with withinh operator", async function () {
10611062
<form>
10621063
<sheet>
10631064
<group>
1064-
<field name="foo" widget="domain" options="{'model': 'partner', 'foldable': true}" />
1065+
<field name="foo" widget="domain" options="{'model': 'partner', 'foldable': true, 'allow_expressions':True}" />
10651066
</group>
10661067
</sheet>
10671068
</form>`,
@@ -1138,3 +1139,48 @@ test("allow_expressions = false (default)", async function () {
11381139
expect(".o_field_domain").toHaveClass("o_field_invalid");
11391140
expect.verifySteps(["The domain should not involve non-literals"]);
11401141
});
1142+
1143+
test("hide within operators when allow_expressions = False", async function () {
1144+
Partner._records[0].foo = `[("datetime", "=", False)]`;
1145+
1146+
serverState.debug = "1";
1147+
replaceNotificationService();
1148+
1149+
await mountView({
1150+
type: "form",
1151+
resModel: "partner",
1152+
resId: 1,
1153+
arch: `
1154+
<form>
1155+
<sheet>
1156+
<group>
1157+
<field name="foo" widget="domain" options="{'model': 'partner' }" />
1158+
</group>
1159+
</sheet>
1160+
</form>`,
1161+
});
1162+
1163+
expect(getOperatorOptions()).toEqual([
1164+
"is equal",
1165+
"is not equal",
1166+
"is greater",
1167+
"is greater or equal",
1168+
"is lower",
1169+
"is lower or equal",
1170+
"is between",
1171+
"is set",
1172+
"is not set",
1173+
]);
1174+
1175+
await contains(SELECTORS.debugArea).edit(
1176+
`["&", ("date", ">=", context_today().strftime("%Y-%m-%d")), ("date", "<=", (context_today() + relativedelta(weeks = a)).strftime("%Y-%m-%d"))]`
1177+
);
1178+
await animationFrame();
1179+
expect(".o_field_domain").toHaveClass("o_field_invalid");
1180+
expect.verifySteps(["The domain should not involve non-literals"]);
1181+
1182+
await contains(`${SELECTORS.valueEditor} ${SELECTORS.editor}:first input`).edit("2");
1183+
await animationFrame();
1184+
expect(".o_field_domain").not.toHaveClass("o_field_invalid"); // should we have class? We don't do it in other cases
1185+
expect.verifySteps(["The domain should not involve non-literals"]);
1186+
});

addons/web/static/tests/core/domain_selector/domain_selector.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,3 +2493,25 @@ test("preserve virtual operators in sub domains", async () => {
24932493
`[("product_id", "any", [("team_id", "any", ["&", ("active", "=", False), ("name", "=", False)])])]`,
24942494
]);
24952495
});
2496+
2497+
test("hide within operators when allowExpressions = False", async () => {
2498+
Team._fields.active = fields.Boolean();
2499+
await makeDomainSelector({
2500+
domain: `[("datetime", "=", False)]`,
2501+
allowExpressions: false,
2502+
update(domain) {
2503+
expect.step(domain);
2504+
},
2505+
});
2506+
expect(getOperatorOptions()).toEqual([
2507+
"is equal",
2508+
"is not equal",
2509+
"is greater",
2510+
"is greater or equal",
2511+
"is lower",
2512+
"is lower or equal",
2513+
"is between",
2514+
"is set",
2515+
"is not set",
2516+
]);
2517+
});

0 commit comments

Comments
 (0)
0