From 002a76eaaaaea614ef17259b523c2f18f6105ca0 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 29 Aug 2022 13:11:02 +0000 Subject: [PATCH] [IMP] web,account: orm service: allow to create in batch This commit changes the API of the "create" function of the orm service to better reflect the API of the corresponding method in models.py, which takes a list of record values in argument. Before this commit, a call to "create" only allowed to create a single record. Part-of: odoo/odoo#99018 --- .../components/bills_upload/bills_upload.js | 2 +- addons/web/static/src/core/orm_service.js | 9 ++++--- .../web/static/src/views/relational_model.js | 2 +- .../views/view_dialogs/export_data_dialog.js | 24 ++++++++++--------- .../static/tests/core/orm_service_tests.js | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/addons/account/static/src/components/bills_upload/bills_upload.js b/addons/account/static/src/components/bills_upload/bills_upload.js index 14656b57f8e..0db2516cb21 100644 --- a/addons/account/static/src/components/bills_upload/bills_upload.js +++ b/addons/account/static/src/components/bills_upload/bills_upload.js @@ -26,7 +26,7 @@ export class AccountFileUploader extends Component { mimetype: file.type, datas: file.data, }; - const att_id = await this.orm.create("ir.attachment", att_data, { + const att_id = await this.orm.create("ir.attachment", [att_data], { ...this.props.extraContext, ...this.env.searchModel.context, }); this.attachmentIdsToProcess.push(att_id); diff --git a/addons/web/static/src/core/orm_service.js b/addons/web/static/src/core/orm_service.js index a89fe86b201..9b96dd21f2e 100644 --- a/addons/web/static/src/core/orm_service.js +++ b/addons/web/static/src/core/orm_service.js @@ -109,9 +109,12 @@ export class ORM { return this.rpc(url, params, { silent: this._silent }); } - create(model, state, context) { - validateObject("state", state); - return this.call(model, "create", [state], { context }); + create(model, records, context) { + validateArray("records", records); + for (const record of records) { + validateObject("record", record); + } + return this.call(model, "create", records, { context }); } nameGet(model, ids, context) { diff --git a/addons/web/static/src/views/relational_model.js b/addons/web/static/src/views/relational_model.js index 2121b1bf57a..ae98bbd9972 100644 --- a/addons/web/static/src/views/relational_model.js +++ b/addons/web/static/src/views/relational_model.js @@ -1270,7 +1270,7 @@ export class Record extends DataPoint { ); this.resId = resId; } else { - this.resId = await this.model.orm.create(this.resModel, changes, this.context); + this.resId = await this.model.orm.create(this.resModel, [changes], this.context); } delete this.virtualId; this.data.id = this.resId; diff --git a/addons/web/static/src/views/view_dialogs/export_data_dialog.js b/addons/web/static/src/views/view_dialogs/export_data_dialog.js index 9ddcb27e749..0d6973b82fb 100644 --- a/addons/web/static/src/views/view_dialogs/export_data_dialog.js +++ b/addons/web/static/src/views/view_dialogs/export_data_dialog.js @@ -266,17 +266,19 @@ export class ExportDataDialog extends Component { } const id = await this.orm.create( "ir.exports", - { - name, - export_fields: this.state.exportList.map((field) => [ - 0, - 0, - { - name: field.name, - }, - ]), - resource: this.props.root.resModel, - }, + [ + { + name, + export_fields: this.state.exportList.map((field) => [ + 0, + 0, + { + name: field.name, + }, + ]), + resource: this.props.root.resModel, + }, + ], this.props.context ); this.state.isEditingTemplate = false; diff --git a/addons/web/static/tests/core/orm_service_tests.js b/addons/web/static/tests/core/orm_service_tests.js index 7330c72f673..75e0b4bf188 100644 --- a/addons/web/static/tests/core/orm_service_tests.js +++ b/addons/web/static/tests/core/orm_service_tests.js @@ -94,7 +94,7 @@ QUnit.test("create method", async (assert) => { const [query, rpc] = makeFakeRPC(); serviceRegistry.add("rpc", rpc); const env = await makeTestEnv(); - await env.services.orm.create("partner", { color: "red" }); + await env.services.orm.create("partner", [{ color: "red" }]); assert.strictEqual(query.route, "/web/dataset/call_kw/partner/create"); assert.deepEqual(query.params, { args: [