diff --git a/web_excel_export_dynamic_expand/README.rst b/web_excel_export_dynamic_expand/README.rst new file mode 100644 index 000000000000..5e8407e41f76 --- /dev/null +++ b/web_excel_export_dynamic_expand/README.rst @@ -0,0 +1,82 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=============================== +Web Excel Export Dynamic Expand +=============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:0ebaf3ae34e14b004119de667f5e9923e0e3cc6a0e57349fd4b860af03a2b9c2 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/19.0/web_excel_export_dynamic_expand + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-19-0/web-19-0-web_excel_export_dynamic_expand + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +When exporting a tree view to an Excel file, you can decide if you want +to export only the main groups or the full tree, depending on if all +groups are collapsed, or there is one open. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ForgeFlow + +Contributors +------------ + +- Arnau Cruz + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_excel_export_dynamic_expand/__init__.py b/web_excel_export_dynamic_expand/__init__.py new file mode 100644 index 000000000000..e046e49fbe22 --- /dev/null +++ b/web_excel_export_dynamic_expand/__init__.py @@ -0,0 +1 @@ +from . import controllers diff --git a/web_excel_export_dynamic_expand/__manifest__.py b/web_excel_export_dynamic_expand/__manifest__.py new file mode 100644 index 000000000000..962b71739097 --- /dev/null +++ b/web_excel_export_dynamic_expand/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright (C) 2024 - ForgeFlow S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Web Excel Export Dynamic Expand", + "summary": "Export collapsed groups or the full tree, based on its view.", + "version": "19.0.1.0.0", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/web", + "license": "AGPL-3", + "depends": ["web"], + "data": [], + "assets": { + "web.assets_backend": [ + "web_excel_export_dynamic_expand/static/src/js/data_export.esm.js", + ], + }, +} diff --git a/web_excel_export_dynamic_expand/controllers/__init__.py b/web_excel_export_dynamic_expand/controllers/__init__.py new file mode 100644 index 000000000000..14a7468918e2 --- /dev/null +++ b/web_excel_export_dynamic_expand/controllers/__init__.py @@ -0,0 +1 @@ +from . import excel_export diff --git a/web_excel_export_dynamic_expand/controllers/excel_export.py b/web_excel_export_dynamic_expand/controllers/excel_export.py new file mode 100644 index 000000000000..a5d96f866abb --- /dev/null +++ b/web_excel_export_dynamic_expand/controllers/excel_export.py @@ -0,0 +1,38 @@ +# Copyright 2024 ForgeFlow S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import json +from collections import OrderedDict + +from odoo import http + +from odoo.addons.web.controllers.export import ExcelExport + + +class CustomGroupsTreeNode(ExcelExport): + @http.route("/web/export/xlsx", type="http", auth="user") + def web_export_xlsx(self, data): + params = json.loads(data) + self.context = params.get("context", {}) + response = super().web_export_xlsx(data) + return response + + @property + def context(self): + return self._context + + @context.setter + def context(self, value): + self._context = value + + def from_group_data(self, fields, columns_headers, groups): + collapse_groups = self.context.get("collapse_groups") + if collapse_groups: + for _child_key, child_node in groups.children.items(): + aggregated_values = child_node.aggregated_values + if child_node.children: + child_node.children = OrderedDict() + if child_node.data: + child_node.data = [] + child_node.aggregated_values = aggregated_values + return super().from_group_data(fields, columns_headers, groups) diff --git a/web_excel_export_dynamic_expand/i18n/it.po b/web_excel_export_dynamic_expand/i18n/it.po new file mode 100644 index 000000000000..5eaf2c20946c --- /dev/null +++ b/web_excel_export_dynamic_expand/i18n/it.po @@ -0,0 +1,14 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" diff --git a/web_excel_export_dynamic_expand/i18n/web_excel_export_dynamic_expand.pot b/web_excel_export_dynamic_expand/i18n/web_excel_export_dynamic_expand.pot new file mode 100644 index 000000000000..6603b4a88cd9 --- /dev/null +++ b/web_excel_export_dynamic_expand/i18n/web_excel_export_dynamic_expand.pot @@ -0,0 +1,13 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" diff --git a/web_excel_export_dynamic_expand/pyproject.toml b/web_excel_export_dynamic_expand/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/web_excel_export_dynamic_expand/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/web_excel_export_dynamic_expand/readme/CONTRIBUTORS.md b/web_excel_export_dynamic_expand/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..05cf886890dd --- /dev/null +++ b/web_excel_export_dynamic_expand/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Arnau Cruz \ diff --git a/web_excel_export_dynamic_expand/readme/DESCRIPTION.md b/web_excel_export_dynamic_expand/readme/DESCRIPTION.md new file mode 100644 index 000000000000..86a4b3cb99a2 --- /dev/null +++ b/web_excel_export_dynamic_expand/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +When exporting a tree view to an Excel file, you can decide if you want +to export only the main groups or the full tree, depending on if all groups +are collapsed, or there is one open. diff --git a/web_excel_export_dynamic_expand/static/description/icon.png b/web_excel_export_dynamic_expand/static/description/icon.png new file mode 100644 index 000000000000..1dcc49c24f36 Binary files /dev/null and b/web_excel_export_dynamic_expand/static/description/icon.png differ diff --git a/web_excel_export_dynamic_expand/static/description/index.html b/web_excel_export_dynamic_expand/static/description/index.html new file mode 100644 index 000000000000..de95bb4d6916 --- /dev/null +++ b/web_excel_export_dynamic_expand/static/description/index.html @@ -0,0 +1,431 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Web Excel Export Dynamic Expand

+ +

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runboat

+

When exporting a tree view to an Excel file, you can decide if you want +to export only the main groups or the full tree, depending on if all +groups are collapsed, or there is one open.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/web project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/web_excel_export_dynamic_expand/static/src/js/data_export.esm.js b/web_excel_export_dynamic_expand/static/src/js/data_export.esm.js new file mode 100644 index 000000000000..19a7fb9ba427 --- /dev/null +++ b/web_excel_export_dynamic_expand/static/src/js/data_export.esm.js @@ -0,0 +1,38 @@ +import {patch} from "@web/core/utils/patch"; +import {ListController} from "@web/views/list/list_controller"; +import {useBus} from "@web/core/utils/hooks"; + +function computeCollapseGroups(root) { + const groups = root.groups; // Undefined/empty for ungrouped lists + return ( + Array.isArray(groups) && groups.length > 0 && groups.every((g) => g.isFolded) + ); +} + +patch(ListController.prototype, { + setup() { + const searchModel = this.env.searchModel; + if (searchModel) { + useBus(searchModel, "direct-export-data", () => { + if (this.model && this.model.root) { + this.model.root.config.context = { + ...this.model.root.config.context, + collapse_groups: computeCollapseGroups(this.model.root), + }; + } + }); + } + super.setup(...arguments); + + const originalExportRecords = this.exportRecords; + this.exportRecords = async (...args) => { + if (this.model && this.model.root) { + this.model.root.config.context = { + ...this.model.root.config.context, + collapse_groups: computeCollapseGroups(this.model.root), + }; + } + return originalExportRecords.call(this, ...args); + }; + }, +}); diff --git a/web_excel_export_dynamic_expand/tests/__init__.py b/web_excel_export_dynamic_expand/tests/__init__.py new file mode 100644 index 000000000000..e08618ff665a --- /dev/null +++ b/web_excel_export_dynamic_expand/tests/__init__.py @@ -0,0 +1 @@ +from . import test_excel_export diff --git a/web_excel_export_dynamic_expand/tests/test_excel_export.py b/web_excel_export_dynamic_expand/tests/test_excel_export.py new file mode 100644 index 000000000000..b490f9c98e35 --- /dev/null +++ b/web_excel_export_dynamic_expand/tests/test_excel_export.py @@ -0,0 +1,80 @@ +# Copyright 2024 ForgeFlow S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import json +from collections import OrderedDict +from unittest.mock import patch + +from odoo.tests.common import TransactionCase + +from odoo.addons.web_excel_export_dynamic_expand.controllers.excel_export import ( + CustomGroupsTreeNode, +) + + +class MockGroupNode: + def __init__(self, children=None, data=None, aggregated_values=None): + self.children = OrderedDict(children) if children else OrderedDict() + self.data = data or [] + self.aggregated_values = aggregated_values or {} + + +class TestExcelExportDynamicExpand(TransactionCase): + def setUp(self): + super().setUp() + self.controller = CustomGroupsTreeNode() + + @patch("odoo.addons.web.controllers.export.ExcelExport.web_export_xlsx") + def test_web_export_xlsx_context(self, mock_super_export): + mock_super_export.return_value = "dummy_response" + data = json.dumps({"context": {"collapse_groups": True}}) + + response = self.controller.web_export_xlsx(data) + + if hasattr(response, "data"): + self.assertEqual(response.data, b"dummy_response") + else: + self.assertEqual(response, "dummy_response") + + self.assertEqual(self.controller.context.get("collapse_groups"), True) + mock_super_export.assert_called_once_with(data) + + @patch("odoo.addons.web.controllers.export.ExcelExport.from_group_data") + def test_from_group_data_collapse(self, mock_super_from_group_data): + self.controller.context = {"collapse_groups": True} + + child1 = MockGroupNode( + data=[1, 2], + aggregated_values={"val": 10}, + children={"sub": MockGroupNode()}, + ) + groups = MockGroupNode(children={"child1": child1}) + + self.controller.from_group_data([], [], groups) + + self.assertEqual(child1.children, OrderedDict()) + self.assertEqual(child1.data, []) + self.assertEqual(child1.aggregated_values, {"val": 10}) + mock_super_from_group_data.assert_called_once() + + @patch("odoo.addons.web.controllers.export.ExcelExport.from_group_data") + def test_from_group_data_no_collapse(self, mock_super_from_group_data): + self.controller.context = {"collapse_groups": False} + + child1 = MockGroupNode( + data=[1, 2], + aggregated_values={"val": 10}, + children={"sub": MockGroupNode()}, + ) + groups = MockGroupNode(children={"child1": child1}) + + self.controller.from_group_data([], [], groups) + + self.assertEqual(len(child1.children), 1) + self.assertEqual(child1.data, [1, 2]) + self.assertEqual(child1.aggregated_values, {"val": 10}) + mock_super_from_group_data.assert_called_once() + + def test_context_property(self): + self.controller.context = {"test": 1} + self.assertEqual(self.controller.context, {"test": 1})