Skip to content

Commit

Permalink
Merge PR #2941 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed May 14, 2024
2 parents 84caeb7 + 1e40f17 commit ac4dcc1
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
23 changes: 22 additions & 1 deletion base_view_inheritance_extension/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ Deprecated. This feature is now native, use `<attribute name="attrname" separato

This feature is now native, cf the `official Odoo documentation <https://www.odoo.com/documentation/14.0/developer/reference/addons/views.html#inheritance-specs>`_.

**Add text after and/or before than original**

.. code-block:: xml
<attribute name="$attribute" operation="text_add">
$text_before {old_value} $text_after
</attribute>
**Add domain with AND/OR join operator (AND if missed) allowing conditional changes**

.. code-block:: xml
<attribute name="$attribute" operation="domain_add"
condition="$field_condition" join_operator="OR">
$domain_to_add
</attribute>
Known issues / Roadmap
======================

Expand Down Expand Up @@ -92,7 +109,11 @@ Contributors

* Holger Brunn <hbrunn@therp.nl>
* Ronald Portier <rportier@therp.nl>
* Sergio Teruel <sergio.teruel@tecnativa.com>
* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
* Carlos Dauden

* Iván Todorovich <ivan.todorovich@camptocamp.com>

Maintainers
Expand Down
77 changes: 77 additions & 0 deletions base_view_inheritance_extension/models/ir_ui_view.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright 2016 Therp BV <https://therp.nl>
# Copyright 2018 Tecnativa - Sergio Teruel
# Copyright 2021 Camptocamp SA (https://www.camptocamp.com).
# Copyright 2023 Tecnativa - Carlos Dauden
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
import ast
import logging
import re

try:
import astor
Expand All @@ -14,6 +16,7 @@
from lxml import etree

from odoo import api, models
from odoo.osv import expression


class IrUiView(models.Model):
Expand Down Expand Up @@ -133,3 +136,77 @@ def inheritance_handler_attributes_list_remove(self, source, specs):
new_values = [x for x in old_values if x not in remove_values]
node.attrib[attribute_name] = ",".join([_f for _f in new_values if _f])
return source

@api.model
def inheritance_handler_attributes_text_add(self, source, specs):
"""Implement
<$node position="attributes">
<attribute name="$attribute" operation="text_add">
$text_before {old_value} $text_after
</attribute>
</$node>"""
node = self.locate_node(source, specs)
for attribute_node in specs:
attribute_name = attribute_node.get("name")
old_value = node.get(attribute_name) or ""
node.attrib[attribute_name] = attribute_node.text.format(
old_value=old_value
)
return source

@api.model
def inheritance_handler_attributes_domain_add(self, source, specs):
"""Implement
<$node position="attributes">
<attribute name="$attribute" operation="domain_add"
condition="$field_condition" join_operator="OR">
$domain_to_add
</attribute>
</$node>"""
node = self.locate_node(source, specs)
for attribute_node in specs:
attribute_name = attribute_node.get("name")
condition = attribute_node.get("condition")
join_operator = attribute_node.get("join_operator") or "AND"
old_value = node.get(attribute_name) or ""
if old_value:
old_domain = ast.literal_eval(
self.var2str_domain_text(old_value.strip())
)
new_domain = ast.literal_eval(
self.var2str_domain_text(attribute_node.text.strip())
)
if join_operator == "OR":
new_value = str(expression.OR([old_domain, new_domain]))
else:
new_value = str(expression.AND([old_domain, new_domain]))
new_value = self.str2var_domain_text(new_value)
else:
new_value = attribute_node.text
if condition:
new_value = "{condition} and {new_value} or {old_value}".format(
condition=condition,
new_value=new_value,
old_value=old_value,
)
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"""
# Replace fields in 2 steps because 1 step returns "parent_sufix"."var_sufix"
regex_parent = re.compile(r"parent\.(\b\w+\b)")
domain_str = re.sub(
regex_parent, r"'parent.\1_is_a_var_to_replace'", domain_str
)
regex = re.compile(r"(?<!\')(\b\w+\b)(?!\')")
return re.sub(regex, r"'\1_is_a_var_to_replace'", domain_str)

@api.model
def str2var_domain_text(self, domain_str):
"""Revert var2str_domain_text cleaning apostrophes and suffix in vars"""
pattern = re.compile(r"'(parent\.[^']+)_is_a_var_to_replace'")
domain_str = pattern.sub(r"\1", domain_str)
pattern = re.compile(r"'([^']+)_is_a_var_to_replace'")
return pattern.sub(r"\1", domain_str)
6 changes: 5 additions & 1 deletion base_view_inheritance_extension/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
* Holger Brunn <hbrunn@therp.nl>
* Ronald Portier <rportier@therp.nl>
* Sergio Teruel <sergio.teruel@tecnativa.com>
* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
* Carlos Dauden

* Iván Todorovich <ivan.todorovich@camptocamp.com>
17 changes: 17 additions & 0 deletions base_view_inheritance_extension/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ Deprecated. This feature is now native, use `<attribute name="attrname" separato
**Move an element in the view**

This feature is now native, cf the `official Odoo documentation <https://www.odoo.com/documentation/14.0/developer/reference/addons/views.html#inheritance-specs>`_.

**Add text after and/or before than original**

.. code-block:: xml
<attribute name="$attribute" operation="text_add">
$text_before {old_value} $text_after
</attribute>
**Add domain with AND/OR join operator (AND if missed) allowing conditional changes**

.. code-block:: xml
<attribute name="$attribute" operation="domain_add"
condition="$field_condition" join_operator="OR">
$domain_to_add
</attribute>
20 changes: 18 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 @@ -402,6 +401,19 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>Deprecated. This feature is now native, use <cite>&lt;attribute name=”attrname” separator=”,” remove=”something” /&gt;</cite>.</p>
<p><strong>Move an element in the view</strong></p>
<p>This feature is now native, cf the <a class="reference external" href="https://www.odoo.com/documentation/14.0/developer/reference/addons/views.html#inheritance-specs">official Odoo documentation</a>.</p>
<p><strong>Add text after and/or before than original</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;text_add&quot;</span><span class="nt">&gt;</span><span class="w">
</span>$text_before<span class="w"> </span>{old_value}<span class="w"> </span>$text_after<span class="w">
</span><span class="nt">&lt;/attribute&gt;</span>
</pre>
<p><strong>Add domain with AND/OR join operator (AND if missed) allowing conditional changes</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;domain_add&quot;</span><span class="w">
</span><span class="na">condition=</span><span class="s">&quot;$field_condition&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<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 @@ -431,7 +443,11 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Holger Brunn &lt;<a class="reference external" href="mailto:hbrunn&#64;therp.nl">hbrunn&#64;therp.nl</a>&gt;</li>
<li>Ronald Portier &lt;<a class="reference external" href="mailto:rportier&#64;therp.nl">rportier&#64;therp.nl</a>&gt;</li>
<li>Sergio Teruel &lt;<a class="reference external" href="mailto:sergio.teruel&#64;tecnativa.com">sergio.teruel&#64;tecnativa.com</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Sergio Teruel</li>
<li>Carlos Dauden</li>
</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>
</ul>
</div>
Expand Down

0 comments on commit ac4dcc1

Please sign in to comment.