Skip to content

Commit 0f22f5b

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 3869719 commit 0f22f5b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/util/fields.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import warnings
77

8+
import lxml
89
import psycopg2
910

1011
try:
@@ -18,6 +19,12 @@
1819
from openerp.tools.misc import mute_logger
1920
from openerp.tools.safe_eval import safe_eval
2021

22+
try:
23+
from contextlib import suppress
24+
except ImportError:
25+
# python2 code, use the openerp vendor
26+
from openerp.tools.misc import ignore as suppress
27+
2128
try:
2229
from odoo.tools.sql import make_index_name
2330
except ImportError:
@@ -45,6 +52,7 @@ def make_index_name(table_name, column_name):
4552
savepoint,
4653
table_exists,
4754
)
55+
from .records import edit_view
4856
from .report import add_to_migration_reports, get_anchor_link_to_record
4957

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

5664

65+
class _Skip(Exception):
66+
pass
67+
68+
5769
_logger = logging.getLogger(__name__)
5870
IMD_FIELD_PATTERN = "field_%s__%s" if version_gte("saas~11.2") else "field_%s_%s"
5971

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

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

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

11231140
add_to_migration_reports(message=msg, category="Server Actions")
1141+
1142+
1143+
def update_custom_filter(cr, old, model, new=None):
1144+
arch_db = (
1145+
get_value_or_en_translation(cr, "ir_ui_view", "arch_db")
1146+
if column_exists(cr, "ir_ui_view", "arch_db")
1147+
else "arch"
1148+
)
1149+
match_old = r"\y{}\y".format(re.escape(old))
1150+
cr.execute(
1151+
"""
1152+
SELECT id
1153+
FROM ir_ui_view
1154+
WHERE {} ~ %s
1155+
AND type = 'search'
1156+
AND model = %s
1157+
AND active = 't'
1158+
""".format(
1159+
arch_db
1160+
),
1161+
[match_old, model],
1162+
)
1163+
for view_id in cr.fetchall():
1164+
try:
1165+
with suppress(_Skip), edit_view(cr, view_id=view_id, active=None) as view:
1166+
for elem in view.xpath("//filter[contains(@context, '{0}')]".format(old)):
1167+
context = elem.attrib["context"]
1168+
context = safe_eval(context or "{}", SelfPrintEvalContext(), nocopy=True)
1169+
if "group_by" in context and context["group_by"] == old:
1170+
if new:
1171+
context["group_by"] = new
1172+
else:
1173+
context.pop("group_by")
1174+
_logger.info(
1175+
"Field %s was removed during the upgrade, context has been updated from filter %s to avoid disabling of custom view",
1176+
old,
1177+
elem.attrib["name"],
1178+
)
1179+
elem.attrib["context"] = unicode(context)
1180+
except lxml.etree.XMLSyntaxError as e:
1181+
if e.msg.startswith("Opening and ending tag mismatch"):
1182+
# this view is already wrong, we don't change it
1183+
_logger.warning("Skipping custom filter adaptaiton for invalid view (id=%s):\n%s", view_id, e.msg)
1184+
continue
1185+
raise

0 commit comments

Comments
 (0)