[FIX] spreadsheet: traceback while inserting chart from other model

Steps to reproduce:
- Insert a list/pivot in a blank spreadsheet from a module (say Sales).
- Apply a global filter on it.
- Insert an odoo chart in the same sheet but from a different module (eg. CRM)

This works just fine when chart is inserted from the same model because it matches
the existing filter (from the pivot/list), and directly returns that field matching
without check the field matching from charts.

However in case of different model, the issue is that when there are no charts in the
sheet, the existing code of `getOdooChartIds` retrieves the incorrect chart ids
(`getChartIds` getter returns all chart ids, including the id of chart being inserted).
This leads to a traceback as the code tries to fetch fieldMatchings for a non-existent
chart within the sheet.

This commit resolves the issue by modifying the `getOdooChartIds` method to now utilize
`this.charts` instead of `getChartIds` getter, which correctly provides the ids of
charts already present in the sheet.

Task ID: 3573402

closes odoo/odoo#140700

Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
This commit is contained in:
Khushi
2023-11-16 08:57:45 +00:00
parent 9008399a7b
commit 905d8565ad
2 changed files with 14 additions and 9 deletions
@@ -86,15 +86,7 @@ export class OdooChartCorePlugin extends CorePlugin {
* @returns {Array<string>}
*/
getOdooChartIds() {
const ids = [];
for (const sheetId of this.getters.getSheetIds()) {
ids.push(
...this.getters
.getChartIds(sheetId)
.filter((id) => this.getters.getChartType(id).startsWith("odoo_"))
);
}
return ids;
return Object.keys(this.charts);
}
/**
@@ -5,7 +5,10 @@ import { OdooChart } from "@spreadsheet/chart/odoo_chart/odoo_chart";
import { OdooLineChart } from "@spreadsheet/chart/odoo_chart/odoo_line_chart";
import { nextTick } from "@web/../tests/helpers/utils";
import { createSpreadsheetWithChart, insertChartInSpreadsheet } from "../../utils/chart";
import { insertListInSpreadsheet } from "../../utils/list";
import { createModelWithDataSource, waitForDataSourcesLoaded } from "../../utils/model";
import { addGlobalFilter } from "../../utils/commands";
import { THIS_YEAR_GLOBAL_FILTER } from "../../utils/global_filter";
import * as spreadsheet from "@odoo/o-spreadsheet";
import { makeServerError } from "@web/../tests/helpers/mock_server";
import { session } from "@web/session";
@@ -519,4 +522,14 @@ QUnit.module("spreadsheet > odoo chart plugin", {}, () => {
[1, 3]
);
});
QUnit.test("Can insert odoo chart from a different model", async (assert) => {
const model = await createModelWithDataSource();
insertListInSpreadsheet(model, { model: "product", columns: ["name"] });
await addGlobalFilter(model, THIS_YEAR_GLOBAL_FILTER);
const sheetId = model.getters.getActiveSheetId();
assert.strictEqual(model.getters.getChartIds(sheetId).length, 0);
insertChartInSpreadsheet(model);
assert.strictEqual(model.getters.getChartIds(sheetId).length, 1);
});
});