22
22
from .helpers import _dashboard_actions , _validate_model
23
23
from .inherit import for_each_inherit
24
24
from .misc import SelfPrintEvalContext
25
- from .pg import column_exists , table_exists
25
+ from .pg import column_exists , get_value_or_en_translation , table_exists
26
+ from .records import edit_view
26
27
27
28
# python3 shims
28
29
try :
@@ -133,10 +134,8 @@ def _get_domain_fields(cr):
133
134
yield df
134
135
135
136
136
- def _valid_path_to (cr , path , from_ , to ):
137
- model = from_
138
- while path :
139
- field = path .pop (0 )
137
+ def _model_of_path (cr , model , path ):
138
+ for field in path :
140
139
cr .execute (
141
140
"""
142
141
SELECT relation
@@ -147,11 +146,15 @@ def _valid_path_to(cr, path, from_, to):
147
146
[model , field ],
148
147
)
149
148
if not cr .rowcount :
150
- # unknown field. Maybe an old domain. Cannot validate it.
151
- return False
149
+ return None
152
150
[model ] = cr .fetchone ()
153
151
154
- return model == to
152
+ return model
153
+
154
+
155
+ def _valid_path_to (cr , path , from_ , to ):
156
+ model = _model_of_path (cr , from_ , path )
157
+ return model is not None and model == to
155
158
156
159
157
160
def _adapt_one_domain (cr , target_model , old , new , model , domain , adapter = None , force_adapt = False ):
@@ -258,7 +261,7 @@ def adapt_domains(cr, model, old, new, adapter=None, skip_inherit=(), force_adap
258
261
it is part of an and ("&") domain. The other parameter signals if the leaf is
259
262
{negated} ("!").
260
263
261
- Note that the {adapter} is called ony on leafs that use the {old} field of {model}.
264
+ Note that the {adapter} is called only on leafs that use the {old} field of {model}.
262
265
263
266
{force_adapt} will run the adapter on all leaves having the removed field in the path. Useful
264
267
when deleting a field (in which case {new} is ignored).
@@ -288,6 +291,41 @@ def adapt_domains(cr, model, old, new, adapter=None, skip_inherit=(), force_adap
288
291
[unicode (new_domain ), id_ ],
289
292
)
290
293
294
+ # adapt search views
295
+ arch_db = get_value_or_en_translation (cr , "ir_ui_view" , "arch_db" )
296
+ cr .execute ("SELECT id, model FROM ir_ui_view WHERE {} ~ %s" .format (arch_db ), [match_old ])
297
+ for view_id , view_model in cr .fetchall ():
298
+ with edit_view (cr , view_id = view_id ) as view :
299
+ for node in view .xpath (
300
+ "//filter[contains(@domain, '{0}')]|//field[contains(@filter_domain, '{0}')]" .format (old )
301
+ ):
302
+ attr = "domain" if "domain" in node .attrib else "filter_domain"
303
+ domain = _adapt_one_domain (
304
+ cr , target_model , old , new , view_model , node .get (attr ), adapter = adapter , force_adapt = force_adapt
305
+ )
306
+ if domain :
307
+ node .set (attr , unicode (domain ))
308
+
309
+ for node in view .xpath ("//field[contains(@domain, '{0}')]" .format (old )):
310
+ # as <fields> can happen in sub-views, we should deternine the actual model the field belongs to
311
+ path = list (reversed ([p .get ("name" ) for p in node .iterancestors ("field" )])) + [node .get ("name" )]
312
+ field_model = _model_of_path (cr , view_model , path )
313
+ if not field_model :
314
+ continue
315
+
316
+ domain = _adapt_one_domain (
317
+ cr ,
318
+ target_model ,
319
+ old ,
320
+ new ,
321
+ field_model ,
322
+ node .get ("domain" ),
323
+ adapter = adapter ,
324
+ force_adapt = force_adapt ,
325
+ )
326
+ if domain :
327
+ node .set ("domain" , unicode (domain ))
328
+
291
329
# adapt domain in dashboards.
292
330
# NOTE: does not filter on model at dashboard selection for handle dotted domains
293
331
for _ , act in _dashboard_actions (cr , match_old ):
0 commit comments