Skip to content

Commit

Permalink
[IMP] spreadsheet_oca: Allow to import lists
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Jun 10, 2023
1 parent 4765373 commit 5ecbb1f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
2 changes: 2 additions & 0 deletions spreadsheet_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"spreadsheet_oca/static/src/spreadsheet/spreadsheet_action.esm.js",
"spreadsheet_oca/static/src/spreadsheet/pivot_controller.esm.js",
"spreadsheet_oca/static/src/spreadsheet/graph_controller.esm.js",
"spreadsheet_oca/static/src/spreadsheet/list_controller.esm.js",
"spreadsheet_oca/static/src/spreadsheet/list_renderer.esm.js",
],
"spreadsheet.o_spreadsheet": [
"spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet.xml",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @odoo-module **/

import ListDataSource from "@spreadsheet/list/list_data_source";
import PivotDataSource from "@spreadsheet/pivot/pivot_data_source";
import {SpreadsheetControlPanel} from "./spreadsheet_controlpanel.esm";
import {SpreadsheetRenderer} from "./spreadsheet_renderer.esm";
Expand Down Expand Up @@ -91,7 +92,7 @@ export class ActionSpreadsheetOca extends Component {
definition,
});
}
async importDataPivot(spreadsheet_model) {
importCreateOrReuseSheet(spreadsheet_model) {
var sheetId = spreadsheet_model.getters.getActiveSheetId();
var row = 0;
if (this.import_data.new === undefined && this.import_data.new_sheet) {
Expand Down Expand Up @@ -128,6 +129,48 @@ export class ActionSpreadsheetOca extends Component {
}
row += 1;
}
return {sheetId, row};
}
async importDataList(spreadsheet_model) {
var {sheetId, row} = this.importCreateOrReuseSheet(spreadsheet_model);
const dataSourceId = uuidGenerator.uuidv4();
var list_info = {
metaData: {
resModel: this.import_data.metaData.model,
columns: this.import_data.metaData.columns.map((column) => column.name),
fields: this.import_data.metaData.fields,
},
searchParams: {
domain: this.import_data.metaData.domain,
context: this.import_data.metaData.context,
orderBy: this.import_data.metaData.orderBy,
},
name: this.import_data.metaData.name,
};
const dataSource = spreadsheet_model.config.dataSources.add(
dataSourceId,
ListDataSource,
list_info
);
await dataSource.load();
spreadsheet_model.dispatch("INSERT_ODOO_LIST", {
sheetId,
col: 0,
row: row,
id: spreadsheet_model.getters.getNextListId(),
dataSourceId,
definition: list_info,
linesNumber: this.import_data.metaData.threshold,
columns: this.import_data.metaData.columns,
});
const columns = [];
for (let col = 0; col < this.import_data.metaData.columns.length; col++) {
columns.push(col);
}
spreadsheet_model.dispatch("AUTORESIZE_COLUMNS", {sheetId, cols: columns});
}
async importDataPivot(spreadsheet_model) {
var {sheetId, row} = this.importCreateOrReuseSheet(spreadsheet_model);
const dataSourceId = uuidGenerator.uuidv4();
const pivot_info = {
metaData: {
Expand Down Expand Up @@ -167,6 +210,9 @@ export class ActionSpreadsheetOca extends Component {
if (this.import_data.mode === "graph") {
await this.importDataGraph(spreadsheet_model);
}
if (this.import_data.mode === "list") {
await this.importDataList(spreadsheet_model);
}
}
}
ActionSpreadsheetOca.template = "spreadsheet_oca.ActionSpreadsheetOca";
Expand Down
14 changes: 14 additions & 0 deletions spreadsheet_oca/static/src/spreadsheet/list_controller.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @odoo-module **/
import {ListController} from "@web/views/list/list_controller";

import {patch} from "web.utils";

patch(
ListController.prototype,
"spreadsheet_oca/static/src/spreadsheet/list_controller.esm.js",
{
onSpreadsheetButtonClicked() {
this.env.bus.trigger("addListOnSpreadsheet");
},
}
);
58 changes: 58 additions & 0 deletions spreadsheet_oca/static/src/spreadsheet/list_renderer.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** @odoo-module **/
import {useBus, useService} from "@web/core/utils/hooks";
import {ListRenderer} from "@web/views/list/list_renderer";
import {omit} from "@web/core/utils/objects";
import {patch} from "web.utils";

patch(
ListRenderer.prototype,
"spreadsheet_oca/static/src/spreadsheet/list_renderer.esm.js",
{
setup() {
this._super(...arguments);
this.userService = useService("user");
this.actionService = useService("action");
useBus(
this.env.bus,
"addListOnSpreadsheet",
this.onAddListOnSpreadsheet.bind(this)
);
},
onAddListOnSpreadsheet() {
const model = this.env.model.root;
this.actionService.doAction(
"spreadsheet_oca.spreadsheet_spreadsheet_import_act_window",
{
additionalContext: {
default_name: this.env.config.getDisplayName(),
default_import_data: {
mode: "list",
metaData: {
model: model.resModel,
domain: model.domain,
orderBy: model.orderBy,
context: omit(
model.context,
...Object.keys(this.userService.context)
),
columns: this.getSpreadsheetColumns(),
fields: model.fields,
name: this.env.config.getDisplayName(),
threshold: Math.min(model.count, model.limit),
},
},
},
}
);
},
getSpreadsheetColumns() {
const fields = this.env.model.root.fields;
return this.state.columns
.filter(
(col) => col.type === "field" && fields[col.name].type !== "binary"
// We want to avoid binary fields
)
.map((col) => ({name: col.name, type: fields[col.name].type}));
},
}
);
15 changes: 15 additions & 0 deletions spreadsheet_oca/static/src/spreadsheet/spreadsheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
</xpath>
</t>

<t t-inherit="web.ListView.Buttons" t-inherit-mode="extension" owl="1">
<xpath expr="//button[hasclass('o_list_export_xlsx')]/.." position="after">
<t t-if="!env.isSmall">
<button
type="button"
class="btn btn-secondary fa fa-table o_list_export_spreadsheet"
data-tooltip="Add to spreadsheet"
aria-label="Add to spreadesheet"
t-on-click="(ev) => this.onSpreadsheetButtonClicked(ev)"
/>
</t>
</xpath>
</t>


<t t-inherit="web.GraphView.Buttons" t-inherit-mode="extension" owl="1">
<xpath expr="//button[hasclass('fa-pie-chart')]" position="after">
<button
Expand Down

0 comments on commit 5ecbb1f

Please sign in to comment.