[MIG] base_view_inheritance_extension: Migration to v13.0

pull/2494/head
Sergio Teruel 2020-03-03 19:35:49 +01:00 committed by Enric Tobella
parent 45542a0071
commit d1a9c492f7
6 changed files with 106 additions and 41 deletions

View File

@ -14,13 +14,13 @@ Extended view inheritance
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3 :alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/12.0/base_view_inheritance_extension :target: https://github.com/OCA/server-tools/tree/13.0/base_view_inheritance_extension
:alt: OCA/server-tools :alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_view_inheritance_extension :target: https://translation.odoo-community.org/projects/server-tools-13-0/server-tools-13-0-base_view_inheritance_extension
:alt: Translate me on Weblate :alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png .. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/12.0 :target: https://runbot.odoo-community.org/runbot/149/13.0
:alt: Try me on Runbot :alt: Try me on Runbot
|badge1| |badge2| |badge3| |badge4| |badge5| |badge1| |badge2| |badge3| |badge4| |badge5|
@ -68,8 +68,6 @@ to refer to some xmlid, say ``%(xmlid)s``.
This feature is now native, cf the `official Odoo documentation <https://www.odoo.com/documentation/12.0/reference/views.html#inheritance-specs>`_. This feature is now native, cf the `official Odoo documentation <https://www.odoo.com/documentation/12.0/reference/views.html#inheritance-specs>`_.
Known issues / Roadmap Known issues / Roadmap
====================== ======================
@ -83,7 +81,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_. Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported. 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 If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_view_inheritance_extension%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. `feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_view_inheritance_extension%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Do not contact contributors directly about support or help with technical issues. Do not contact contributors directly about support or help with technical issues.
@ -115,6 +113,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and mission is to support the collaborative development of Odoo features and
promote its widespread use. promote its widespread use.
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/12.0/base_view_inheritance_extension>`_ project on GitHub. This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/13.0/base_view_inheritance_extension>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

View File

@ -3,7 +3,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
{ {
"name": "Extended view inheritance", "name": "Extended view inheritance",
"version": "12.0.1.0.0", "version": "13.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)", "author": "Therp BV,Odoo Community Association (OCA)",
"license": "LGPL-3", "license": "LGPL-3",
"category": "Hidden/Dependency", "category": "Hidden/Dependency",

View File

@ -1,25 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="view_partner_simple_form" model="ir.ui.view"> <record id="view_partner_simple_form" model="ir.ui.view">
<field name="model">res.partner</field> <field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_simple_form" /> <field name="inherit_id" ref="base.view_partner_simple_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="." position="attributes"> <xpath expr="." position="attributes">
<attribute name="string">Partner form</attribute> <attribute name="string">Partner form</attribute>
</xpath> </xpath>
<field name="parent_id" position="attributes"> <field name="parent_id" position="attributes">
<attribute name="context" operation="python_dict" key="default_name">'The company name'</attribute> <attribute
<attribute name="context" operation="python_dict" key="default_company_id">context.get('company_id', context.get('company'))</attribute> name="context"
</field> operation="python_dict"
<!-- without operations, the standard handler should be called /--> key="default_name"
<field name="parent_id" position="attributes"> >'The company name'</attribute>
<attribute name="name">parent_id</attribute> <attribute
</field> name="context"
<form position="inside"> operation="python_dict"
<notebook> key="default_company_id"
<page string="Phone numbers" name="phone_book" /> >context.get('company_id', context.get('company'))</attribute>
</notebook>
</form>
</field> </field>
</record> <!-- without operations, the standard handler should be called /-->
<field name="parent_id" position="attributes">
<attribute name="name">parent_id</attribute>
</field>
<form position="inside">
<notebook>
<page string="Phone numbers" name="phone_book" />
</notebook>
</form>
</field>
</record>
</odoo> </odoo>

View File

@ -2,9 +2,9 @@
# Copyright 2018 Tecnativa - Sergio Teruel # Copyright 2018 Tecnativa - Sergio Teruel
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from lxml import etree from lxml import etree
from yaml import safe_load
from odoo import api, models, tools from odoo import api, models, tools
from odoo.tools.safe_eval import safe_eval
class UnquoteObject(str): class UnquoteObject(str):
@ -78,6 +78,24 @@ class IrUiView(models.Model):
) )
return handler return handler
def _is_variable(self, value):
return not ("'" in value or '"' in value) and True or False
def _list_variables(self, str_dict):
"""
Store non literal dictionary values into a list to post-process
operations.
"""
variables = []
items = str_dict.replace("{", "").replace("}", "").split(",")
for item in items:
key_value = item.split(":")
if len(key_value) == 2:
value = key_value[1]
if self._is_variable(value):
variables.append(value.strip())
return variables
@api.model @api.model
def inheritance_handler_attributes_python_dict(self, source, specs, inherit_id): def inheritance_handler_attributes_python_dict(self, source, specs, inherit_id):
"""Implement """Implement
@ -88,13 +106,15 @@ class IrUiView(models.Model):
</$node>""" </$node>"""
node = self.locate_node(source, specs) node = self.locate_node(source, specs)
for attribute_node in specs: for attribute_node in specs:
python_dict = safe_eval( str_dict = node.get(attribute_node.get("name")) or "{}"
node.get(attribute_node.get("name")) or "{}", variables = self._list_variables(str_dict)
UnquoteEvalObjectContext(), if self._is_variable(attribute_node.text):
nocopy=True, variables.append(attribute_node.text)
) my_dict = safe_load(str_dict)
python_dict[attribute_node.get("key")] = UnquoteObject(attribute_node.text) my_dict[attribute_node.get("key")] = attribute_node.text
node.attrib[attribute_node.get("name")] = str(python_dict) for k, v in my_dict.items():
my_dict[k] = UnquoteObject(v) if v in variables else v
node.attrib[attribute_node.get("name")] = str(my_dict)
return source return source
@api.model @api.model

View File

@ -367,7 +367,7 @@ ul.auto-toc {
!! This file is generated by oca-gen-addon-readme !! !! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !! !! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_view_inheritance_extension"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_view_inheritance_extension"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/149/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p> <p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-tools/tree/13.0/base_view_inheritance_extension"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-tools-13-0/server-tools-13-0-base_view_inheritance_extension"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/149/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module was written to make it simple to add custom operators for view <p>This module was written to make it simple to add custom operators for view
inheritance.</p> inheritance.</p>
<p><strong>Table of contents</strong></p> <p><strong>Table of contents</strong></p>
@ -422,7 +422,7 @@ to refer to some xmlid, say <tt class="docutils literal">%(xmlid)s</tt>.</p>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>. <p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported. 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 If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20base_view_inheritance_extension%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p> <a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20base_view_inheritance_extension%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p> <p>Do not contact contributors directly about support or help with technical issues.</p>
</div> </div>
<div class="section" id="credits"> <div class="section" id="credits">
@ -448,7 +448,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose <p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and mission is to support the collaborative development of Odoo features and
promote its widespread use.</p> promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_view_inheritance_extension">OCA/server-tools</a> project on GitHub.</p> <p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-tools/tree/13.0/base_view_inheritance_extension">OCA/server-tools</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p> <p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div> </div>
</div> </div>

View File

@ -90,3 +90,42 @@ class TestBaseViewInheritanceExtension(TransactionCase):
) )
button_node = modified_source.xpath('//button[@name="test"]')[0] button_node = modified_source.xpath('//button[@name="test"]')[0]
self.assertEqual(button_node.attrib["states"], "draft,valid,paid") self.assertEqual(button_node.attrib["states"], "draft,valid,paid")
def test_python_dict_inheritance(self):
view_model = self.env["ir.ui.view"]
inherit_id = self.env.ref("base.view_partner_form").id
source = etree.fromstring(
"""<form>
<field name="invoice_line_ids"
context="{
'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',
}"/>
</form>"""
)
specs = etree.fromstring(
"""\
<field name="invoice_line_ids" position="attributes">
<attribute name="context" operation="python_dict"
key="my_key">my_value</attribute>
<attribute name="context" operation="python_dict"
key="my_key2">'my name'</attribute>
<attribute name="context" operation="python_dict"
key="default_cost_center_id">cost_center_id</attribute>
</field>
"""
)
modified_source = view_model.inheritance_handler_attributes_python_dict(
source, specs, inherit_id
)
field_node = modified_source.xpath('//field[@name="invoice_line_ids"]')[0]
self.assertTrue(
"currency_id != company_currency_id and currency_id or False"
in field_node.attrib["context"]
)
self.assertTrue("my_value" in field_node.attrib["context"])
self.assertFalse("'cost_center_id'" in field_node.attrib["context"])