Skip to content

Commit

Permalink
[FIX] util/fields : update field in context of custom search view
Browse files Browse the repository at this point in the history
This commit aims to make views compatible which are generated when a
search filter is created using studio, view looks like
<data>
  <xpath expr="xyz" position="after">
    <filter name="filter1"  context="{'group_by': 'renamed field'}"/>
    <filter name="filter2"  context="{'group_by': 'removed field'}"/>
  </xpath>
</data>

Currently after upgrade this view will be disabled because we are not updating
the field in this view.

After this fix

view:
<data>
  <xpath expr="xyz" position="after">
    <filter name="filter1"  context="{'group_by': 'New Name'}"/>
    <filter name="filter2"  context="{}"/>
  </xpath>
</data>
  • Loading branch information
thni-odoo committed Mar 15, 2024
1 parent 35b43aa commit 585940d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/util/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def make_index_name(table_name, column_name):
savepoint,
table_exists,
)
from .records import adapt_search_views
from .report import add_to_migration_reports, get_anchor_link_to_record

# python3 shims
Expand Down Expand Up @@ -175,6 +176,8 @@ def clean_context(context):
""",
[(fieldname, fieldname + " desc"), model, r"\y{}\y".format(fieldname)],
)
# clean filter's of custom search view
adapt_search_views(cr, fieldname, model, skip_inherit=skip_inherit)

def adapter(leaf, is_or, negated):
# replace by TRUE_LEAF, unless negated or in a OR operation but not negated
Expand Down Expand Up @@ -1061,6 +1064,8 @@ def adapt_dict(d):
adapt_domains(cr, model, old, new, adapter=domain_adapter, skip_inherit="*", force_adapt=True)
adapt_related(cr, model, old, new, skip_inherit="*")
adapt_depends(cr, model, old, new, skip_inherit="*")
# clean filter's of custom search view
adapt_search_views(cr, old, model, new, skip_inherit="*")

inherited_models = tuple(
inh.model for model in only_models for inh in for_each_inherit(cr, model, skip_inherit)
Expand Down
48 changes: 47 additions & 1 deletion src/util/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
from odoo import release
from odoo.tools.convert import xml_import
from odoo.tools.misc import file_open
from odoo.tools.safe_eval import safe_eval
from odoo.tools.translate import xml_translate
except ImportError:
from openerp import release
from openerp.tools.convert import xml_import
from openerp.tools.misc import file_open
from openerp.tools.safe_eval import safe_eval

from .const import NEARLYWARN
from .exceptions import MigrationError
from .helpers import _get_theme_models, _ir_values_value, _validate_model, model_of_table, table_of_model
from .indirect_references import indirect_references
from .inherit import direct_inherit_parents, for_each_inherit
from .misc import parse_version, version_gte
from .misc import SelfPrintEvalContext, parse_version, version_gte
from .orm import env, flush
from .pg import (
PGRegexp,
Expand Down Expand Up @@ -1338,3 +1340,47 @@ def remove_act_window_view_mode(cr, model, view_mode):
""",
[view_mode, model, view_mode, view_mode],
)


def adapt_search_views(cr, old, model, new=None, skip_inherit=()):
arch_db = (
get_value_or_en_translation(cr, "ir_ui_view", "arch_db")
if column_exists(cr, "ir_ui_view", "arch_db")
else "arch"
)
match_old = r"\y{}\y".format(re.escape(old))
cr.execute(
"""
SELECT id
FROM ir_ui_view
WHERE {} ~ %s
AND type = 'search'
AND model = %s
""".format(arch_db),
[match_old, model],
)
for view_id in cr.fetchall():
try:
with edit_view(cr, view_id=view_id, active=None) as view:
for elem in view.xpath("//filter[contains(@context, '{0}')]".format(old)):
context = elem.attrib["context"]
context = safe_eval(context or "{}", SelfPrintEvalContext(), nocopy=True)
if "group_by" in context and context["group_by"] == old:
if new:
context["group_by"] = new
else:
context.pop("group_by")
_logger.info(
"Field %s was removed during the upgrade, context has been updated from filter %s to avoid disabling of custom view",
old,
elem.attrib["name"],
)
elem.attrib["context"] = unicode(context)
except lxml.etree.XMLSyntaxError as e:
# Error faced while editing view
_logger.warning("Skipping custom search view adaptaiton for invalid view (id=%s):\n%s", view_id, e.msg)
continue

# down on inherits
for inh in for_each_inherit(cr, model, skip_inherit):
adapt_search_views(cr, old, inh.model, new=new, skip_inherit=skip_inherit)

0 comments on commit 585940d

Please sign in to comment.