Skip to content

Commit

Permalink
[IMP] rename_field : rename field also on custom view like the one used
Browse files Browse the repository at this point in the history
on dashboards
  • Loading branch information
duong77476-viindoo committed Jul 16, 2022
1 parent c9fa9b0 commit b88c457
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import inspect
import uuid
import re
import logging as _logging_module
from datetime import datetime
from functools import wraps
Expand Down Expand Up @@ -582,6 +583,65 @@ def rename_columns(cr, column_spec):
)


def _rename_field_on_dashboard(env, model, old_field, new_field):
dashboard_view_data = env["ir.ui.view.custom"].search([])
for r in dashboard_view_data:
parsed_arch = etree.XML(r.arch)
act_window_ids = parsed_arch.xpath("//action/@name")
actions = env["ir.actions.act_window"].search(
[
("id", "in", act_window_ids),
("res_model", "=", model),
]
)
for action in actions:
condition_for_element = "//action[@name='{}']".format(
action.id
)
condition_for_domain = "//action[@name='{}']/@domain".format(
action.id
)
condition_for_context = "//action[@name='{}']/@context".format(
action.id
)
arch_element = parsed_arch.xpath(condition_for_element)
for index in range(len(arch_element)):
elem = arch_element[index]
arch_domain = elem.xpath(condition_for_domain)[index]
arch_context = elem.xpath(condition_for_context)[index]

arch_context = re.sub(
r"""('group_by'|'col_group_by'|'graph_groupbys'
|'pivot_measures'|'pivot_row_groupby'|'pivot_column_groupby'
):([\s*][^\]]*)'%s(:day|:week|:month|:year)
{0,1}'(.*?\])"""
% old_field,
r"\1:\2'%s\3'\4" % new_field,
arch_context,
)

arch_context = re.sub(
r"""'graph_measure':([\s*])'%s(:day|:week|:month|:year)
{0,1}'"""
% old_field,
r"'graph_measure':\1'%s\2'" % new_field,
arch_context,
)

arch_domain = re.sub(
r"""('|")%s('|")""" % old_field,
r"\1%s\2" % new_field,
arch_domain,
)

elem.set("domain", arch_domain)
elem.set("context", arch_context)

new_arch = etree.tostring(parsed_arch, encoding="unicode")

r.write({"arch": new_arch})


def rename_fields(env, field_spec, no_deep=False):
"""Rename fields. Typically called in the pre script. WARNING: If using
this on base module, pass the argument ``no_deep`` with True value for
Expand Down Expand Up @@ -700,6 +760,9 @@ def rename_fields(env, field_spec, no_deep=False):
'new_pattern': "$$'%s'$$" % new_field,
}, (model, ),
)
# Rename on the custom view like the one used on dashboard
if version_info[0] > 9:
_rename_field_on_dashboard(env, model, old_field, new_field)


def rename_tables(cr, table_spec):
Expand Down

0 comments on commit b88c457

Please sign in to comment.