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 Jan 3, 2024
1 parent 3869719 commit 0f22f5b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/util/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import warnings

import lxml
import psycopg2

try:
Expand All @@ -18,6 +19,12 @@
from openerp.tools.misc import mute_logger
from openerp.tools.safe_eval import safe_eval

try:
from contextlib import suppress
except ImportError:
# python2 code, use the openerp vendor
from openerp.tools.misc import ignore as suppress

try:
from odoo.tools.sql import make_index_name
except ImportError:
Expand Down Expand Up @@ -45,6 +52,7 @@ def make_index_name(table_name, column_name):
savepoint,
table_exists,
)
from .records import edit_view
from .report import add_to_migration_reports, get_anchor_link_to_record

# python3 shims
Expand All @@ -54,6 +62,10 @@ def make_index_name(table_name, column_name):
basestring = unicode = str


class _Skip(Exception):
pass


_logger = logging.getLogger(__name__)
IMD_FIELD_PATTERN = "field_%s__%s" if version_gte("saas~11.2") else "field_%s_%s"

Expand Down Expand Up @@ -153,6 +165,9 @@ def clean_context(context):
if changed:
add_to_migration_reports(("ir.filters", id_, name), "Filters/Dashboards")

# clean filter's of custom search view
update_custom_filter(cr, fieldname, model)

def adapter(leaf, is_or, negated):
# replace by TRUE_LEAF, unless negated or in a OR operation but not negated
if is_or ^ negated:
Expand Down Expand Up @@ -998,6 +1013,8 @@ def adapt_dict(d):
# skip all inherit, they will be handled by the resursive call
adapt_domains(cr, model, old, new, adapter=domain_adapter, skip_inherit="*", force_adapt=True)
adapt_related(cr, model, old, new, skip_inherit="*")
# clean filter's of custom search view
update_custom_filter(cr, old, model, new)

inherited_models = tuple(
inh.model for model in only_models for inh in for_each_inherit(cr, model, skip_inherit)
Expand Down Expand Up @@ -1121,3 +1138,48 @@ def update_server_actions_fields(cr, src_model, dst_model=None, fields_mapping=N
) % {"src_model": src_model, "dst_model": dst_model, "actions": ", ".join(action_names)}

add_to_migration_reports(message=msg, category="Server Actions")


def update_custom_filter(cr, old, model, new=None):
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
AND active = 't'
""".format(
arch_db
),
[match_old, model],
)
for view_id in cr.fetchall():
try:
with suppress(_Skip), 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:
if e.msg.startswith("Opening and ending tag mismatch"):
# this view is already wrong, we don't change it
_logger.warning("Skipping custom filter adaptaiton for invalid view (id=%s):\n%s", view_id, e.msg)
continue
raise

0 comments on commit 0f22f5b

Please sign in to comment.