@@ -38,6 +38,7 @@ def make_index_name(table_name, column_name):
38
38
39
39
40
40
from .const import ENVIRON
41
+ from .context import adapt_context , clean_context
41
42
from .domains import _adapt_one_domain , _replace_path , _valid_path_to , adapt_domains
42
43
from .exceptions import SleepyDeveloperError
43
44
from .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):
62
63
63
64
# python3 shims
64
65
try :
65
- basestring # noqa: B018
66
+ unicode # noqa: B018
66
67
except NameError :
67
- basestring = unicode = str
68
+ unicode = str
68
69
69
70
70
71
_logger = logging .getLogger (__name__ )
71
72
IMD_FIELD_PATTERN = "field_%s__%s" if version_gte ("saas~11.2" ) else "field_%s_%s"
72
73
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
-
83
74
def ensure_m2o_func_field_data (cr , src_table , column , dst_table ):
84
75
"""
85
76
Fix broken m2o relations.
@@ -123,46 +114,10 @@ def remove_field(cr, model, fieldname, cascade=False, drop_column=True, skip_inh
123
114
124
115
ENVIRON ["__renamed_fields" ][model ][fieldname ] = None
125
116
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
-
162
117
# clean dashboard's contexts
163
118
for id_ , action in _dashboard_actions (cr , r"\y{}\y" .format (fieldname ), model ):
164
119
context = safe_eval (action .get ("context" , "{}" ), SelfPrintEvalContext (), nocopy = True )
165
- changed = clean_context (context )
120
+ changed = clean_context (context , fieldname )
166
121
action .set ("context" , unicode (context ))
167
122
if changed :
168
123
add_to_migration_reports (
@@ -176,7 +131,7 @@ def clean_context(context):
176
131
)
177
132
for id_ , name , context_s in cr .fetchall ():
178
133
context = safe_eval (context_s or "{}" , SelfPrintEvalContext (), nocopy = True )
179
- changed = clean_context (context )
134
+ changed = clean_context (context , fieldname )
180
135
cr .execute ("UPDATE ir_filters SET context = %s WHERE id = %s" , [unicode (context ), id_ ])
181
136
if changed :
182
137
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
1146
1101
# ir.ui.view.custom
1147
1102
# adapt the context. The domain will be done by `adapt_domain`
1148
1103
eval_context = SelfPrintEvalContext ()
1149
- def_old = "default_{}" .format (old )
1150
- def_new = "default_{}" .format (new )
1151
1104
match = "{0[old]}|{0[def_old]}" .format (p )
1152
1105
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
-
1187
1106
for _ , act in _dashboard_actions (cr , match , * only_models or ()):
1188
1107
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 )
1193
1109
act .set ("context" , unicode (context ))
1194
1110
1195
1111
# domains, related and inhited models
0 commit comments