@@ -38,6 +38,7 @@ def make_index_name(table_name, column_name):
3838
3939
4040from .const import ENVIRON
41+ from .context import adapt_context , clean_context
4142from .domains import _adapt_one_domain , _replace_path , _valid_path_to , adapt_domains
4243from .exceptions import SleepyDeveloperError
4344from .helpers import _dashboard_actions , _validate_model , resolve_model_fields_path , table_of_model
@@ -62,24 +63,14 @@ def make_index_name(table_name, column_name):
6263
6364# python3 shims
6465try :
65- basestring # noqa: B018
66+ unicode # noqa: B018
6667except NameError :
67- basestring = unicode = str
68+ unicode = str
6869
6970
7071_logger = logging .getLogger (__name__ )
7172IMD_FIELD_PATTERN = "field_%s__%s" if version_gte ("saas~11.2" ) else "field_%s_%s"
7273
73- _CONTEXT_KEYS_TO_CLEAN = (
74- "group_by" ,
75- "pivot_measures" ,
76- "pivot_column_groupby" ,
77- "pivot_row_groupby" ,
78- "graph_groupbys" ,
79- "orderedBy" ,
80- )
81-
82-
8374def ensure_m2o_func_field_data (cr , src_table , column , dst_table ):
8475 """
8576 Fix broken m2o relations.
@@ -123,46 +114,10 @@ def remove_field(cr, model, fieldname, cascade=False, drop_column=True, skip_inh
123114
124115 ENVIRON ["__renamed_fields" ][model ][fieldname ] = None
125116
126- def filter_value (key , value ):
127- if key == "orderedBy" and isinstance (value , dict ):
128- res = {k : (filter_value (None , v ) if k == "name" else v ) for k , v in value .items ()}
129- # return if name didn't match fieldname
130- return res if "name" not in res or res ["name" ] is not None else None
131- if not isinstance (value , basestring ):
132- # if not a string, ignore it
133- return value
134- if value .split (":" )[0 ] != fieldname :
135- # only return if not matching fieldname
136- return value
137- return None # value filtered out
138-
139- def clean_context (context ):
140- if not isinstance (context , dict ):
141- return False
142-
143- changed = False
144- for key in _CONTEXT_KEYS_TO_CLEAN :
145- if context .get (key ):
146- context_part = [filter_value (key , e ) for e in context [key ]]
147- changed |= context_part != context [key ]
148- context [key ] = [e for e in context_part if e is not None ]
149-
150- for vt in ["pivot" , "graph" , "cohort" ]:
151- key = "{}_measure" .format (vt )
152- if key in context :
153- new_value = filter_value (key , context [key ])
154- changed |= context [key ] != new_value
155- context [key ] = new_value if new_value is not None else "id"
156-
157- if vt in context :
158- changed |= clean_context (context [vt ])
159-
160- return changed
161-
162117 # clean dashboard's contexts
163118 for id_ , action in _dashboard_actions (cr , r"\y{}\y" .format (fieldname ), model ):
164119 context = safe_eval (action .get ("context" , "{}" ), SelfPrintEvalContext (), nocopy = True )
165- changed = clean_context (context )
120+ changed = clean_context (context , fieldname )
166121 action .set ("context" , unicode (context ))
167122 if changed :
168123 add_to_migration_reports (
@@ -176,7 +131,7 @@ def clean_context(context):
176131 )
177132 for id_ , name , context_s in cr .fetchall ():
178133 context = safe_eval (context_s or "{}" , SelfPrintEvalContext (), nocopy = True )
179- changed = clean_context (context )
134+ changed = clean_context (context , fieldname )
180135 cr .execute ("UPDATE ir_filters SET context = %s WHERE id = %s" , [unicode (context ), id_ ])
181136 if changed :
182137 add_to_migration_reports (("ir.filters" , id_ , name ), "Filters/Dashboards" )
@@ -1146,50 +1101,11 @@ def _update_field_usage_multi(cr, models, old, new, domain_adapter=None, skip_in
11461101 # ir.ui.view.custom
11471102 # adapt the context. The domain will be done by `adapt_domain`
11481103 eval_context = SelfPrintEvalContext ()
1149- def_old = "default_{}" .format (old )
1150- def_new = "default_{}" .format (new )
11511104 match = "{0[old]}|{0[def_old]}" .format (p )
11521105
1153- def adapt_value (key , value ):
1154- if key == "orderedBy" and isinstance (value , dict ):
1155- # only adapt the "name" key
1156- return {k : (adapt_value (None , v ) if k == "name" else v ) for k , v in value .items ()}
1157-
1158- if not isinstance (value , basestring ):
1159- # ignore if not a string
1160- return value
1161-
1162- parts = value .split (":" , 1 )
1163- if parts [0 ] != old :
1164- # if not match old, leave it
1165- return value
1166- # change to new, and return it
1167- parts [0 ] = new
1168- return ":" .join (parts )
1169-
1170- def adapt_dict (d ):
1171- # adapt (in place) dictionary values
1172- if not isinstance (d , dict ):
1173- return
1174-
1175- for key in _CONTEXT_KEYS_TO_CLEAN :
1176- if d .get (key ):
1177- d [key ] = [adapt_value (key , e ) for e in d [key ]]
1178-
1179- for vt in ["pivot" , "graph" , "cohort" ]:
1180- key = "{}_measure" .format (vt )
1181- if key in d :
1182- d [key ] = adapt_value (key , d [key ])
1183-
1184- if vt in d :
1185- adapt_dict (d [vt ])
1186-
11871106 for _ , act in _dashboard_actions (cr , match , * only_models or ()):
11881107 context = safe_eval (act .get ("context" , "{}" ), eval_context , nocopy = True )
1189- adapt_dict (context )
1190-
1191- if def_old in context :
1192- context [def_new ] = context .pop (def_old )
1108+ adapt_context (context , old , new )
11931109 act .set ("context" , unicode (context ))
11941110
11951111 # domains, related and inhited models
0 commit comments