|
8 | 8 | import os |
9 | 9 | import inspect |
10 | 10 | import uuid |
| 11 | +import re |
11 | 12 | import logging as _logging_module |
12 | 13 | from datetime import datetime |
13 | 14 | from functools import wraps |
@@ -582,6 +583,65 @@ def rename_columns(cr, column_spec): |
582 | 583 | ) |
583 | 584 |
|
584 | 585 |
|
| 586 | +def _rename_field_on_dashboard(env, model, old_field, new_field): |
| 587 | + dashboard_view_data = env["ir.ui.view.custom"].search([]) |
| 588 | + for r in dashboard_view_data: |
| 589 | + parsed_arch = etree.XML(r.arch) |
| 590 | + act_window_ids = parsed_arch.xpath("//action/@name") |
| 591 | + actions = env["ir.actions.act_window"].search( |
| 592 | + [ |
| 593 | + ("id", "in", act_window_ids), |
| 594 | + ("res_model", "=", model), |
| 595 | + ] |
| 596 | + ) |
| 597 | + for action in actions: |
| 598 | + condition_for_element = "//action[@name='{}']".format( |
| 599 | + action.id |
| 600 | + ) |
| 601 | + condition_for_domain = "//action[@name='{}']/@domain".format( |
| 602 | + action.id |
| 603 | + ) |
| 604 | + condition_for_context = "//action[@name='{}']/@context".format( |
| 605 | + action.id |
| 606 | + ) |
| 607 | + arch_element = parsed_arch.xpath(condition_for_element) |
| 608 | + for index in range(len(arch_element)): |
| 609 | + elem = arch_element[index] |
| 610 | + arch_domain = elem.xpath(condition_for_domain)[index] |
| 611 | + arch_context = elem.xpath(condition_for_context)[index] |
| 612 | + |
| 613 | + arch_context = re.sub( |
| 614 | + r"""('group_by'|'col_group_by'|'graph_groupbys' |
| 615 | + |'pivot_measures'|'pivot_row_groupby'|'pivot_column_groupby' |
| 616 | + ):([\s*][^\]]*)'%s(:day|:week|:month|:year) |
| 617 | + {0,1}'(.*?\])""" |
| 618 | + % old_field, |
| 619 | + r"\1:\2'%s\3'\4" % new_field, |
| 620 | + arch_context, |
| 621 | + ) |
| 622 | + |
| 623 | + arch_context = re.sub( |
| 624 | + r"""'graph_measure':([\s*])'%s(:day|:week|:month|:year) |
| 625 | + {0,1}'""" |
| 626 | + % old_field, |
| 627 | + r"'graph_measure':\1'%s\2'" % new_field, |
| 628 | + arch_context, |
| 629 | + ) |
| 630 | + |
| 631 | + arch_domain = re.sub( |
| 632 | + r"""('|")%s('|")""" % old_field, |
| 633 | + r"\1%s\2" % new_field, |
| 634 | + arch_domain, |
| 635 | + ) |
| 636 | + |
| 637 | + elem.set("domain", arch_domain) |
| 638 | + elem.set("context", arch_context) |
| 639 | + |
| 640 | + new_arch = etree.tostring(parsed_arch, encoding="unicode") |
| 641 | + |
| 642 | + r.write({"arch": new_arch}) |
| 643 | + |
| 644 | + |
585 | 645 | def rename_fields(env, field_spec, no_deep=False): |
586 | 646 | """Rename fields. Typically called in the pre script. WARNING: If using |
587 | 647 | this on base module, pass the argument ``no_deep`` with True value for |
@@ -700,6 +760,9 @@ def rename_fields(env, field_spec, no_deep=False): |
700 | 760 | 'new_pattern': "$$'%s'$$" % new_field, |
701 | 761 | }, (model, ), |
702 | 762 | ) |
| 763 | + # Rename on the custom view like the one used on dashboard |
| 764 | + if version_info[0] > 9: |
| 765 | + _rename_field_on_dashboard(env, model, old_field, new_field) |
703 | 766 |
|
704 | 767 |
|
705 | 768 | def rename_tables(cr, table_spec): |
|
0 commit comments