|
12 | 12 | from odoo import release
|
13 | 13 | from odoo.tools.convert import xml_import
|
14 | 14 | from odoo.tools.misc import file_open
|
| 15 | + from odoo.tools.safe_eval import safe_eval |
15 | 16 | from odoo.tools.translate import xml_translate
|
16 | 17 | except ImportError:
|
17 | 18 | from openerp import release
|
18 | 19 | from openerp.tools.convert import xml_import
|
19 | 20 | from openerp.tools.misc import file_open
|
| 21 | + from openerp.tools.safe_eval import safe_eval |
20 | 22 |
|
21 | 23 | from .const import NEARLYWARN
|
22 | 24 | from .exceptions import MigrationError
|
23 | 25 | from .helpers import _get_theme_models, _ir_values_value, _validate_model, model_of_table, table_of_model
|
24 | 26 | from .indirect_references import indirect_references
|
25 | 27 | 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 |
27 | 29 | from .orm import env, flush
|
28 | 30 | from .pg import (
|
29 | 31 | PGRegexp,
|
@@ -1338,3 +1340,47 @@ def remove_act_window_view_mode(cr, model, view_mode):
|
1338 | 1340 | """,
|
1339 | 1341 | [view_mode, model, view_mode, view_mode],
|
1340 | 1342 | )
|
| 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