Skip to content

Commit f48f3f5

Browse files
committed
[FIX] util/fields : update field in context of custom search view
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>
1 parent 35b43aa commit f48f3f5

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/util/fields.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def make_index_name(table_name, column_name):
4848
savepoint,
4949
table_exists,
5050
)
51+
from .records import adapt_search_views
5152
from .report import add_to_migration_reports, get_anchor_link_to_record
5253

5354
# python3 shims
@@ -175,7 +176,8 @@ def clean_context(context):
175176
""",
176177
[(fieldname, fieldname + " desc"), model, r"\y{}\y".format(fieldname)],
177178
)
178-
179+
# clean filter's of custom search view
180+
adapt_search_views(cr, fieldname, model, skip_inherit=skip_inherit)
179181
def adapter(leaf, is_or, negated):
180182
# replace by TRUE_LEAF, unless negated or in a OR operation but not negated
181183
if is_or ^ negated:
@@ -1061,6 +1063,8 @@ def adapt_dict(d):
10611063
adapt_domains(cr, model, old, new, adapter=domain_adapter, skip_inherit="*", force_adapt=True)
10621064
adapt_related(cr, model, old, new, skip_inherit="*")
10631065
adapt_depends(cr, model, old, new, skip_inherit="*")
1066+
# clean filter's of custom search view
1067+
adapt_search_views(cr, old, model, new, skip_inherit="*")
10641068

10651069
inherited_models = tuple(
10661070
inh.model for model in only_models for inh in for_each_inherit(cr, model, skip_inherit)

src/util/records.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
from odoo import release
1313
from odoo.tools.convert import xml_import
1414
from odoo.tools.misc import file_open
15+
from odoo.tools.safe_eval import safe_eval
1516
from odoo.tools.translate import xml_translate
1617
except ImportError:
1718
from openerp import release
1819
from openerp.tools.convert import xml_import
1920
from openerp.tools.misc import file_open
21+
from openerp.tools.safe_eval import safe_eval
2022

2123
from .const import NEARLYWARN
2224
from .exceptions import MigrationError
2325
from .helpers import _get_theme_models, _ir_values_value, _validate_model, model_of_table, table_of_model
2426
from .indirect_references import indirect_references
2527
from .inherit import direct_inherit_parents, for_each_inherit
26-
from .misc import parse_version, version_gte
28+
from .misc import SelfPrintEvalContext, parse_version, version_gte
2729
from .orm import env, flush
2830
from .pg import (
2931
PGRegexp,
@@ -1338,3 +1340,47 @@ def remove_act_window_view_mode(cr, model, view_mode):
13381340
""",
13391341
[view_mode, model, view_mode, view_mode],
13401342
)
1343+
1344+
1345+
def adapt_search_views(cr, old, model, new=None, skip_inherit=()):
1346+
arch_db = (
1347+
get_value_or_en_translation(cr, "ir_ui_view", "arch_db")
1348+
if column_exists(cr, "ir_ui_view", "arch_db")
1349+
else "arch"
1350+
)
1351+
match_old = r"\y{}\y".format(re.escape(old))
1352+
cr.execute(
1353+
"""
1354+
SELECT id
1355+
FROM ir_ui_view
1356+
WHERE {} ~ %s
1357+
AND type = 'search'
1358+
AND model = %s
1359+
""".format(arch_db),
1360+
[match_old, model],
1361+
)
1362+
for view_id in cr.fetchall():
1363+
try:
1364+
with edit_view(cr, view_id=view_id, active=None) as view:
1365+
for elem in view.xpath("//filter[contains(@context, '{0}')]".format(old)):
1366+
context = elem.attrib["context"]
1367+
context = safe_eval(context or "{}", SelfPrintEvalContext(), nocopy=True)
1368+
if "group_by" in context and context["group_by"] == old:
1369+
if new:
1370+
context["group_by"] = new
1371+
else:
1372+
context.pop("group_by")
1373+
_logger.info(
1374+
"Field %s was removed during the upgrade, context has been updated from filter %s to avoid disabling of custom view",
1375+
old,
1376+
elem.attrib["name"],
1377+
)
1378+
elem.attrib["context"] = unicode(context)
1379+
except lxml.etree.XMLSyntaxError as e:
1380+
# Error faced while editing view
1381+
_logger.warning("Skipping custom search view adaptaiton for invalid view (id=%s):\n%s", view_id, e.msg)
1382+
continue
1383+
1384+
# down on inherits
1385+
for inh in for_each_inherit(cr, model, skip_inherit):
1386+
adapt_search_views(cr, old, inh.model, new=new, skip_inherit=skip_inherit)

0 commit comments

Comments
 (0)