[FIX] web: create a record with the widget "many2many"

Before this commit, creating a new record with the "many2many" widget
did not allow to use a context containing a default value
for the parentID (id of the Record containing the many2many).
This value is ignored.

How to reproduce?
- Go to a form view with a field using the "many2many" widget
 with context = {"default_parentID" = ...}
- Click on "Add a new line"
- Click on "Create" (The form view should display the parentID field)

Before this commit:
    The parentID field is empty.

After this commit:
    The parentID field contains the record corresponding
    to "default_parentID".

closes odoo/odoo#102157

X-original-commit: a52eacc5397dc2f98eb052f4cd1e362a97621074
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2022-10-04 20:58:07 +02:00
parent 2c60272507
commit afdaf8cb4f
4 changed files with 74 additions and 12 deletions
@@ -1233,14 +1233,16 @@ export class RelationalModel extends Model {
return newRecord;
}
async addNewRecord(list, params) {
async addNewRecord(list, params, withParentId = true) {
const parentId = this.__bm__.localData[list.__bm_handle__].parentID;
const fieldName = list.__fieldName__;
const context = this.__bm__._getContext(this.__bm__.localData[parentId], { fieldName });
params.context = makeContext([context, params.context]);
params.__syncParent = () => list.__syncData();
const newRecord = this.createDataPoint("record", params);
newRecord.__bm_load_params__.parentID = list.__bm_handle__;
if (withParentId) {
newRecord.__bm_load_params__.parentID = list.__bm_handle__;
}
await newRecord.load();
return newRecord;
}
@@ -561,6 +561,7 @@ export function useOpenX2ManyRecord({
getList,
updateRecord,
saveRecord,
withParentId,
}) {
const viewService = useService("view");
const userService = useService("user");
@@ -601,7 +602,7 @@ export function useOpenX2ManyRecord({
mode: "edit",
viewType: "form",
};
record = await model.addNewRecord(list, recordParams);
record = await model.addNewRecord(list, recordParams, withParentId);
}
addDialog(
@@ -617,15 +618,19 @@ export function useOpenX2ManyRecord({
await saveRecord(rec);
}
if (saveAndNew) {
return model.addNewRecord(list, {
context: list.context,
resModel: resModel,
activeFields: form.activeFields,
fields: { ...form.fields },
views: { form },
mode: "edit",
viewType: "form",
});
return model.addNewRecord(
list,
{
context: list.context,
resModel: resModel,
activeFields: form.activeFields,
fields: { ...form.fields },
views: { form },
mode: "edit",
viewType: "form",
},
withParentId
);
}
},
title,
@@ -67,6 +67,7 @@ export class X2ManyField extends Component {
getList: () => this.list,
saveRecord,
updateRecord,
withParentId: this.activeField.widget !== "many2many",
});
this._openRecord = (params) => {
const activeElement = document.activeElement;
@@ -12,6 +12,7 @@ import {
getNodesTextContent,
nextTick,
patchWithCleanup,
addRow,
} from "@web/../tests/helpers/utils";
import { editSearch, validateSearch } from "@web/../tests/search/helpers";
import { Many2XAutocomplete } from "@web/views/fields/relational_utils";
@@ -1564,6 +1565,59 @@ QUnit.module("Fields", (hooks) => {
assert.verifySteps([]);
});
QUnit.test(
"many2many widget: creates a new record with a context containing the parentID",
async function (assert) {
serverData.views = {
"turtle,false,list": '<tree><field name="display_name"/></tree>',
"turtle,false,search": '<search><field name="display_name"/></search>',
"turtle,false,form":
'<form string="Turtle Power"><field name="turtle_trululu"/></form>',
};
await makeView({
type: "form",
resModel: "partner",
serverData,
arch: `
<form>
<field name="turtles" widget="many2many" context="{'default_turtle_trululu': id}" >
<tree>
<field name="turtle_foo"/>
</tree>
</field>
</form>`,
resId: 1,
mockRPC(route, args) {
const { method, kwargs } = args;
assert.step(method);
if (method === "onchange") {
assert.strictEqual(kwargs.context.default_turtle_trululu, 1);
assert.deepEqual(args.args, [
[],
{},
[],
{
turtle_trululu: "",
},
]);
}
},
});
assert.verifySteps(["get_views", "read", "read"]);
await addRow(target);
assert.verifySteps(["get_views", "web_search_read"]);
await click(target, ".o_create_button");
assert.strictEqual(
target.querySelector("[name='turtle_trululu'] input").value,
"first record"
);
assert.verifySteps(["get_views", "onchange"]);
}
);
QUnit.test("onchange with 40+ commands for a many2many", async function (assert) {
// this test ensures that the basic_model correctly handles more LINK_TO
// commands than the limit of the dataPoint (40 for x2many kanban)