diff --git a/base_view_inheritance_extension/README.rst b/base_view_inheritance_extension/README.rst new file mode 100644 index 000000000..0b1f6e2d6 --- /dev/null +++ b/base_view_inheritance_extension/README.rst @@ -0,0 +1,102 @@ +========================= +Extended view inheritance +========================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Mature-brightgreen.png + :target: https://odoo-community.org/page/development-status + :alt: Mature +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/15.0/base_view_inheritance_extension + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-15-0/server-tools-15-0-base_view_inheritance_extension + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/149/15.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module was written to make it simple to add custom operators for view +inheritance. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +**Change a python dictionary (context for example)** + + +.. code-block:: xml + + + + { + "key": "value", + } + + + + +Note that views are subject to evaluation of xmlids anyways, so if you need +to refer to some xmlid, say ``%(xmlid)s``. + +Known issues / Roadmap +====================== + +* Support an ``eval`` attribute for our new node types. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Therp BV + +Contributors +~~~~~~~~~~~~ + +* Holger Brunn +* Ronald Portier +* Sergio Teruel +* Iván Todorovich + +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/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_view_inheritance_extension/__init__.py b/base_view_inheritance_extension/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/base_view_inheritance_extension/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_view_inheritance_extension/__manifest__.py b/base_view_inheritance_extension/__manifest__.py new file mode 100644 index 000000000..17b486a92 --- /dev/null +++ b/base_view_inheritance_extension/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2016 Therp BV +# Copyright 2018 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "Extended view inheritance", + "version": "16.0.1.0.0", + "development_status": "Mature", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "LGPL-3", + "category": "Hidden/Dependency", + "summary": "Adds more operators for view inheritance", + "website": "https://github.com/OCA/server-tools", + "depends": ["base"], + "external_dependencies": {"python": ["astor"]}, + "demo": ["demo/ir_ui_view.xml"], +} diff --git a/base_view_inheritance_extension/demo/ir_ui_view.xml b/base_view_inheritance_extension/demo/ir_ui_view.xml new file mode 100644 index 000000000..93562cc7a --- /dev/null +++ b/base_view_inheritance_extension/demo/ir_ui_view.xml @@ -0,0 +1,29 @@ + + + + res.partner + + + + Partner form + + + + { + "default_email": "info@odoo-community.org", + "default_company_id": allowed_company_ids[0] + } + + + + + parent_id + +
+ + + +
+
+
+
diff --git a/base_view_inheritance_extension/i18n/base_view_inheritance_extension.pot b/base_view_inheritance_extension/i18n/base_view_inheritance_extension.pot new file mode 100644 index 000000000..32cd7d633 --- /dev/null +++ b/base_view_inheritance_extension/i18n/base_view_inheritance_extension.pot @@ -0,0 +1,29 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" diff --git a/base_view_inheritance_extension/i18n/ca.po b/base_view_inheritance_extension/i18n/ca.po new file mode 100644 index 000000000..a27155bc6 --- /dev/null +++ b/base_view_inheritance_extension/i18n/ca.po @@ -0,0 +1,40 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Marc Tormo i Bochaca , 2017 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-04-19 17:59+0000\n" +"PO-Revision-Date: 2017-04-19 17:59+0000\n" +"Last-Translator: Marc Tormo i Bochaca , 2017\n" +"Language-Team: Catalan (https://www.transifex.com/oca/teams/23907/ca/)\n" +"Language: ca\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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "Empresa de " + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" + +#~ msgid "A new page" +#~ msgstr "Una nova pàgina " diff --git a/base_view_inheritance_extension/i18n/de.po b/base_view_inheritance_extension/i18n/de.po new file mode 100644 index 000000000..abe9bb05a --- /dev/null +++ b/base_view_inheritance_extension/i18n/de.po @@ -0,0 +1,37 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Niki Waibel , 2017 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-12-01 02:10+0000\n" +"PO-Revision-Date: 2017-12-01 02:10+0000\n" +"Last-Translator: Niki Waibel , 2017\n" +"Language-Team: German (https://www.transifex.com/oca/teams/23907/de/)\n" +"Language: de\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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" diff --git a/base_view_inheritance_extension/i18n/es.po b/base_view_inheritance_extension/i18n/es.po new file mode 100644 index 000000000..2a199f81a --- /dev/null +++ b/base_view_inheritance_extension/i18n/es.po @@ -0,0 +1,37 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Pedro M. Baeza , 2017 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-12-01 02:10+0000\n" +"PO-Revision-Date: 2017-12-01 02:10+0000\n" +"Last-Translator: Pedro M. Baeza , 2017\n" +"Language-Team: Spanish (https://www.transifex.com/oca/teams/23907/es/)\n" +"Language: es\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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" diff --git a/base_view_inheritance_extension/i18n/es_AR.po b/base_view_inheritance_extension/i18n/es_AR.po new file mode 100644 index 000000000..09f379eca --- /dev/null +++ b/base_view_inheritance_extension/i18n/es_AR.po @@ -0,0 +1,32 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2022-09-04 06:07+0000\n" +"Last-Translator: Ignacio Buioli \n" +"Language-Team: none\n" +"Language: es_AR\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" +"X-Generator: Weblate 4.3.2\n" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "Contacto desde" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "Números de teléfono" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "Vista" diff --git a/base_view_inheritance_extension/i18n/hr.po b/base_view_inheritance_extension/i18n/hr.po new file mode 100644 index 000000000..400960958 --- /dev/null +++ b/base_view_inheritance_extension/i18n/hr.po @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Bole , 2018 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-03-02 18:40+0000\n" +"PO-Revision-Date: 2018-03-02 18:40+0000\n" +"Last-Translator: Bole , 2018\n" +"Language-Team: Croatian (https://www.transifex.com/oca/teams/23907/hr/)\n" +"Language: hr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n" +"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "Forma partnera" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "Brojevi telefona" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" diff --git a/base_view_inheritance_extension/i18n/it.po b/base_view_inheritance_extension/i18n/it.po new file mode 100644 index 000000000..79eb8743d --- /dev/null +++ b/base_view_inheritance_extension/i18n/it.po @@ -0,0 +1,37 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Paolo Valier , 2018 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-01-06 02:25+0000\n" +"PO-Revision-Date: 2018-01-06 02:25+0000\n" +"Last-Translator: Paolo Valier , 2018\n" +"Language-Team: Italian (https://www.transifex.com/oca/teams/23907/it/)\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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "Form Partner" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "Numeri di telefono" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" diff --git a/base_view_inheritance_extension/i18n/sl.po b/base_view_inheritance_extension/i18n/sl.po new file mode 100644 index 000000000..20034797a --- /dev/null +++ b/base_view_inheritance_extension/i18n/sl.po @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Matjaž Mozetič , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-12-29 03:39+0000\n" +"PO-Revision-Date: 2016-12-29 03:39+0000\n" +"Last-Translator: Matjaž Mozetič , 2016\n" +"Language-Team: Slovenian (https://www.transifex.com/oca/teams/23907/sl/)\n" +"Language: sl\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n" +"%100==4 ? 2 : 3);\n" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "Partnerjev obrazec" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "A new page" +#~ msgstr "Nova stran" diff --git a/base_view_inheritance_extension/i18n/tr.po b/base_view_inheritance_extension/i18n/tr.po new file mode 100644 index 000000000..66aaa0e72 --- /dev/null +++ b/base_view_inheritance_extension/i18n/tr.po @@ -0,0 +1,40 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_view_inheritance_extension +# +# Translators: +# Ahmet Altinisik , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-12-29 03:39+0000\n" +"PO-Revision-Date: 2016-12-29 03:39+0000\n" +"Last-Translator: Ahmet Altinisik , 2016\n" +"Language-Team: Turkish (https://www.transifex.com/oca/teams/23907/tr/)\n" +"Language: tr\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" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Partner form" +msgstr "İş ortağı formu" + +#. module: base_view_inheritance_extension +#: model_terms:ir.ui.view,arch_db:base_view_inheritance_extension.view_partner_simple_form +msgid "Phone numbers" +msgstr "" + +#. module: base_view_inheritance_extension +#: model:ir.model,name:base_view_inheritance_extension.model_ir_ui_view +msgid "View" +msgstr "" + +#~ msgid "ir.ui.view" +#~ msgstr "ir.ui.view" + +#~ msgid "A new page" +#~ msgstr "Yeni bir sayfa" diff --git a/base_view_inheritance_extension/models/__init__.py b/base_view_inheritance_extension/models/__init__.py new file mode 100644 index 000000000..81f52e3bf --- /dev/null +++ b/base_view_inheritance_extension/models/__init__.py @@ -0,0 +1 @@ +from . import ir_ui_view diff --git a/base_view_inheritance_extension/models/ir_ui_view.py b/base_view_inheritance_extension/models/ir_ui_view.py new file mode 100644 index 000000000..e6ee174f2 --- /dev/null +++ b/base_view_inheritance_extension/models/ir_ui_view.py @@ -0,0 +1,130 @@ +# Copyright 2016 Therp BV +# Copyright 2018 Tecnativa - Sergio Teruel +# Copyright 2021 Camptocamp SA (https://www.camptocamp.com). +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +import ast + +import astor +from lxml import etree + +from odoo import api, models + + +def ast_dict_update(source, update): + """Perform a dict `update` on an ast.Dict + + Behaves similar to :meth:`dict.update`, but on ast.Dict instead. + Only compares string-like ast.Dict keys (ast.Str or ast.Constant). + + :returns: The updated ast.Dict + :rtype: ast.Dict + """ + if not isinstance(source, ast.Dict): + raise TypeError("`source` must be an AST dict") + if not isinstance(update, ast.Dict): + raise TypeError("`update` must be an AST dict") + + def ast_key_eq(k1, k2): + # python < 3.8 uses ast.Str; python >= 3.8 uses ast.Constant + if type(k1) != type(k2): + return False + elif isinstance(k1, ast.Str): + return k1.s == k2.s + elif isinstance(k1, ast.Constant): + return k1.value == k2.value + + toadd_uidx = [] + for uidx, ukey in enumerate(update.keys): + found = False + for sidx, skey in enumerate(source.keys): + if ast_key_eq(ukey, skey): + source.values[sidx] = update.values[uidx] + found = True + break + if not found: + toadd_uidx.append(uidx) + for uidx in toadd_uidx: + source.keys.append(update.keys[uidx]) + source.values.append(update.values[uidx]) + return source + + +class IrUiView(models.Model): + _inherit = "ir.ui.view" + + @api.model + def apply_inheritance_specs(self, source, specs_tree, pre_locate=lambda s: True): + for specs, handled_by in self._iter_inheritance_specs(specs_tree): + pre_locate(specs) + source = handled_by(source, specs) + return source + + @api.model + def _iter_inheritance_specs(self, spec): + if spec.tag == "data": + for child in spec: + for node, handler in self._iter_inheritance_specs(child): + yield node, handler + return + if spec.get("position") == "attributes": + if all(not c.get("operation") for c in spec): + yield spec, self._get_inheritance_handler(spec) + return + for child in spec: + node = etree.Element(spec.tag, **spec.attrib) + node.insert(0, child) + yield node, self._get_inheritance_handler_attributes(child) + return + yield spec, self._get_inheritance_handler(spec) + + @api.model + def _get_inheritance_handler(self, node): + handler = super().apply_inheritance_specs + if hasattr(self, "inheritance_handler_%s" % node.tag): + handler = getattr(self, "inheritance_handler_%s" % node.tag) + return handler + + @api.model + def _get_inheritance_handler_attributes(self, node): + handler = super().apply_inheritance_specs + if hasattr(self, "inheritance_handler_attributes_%s" % node.get("operation")): + handler = getattr( + self, "inheritance_handler_attributes_%s" % node.get("operation") + ) + return handler + + @api.model + def inheritance_handler_attributes_update(self, source, specs): + """Implement dict `update` operation on the attribute node. + + .. code-block:: xml + + + + { + "key": "value", + } + + + """ + node = self.locate_node(source, specs) + for spec in specs: + attr_name = spec.get("name") + # Parse ast from both node and spec + node_attr = (node.get(attr_name) or "{}").strip() + source_ast = ast.parse(node_attr, mode="eval").body + update_ast = ast.parse(spec.text.strip(), mode="eval").body + if not isinstance(source_ast, ast.Dict): + raise TypeError(f"Attribute `{attr_name}` is not a dict") + if not isinstance(update_ast, ast.Dict): + raise TypeError(f"Operation for attribute `{attr_name}` is not a dict") + # Update node ast dict + source_ast = ast_dict_update(source_ast, update_ast) + # Dump the ast back to source + # TODO: once odoo requires python >= 3.9; use `ast.unparse` instead + node.attrib[attr_name] = astor.to_source( + source_ast, + pretty_source=lambda s: "".join(s).strip(), + ) + return source diff --git a/base_view_inheritance_extension/readme/CONTRIBUTORS.rst b/base_view_inheritance_extension/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..be9defb1d --- /dev/null +++ b/base_view_inheritance_extension/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Holger Brunn +* Ronald Portier +* Sergio Teruel +* Iván Todorovich diff --git a/base_view_inheritance_extension/readme/DESCRIPTION.rst b/base_view_inheritance_extension/readme/DESCRIPTION.rst new file mode 100644 index 000000000..0536b6f26 --- /dev/null +++ b/base_view_inheritance_extension/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module was written to make it simple to add custom operators for view +inheritance. diff --git a/base_view_inheritance_extension/readme/ROADMAP.rst b/base_view_inheritance_extension/readme/ROADMAP.rst new file mode 100644 index 000000000..ad27c61b8 --- /dev/null +++ b/base_view_inheritance_extension/readme/ROADMAP.rst @@ -0,0 +1 @@ +* Support an ``eval`` attribute for our new node types. diff --git a/base_view_inheritance_extension/readme/USAGE.rst b/base_view_inheritance_extension/readme/USAGE.rst new file mode 100644 index 000000000..66ae50d75 --- /dev/null +++ b/base_view_inheritance_extension/readme/USAGE.rst @@ -0,0 +1,16 @@ +**Change a python dictionary (context for example)** + + +.. code-block:: xml + + + + { + "key": "value", + } + + + + +Note that views are subject to evaluation of xmlids anyways, so if you need +to refer to some xmlid, say ``%(xmlid)s``. diff --git a/base_view_inheritance_extension/static/description/icon.png b/base_view_inheritance_extension/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/base_view_inheritance_extension/static/description/icon.png differ diff --git a/base_view_inheritance_extension/static/description/index.html b/base_view_inheritance_extension/static/description/index.html new file mode 100644 index 000000000..0eafa4295 --- /dev/null +++ b/base_view_inheritance_extension/static/description/index.html @@ -0,0 +1,446 @@ + + + + + + +Extended view inheritance + + + +
+

Extended view inheritance

+ + +

Mature License: LGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

+

This module was written to make it simple to add custom operators for view +inheritance.

+

Table of contents

+ +
+

Usage

+

Change a python dictionary (context for example)

+
+<field position="attributes">
+    <attribute name="context" operation="update">
+        {
+            "key": "value",
+        }
+    </attribute>
+</field>
+
+

Note that views are subject to evaluation of xmlids anyways, so if you need +to refer to some xmlid, say %(xmlid)s.

+
+
+

Known issues / Roadmap

+
    +
  • Support an eval attribute for our new node types.
  • +
+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Therp BV
  • +
+
+
+

Contributors

+ +
+
+

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/server-tools project on GitHub.

+

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

+
+
+
+ + diff --git a/base_view_inheritance_extension/tests/__init__.py b/base_view_inheritance_extension/tests/__init__.py new file mode 100644 index 000000000..261fddfad --- /dev/null +++ b/base_view_inheritance_extension/tests/__init__.py @@ -0,0 +1 @@ +from . import test_base_view_inheritance_extension diff --git a/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py b/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py new file mode 100644 index 000000000..baf749760 --- /dev/null +++ b/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py @@ -0,0 +1,242 @@ +# Copyright 2016 Therp BV +# Copyright 2021 Camptocamp SA (https://www.camptocamp.com). +# @author Iván Todorovich +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from lxml import etree + +from odoo.tests.common import TransactionCase + + +class TestBaseViewInheritanceExtension(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.maxDiff = None + + def test_base_view_inheritance_extension(self): + view_id = self.env.ref("base.view_partner_simple_form").id + arch, view = self.env["res.partner"]._get_view(view_id=view_id) + # Verify normal attributes work + self.assertEqual(arch.xpath("//form")[0].get("string"), "Partner form") + # Verify our extra context key worked + self.assertTrue( + "'default_email': 'info@odoo-community.org'" + in arch.xpath('//field[@name="parent_id"]')[0].get("context") + ) + self.assertTrue( + "'default_company_id': allowed_company_ids[0]" + in arch.xpath('//field[@name="parent_id"]')[0].get("context") + ) + + def test_update_context_default(self): + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + {"default_company_id": company_id} + + + """ + ) + res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + self.assertEqual( + res.xpath('//field[@name="account_move_id"]')[0].attrib["context"], + "{'default_journal_id': journal_id, 'default_company_id': company_id}", + ) + + def test_update_context_complex(self): + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + { + "default_product_id": product_id, + "default_cost_center_id": ( + context.get("handle_mrp_cost") and cost_center_id or False + ), + } + + + """ + ) + res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + expected_items = [ + "'default_type': context.get('default_type')", + "'journal_id': journal_id", + "'default_partner_id': commercial_partner_id", + ( + "'default_currency_id': " + "currency_id != company_currency_id and currency_id or False" + ), + "'default_name': 'The company name'", + "'default_product_id': product_id", + ( + "'default_cost_center_id': " + "context.get('handle_mrp_cost') and cost_center_id or False" + ), + ] + self.assertEqual( + res.xpath('//field[@name="invoice_line_ids"]')[0].attrib["context"], + "{%s}" % ", ".join(expected_items), + ) + + def test_update_attrs_new_key(self): + """Test that we can add new keys to an existing dict""" + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + { + "required": [("state", "!=", "draft")], + } + + + """ + ) + res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + self.assertEqual( + res.xpath('//field[@name="ref"]')[0].attrib["attrs"], + "{'invisible': [('state', '=', 'draft')], " + "'required': [('state', '!=', 'draft')]}", + ) + + def test_update_attrs_replace(self): + """Test that we can replace an existing dict key""" + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + { + "required": [('state', '!=', 'draft')], + } + + + """ + ) + res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + self.assertEqual( + res.xpath('//field[@name="ref"]')[0].attrib["attrs"], + "{'invisible': [('state', '=', 'draft')], " + "'required': [('state', '!=', 'draft')]}", + ) + + def test_update_empty_source_dict(self): + """Test that we can add new keys by creating the dict if it's missing""" + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + { + "required": [('state', '!=', 'draft')], + } + + + """ + ) + res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + self.assertEqual( + res.xpath('//field[@name="ref"]')[0].attrib["attrs"], + "{'required': [('state', '!=', 'draft')]}", + ) + + def test_update_operation_not_a_dict(self): + """We should get an error if we try to update a dict with a non-dict spec""" + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + ["not", "a", "dict"] + + + """ + ) + with self.assertRaisesRegex( + TypeError, "Operation for attribute `attrs` is not a dict" + ): + self.env["ir.ui.view"].apply_inheritance_specs(source, specs) + + def test_update_source_not_a_dict(self): + """We should get an error if we try to update a non-dict attribute""" + source = etree.fromstring( + """ +
+ + + """ + ) + specs = etree.fromstring( + """ + + + { + "required": [('state', '!=', 'draft')], + } + + + """ + ) + with self.assertRaisesRegex(TypeError, "Attribute `domain` is not a dict"): + self.env["ir.ui.view"].apply_inheritance_specs(source, specs) diff --git a/requirements.txt b/requirements.txt index 7a9599183..fdcddc4ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ # generated from manifests external_dependencies +astor dataclasses mako odoorpc diff --git a/setup/base_view_inheritance_extension/odoo/addons/base_view_inheritance_extension b/setup/base_view_inheritance_extension/odoo/addons/base_view_inheritance_extension new file mode 120000 index 000000000..49df2da14 --- /dev/null +++ b/setup/base_view_inheritance_extension/odoo/addons/base_view_inheritance_extension @@ -0,0 +1 @@ +../../../../base_view_inheritance_extension \ No newline at end of file diff --git a/setup/base_view_inheritance_extension/setup.py b/setup/base_view_inheritance_extension/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/base_view_inheritance_extension/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)