Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] rename_fields : rename field also on custom view like the one used on dashboards #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import inspect
import logging as _logging_module
import os
import re
import sys
import uuid
from datetime import datetime
Expand Down Expand Up @@ -628,6 +629,66 @@ def rename_columns(cr, column_spec):
)


def _rename_field_on_custom_view(env, model, old_field, new_field):
"""
Rename field from ir.ui.view.custom. Typically called in the pre script.
:param env: enviroment variable
:param model: the model that contain field to replace
:param old_field: old field need to replace
:param new_field: new field need to replace
"""
dashboard_view_data = env["ir.ui.view.custom"].search(
[("arch", "ilike", old_field)]
)
for r in dashboard_view_data:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the search here and only if the view contains old_field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and suggestion, I'm quite busy at the moment so i will comeback with this ASAP. In the meantime, feel free to add some modification to this PR, i don't mind.

Kind regard,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrobaeza Forgive me, but is all you are suggesting is (in my simplistic ways)

if old_field not in r.arch:
    continue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More or less, for improving drastically the performance. The previous search shouldn't contain non matched results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done i use ilike search for ir.ui.view.custom that contain the old_field , is that ok? , also i rename the method and handle some of your comment, please review if you have time

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]
context_expr = (
r"""('group_by'|'col_group_by'|'graph_groupbys'|'orderedBy'
|'pivot_measures'|'pivot_row_groupby'|'pivot_column_groupby'
):([\s*][^\]]*)'%s(:day|:week|:month|:year)
{0,1}'(.*?\])"""
% old_field
)
context_expr = context_expr.replace("\n", "").replace(" ", "")
arch_context = re.sub(
context_expr,
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 @@ -791,6 +852,10 @@ def rename_fields(env, field_spec, no_deep=False):
},
(model,),
)
# Perform rename field on a custom view used on dashboard
# TODO: Rename when the field is part of a submodel (ex. m2one.field)
if version_info[0] > 9:
_rename_field_on_custom_view(env, model, old_field, new_field)


def rename_tables(cr, table_spec):
Expand Down
Loading