Skip to content

Commit

Permalink
Merge pull request #1453 from OCA/16.0
Browse files Browse the repository at this point in the history
Syncing from upstream OCA/server-tools (16.0)
  • Loading branch information
bt-admin authored Feb 8, 2024
2 parents 78446ab + a3abec1 commit 33188dc
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ addon | version | maintainers | summary
[base_sparse_field_list_support](base_sparse_field_list_support/) | 16.0.1.0.0 | | add list support to convert_to_cache()
[base_technical_user](base_technical_user/) | 16.0.1.0.0 | | Add a technical user parameter on the company
[base_time_window](base_time_window/) | 16.0.1.0.0 | | Base model to handle time windows
[base_view_inheritance_extension](base_view_inheritance_extension/) | 16.0.1.1.0 | | Adds more operators for view inheritance
[base_view_inheritance_extension](base_view_inheritance_extension/) | 16.0.1.2.0 | | Adds more operators for view inheritance
[cron_daylight_saving_time_resistant](cron_daylight_saving_time_resistant/) | 16.0.1.0.0 | [![florian-dacosta](https://github.com/florian-dacosta.png?size=30px)](https://github.com/florian-dacosta) | Run cron on fixed hours
[database_cleanup](database_cleanup/) | 16.0.1.0.1 | | Database cleanup
[dbfilter_from_header](dbfilter_from_header/) | 16.0.1.0.1 | | Filter databases with HTTP headers
Expand Down
12 changes: 11 additions & 1 deletion base_view_inheritance_extension/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Extended view inheritance
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:56c707d911c93aace4dc1593e4c0f7de5816cdf8883d581a9cfdf7d500b2de66
!! source digest: sha256:c4e765a621083d8bc3499b930418a7e00123f7228e93615dd74fe42b1b19af26
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Mature-brightgreen.png
Expand Down Expand Up @@ -73,6 +73,15 @@ to refer to some xmlid, say ``%(xmlid)s``.
$domain_to_add
</attribute>
**Add domain with AND/OR join operator (AND if missed) for key in attrs**

.. code-block:: xml
<attribute name="$attribute" operation="attrs_domain_add"
key="$attrs_key" join_operator="OR">
$domain_to_add_to_attrs_key
</attribute>
Known issues / Roadmap
======================

Expand Down Expand Up @@ -107,6 +116,7 @@ Contributors
* Carlos Dauden

* Iván Todorovich <[email protected]>
* Carlos Serra-Toro <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion base_view_inheritance_extension/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
{
"name": "Extended view inheritance",
"version": "16.0.1.1.0",
"version": "16.0.1.2.0",
"development_status": "Mature",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "LGPL-3",
Expand Down
36 changes: 36 additions & 0 deletions base_view_inheritance_extension/models/ir_ui_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,42 @@ def inheritance_handler_attributes_domain_add(self, source, specs):
node.attrib[attribute_name] = new_value
return source

@api.model
def inheritance_handler_attributes_attrs_domain_add(self, source, specs):
"""Implement attrs_domain_add
<attribute name="$attribute" operation="attrs_domain_add"
key="$attrs_key" join_operator="OR">
$domain_to_add_to_attrs_key
</attribute>
"""
node = self.locate_node(source, specs)
for attribute_node in specs:
attribute_name = attribute_node.get("name")
key = attribute_node.get("key")
join_operator = attribute_node.get("join_operator") or "AND"
old_value = node.get(attribute_name) or ""
if old_value:
old_value = ast.literal_eval(
self.var2str_domain_text(old_value.strip())
)
old_domain_attrs = old_value.get(key)
new_domain = ast.literal_eval(
self.var2str_domain_text(attribute_node.text.strip())
)
if join_operator == "OR":
new_value = expression.OR([old_domain_attrs, new_domain])
else:
new_value = expression.AND([old_domain_attrs, new_domain])
old_value[key] = new_value
new_value = self.str2var_domain_text(str(old_value))
else:
# We must ensure that the domain definition has not line breaks because
# in update mode the domain cause an invalid syntax error
new_value = "{'%s': %s}" % (key, attribute_node.text.strip())
node.attrib[attribute_name] = new_value
return source

@api.model
def var2str_domain_text(self, domain_str):
"""Replaces var names with str names to allow eval without defined vars"""
Expand Down
1 change: 1 addition & 0 deletions base_view_inheritance_extension/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
* Carlos Dauden

* Iván Todorovich <[email protected]>
* Carlos Serra-Toro <[email protected]>
9 changes: 9 additions & 0 deletions base_view_inheritance_extension/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ to refer to some xmlid, say ``%(xmlid)s``.
condition="$field_condition" join_operator="OR">
$domain_to_add
</attribute>
**Add domain with AND/OR join operator (AND if missed) for key in attrs**

.. code-block:: xml
<attribute name="$attribute" operation="attrs_domain_add"
key="$attrs_key" join_operator="OR">
$domain_to_add_to_attrs_key
</attribute>
11 changes: 9 additions & 2 deletions base_view_inheritance_extension/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -367,7 +366,7 @@ <h1 class="title">Extended view inheritance</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:56c707d911c93aace4dc1593e4c0f7de5816cdf8883d581a9cfdf7d500b2de66
!! source digest: sha256:c4e765a621083d8bc3499b930418a7e00123f7228e93615dd74fe42b1b19af26
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Mature" src="https://img.shields.io/badge/maturity-Mature-brightgreen.png" /></a> <a class="reference external image-reference" 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 image-reference" href="https://github.com/OCA/server-tools/tree/16.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 image-reference" href="https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-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 image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module was written to make it simple to add custom operators for view
Expand Down Expand Up @@ -413,6 +412,13 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
</span>$domain_to_add<span class="w">
</span><span class="nt">&lt;/attribute&gt;</span>
</pre>
<p><strong>Add domain with AND/OR join operator (AND if missed) for key in attrs</strong></p>
<pre class="code xml literal-block">
<span class="nt">&lt;attribute</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;$attribute&quot;</span><span class="w"> </span><span class="na">operation=</span><span class="s">&quot;attrs_domain_add&quot;</span><span class="w">
</span><span class="na">key=</span><span class="s">&quot;$attrs_key&quot;</span><span class="w"> </span><span class="na">join_operator=</span><span class="s">&quot;OR&quot;</span><span class="nt">&gt;</span><span class="w">
</span>$domain_to_add_to_attrs_key<span class="w">
</span><span class="nt">&lt;/attribute&gt;</span>
</pre>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
Expand Down Expand Up @@ -447,6 +453,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
</ul>
</li>
<li>Iván Todorovich &lt;<a class="reference external" href="mailto:ivan.todorovich&#64;camptocamp.com">ivan.todorovich&#64;camptocamp.com</a>&gt;</li>
<li>Carlos Serra-Toro &lt;<a class="reference external" href="mailto:carlos.serra&#64;braintec.com">carlos.serra&#64;braintec.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,70 @@ def test_update_source_not_a_dict(self):
)
with self.assertRaisesRegex(TypeError, "Attribute `domain` is not a dict"):
self.env["ir.ui.view"].apply_inheritance_specs(source, specs)

def test_attrs_domain_add_join_operator_or(self):
"""Test that we can add an OR domain to an existing attrs key."""
self._test_attrs_domain_add(join_operator="OR")

def test_attrs_domain_add_join_operator_and(self):
"""Test that we can add an AND domain to an existing attrs key."""
self._test_attrs_domain_add(join_operator="AND")

def _test_attrs_domain_add(self, join_operator):
"""Test that we can add a domain to an existing attrs domain key."""
source = etree.fromstring(
"""
<form>
<field
name="ref"
attrs="{
'invisible': [('state', '=', 'draft')],
'required': [('state', '=', False)],
}"
/>
</form>
"""
)
specs = etree.fromstring(
"""
<field name="ref" position="attributes">
<attribute name="attrs" operation="attrs_domain_add"
key="required" join_operator="%s">
[('state', '!=', 'draft')]
</attribute>
</field>
"""
% (join_operator,)
)
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': ['%s', ('state', '=', False), ('state', '!=', 'draft')]}"
% ("|" if join_operator == "OR" else "&"),
)

def test_attrs_domain_add_no_attrs(self):
"""Test attrs_domain_add if there is no attrs: attrs is created."""
source = etree.fromstring(
"""
<form>
<field name="ref"/>
</form>
"""
)
specs = etree.fromstring(
"""
<field name="ref" position="attributes">
<attribute name="attrs" operation="attrs_domain_add"
key="required" join_operator="OR">
[('state', '!=', 'draft')]
</attribute>
</field>
"""
)
res = self.env["ir.ui.view"].apply_inheritance_specs(source, specs)
self.assertEqual(
res.xpath('//field[@name="ref"]')[0].attrib["attrs"],
"{'required': [('state', '!=', 'draft')]}",
)

0 comments on commit 33188dc

Please sign in to comment.