Skip to content

Commit 3c7205a

Browse files
committed
[IMP] util/{domains,fields}: replace fields in server actions' update_path
`update_path` is used by server actions to know what field to update. It needs to be updated when renaming fields. closes #53 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 9fd126b commit 3c7205a

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/util/domains.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import collections
3+
import functools
34
import logging
45
import re
56

@@ -174,6 +175,21 @@ def _valid_path_to(cr, path, from_, to):
174175
return model is not None and model == to
175176

176177

178+
def _replace_path(cr, old, new, src_model, dst_model, path_str):
179+
"""
180+
Replace `old` by `new` in the fields path `path_str` assuming the path starts from `src_model`.
181+
The replace only takes place if `old` points at `dst_model`.
182+
"""
183+
dot_old = old.split(".")
184+
dot_new = new.split(".")
185+
path = path_str.split(".")
186+
for idx in range(len(path) - len(dot_old), -1, -1):
187+
r = slice(idx, idx + len(dot_old))
188+
if path[r] == dot_old and _valid_path_to(cr, path[:idx], src_model, dst_model):
189+
path[r] = dot_new
190+
return ".".join(path)
191+
192+
177193
def _adapt_one_domain(cr, target_model, old, new, model, domain, adapter=None, force_adapt=False):
178194
if not adapter:
179195
adapter = lambda leaf, _, __: [leaf]
@@ -197,15 +213,7 @@ def _adapt_one_domain(cr, target_model, old, new, model, domain, adapter=None, f
197213
return None
198214

199215
dot_old = old.split(".")
200-
dot_new = new.split(".")
201-
202-
def clean_path(left):
203-
path = left.split(".")
204-
for idx in range(len(path) - len(dot_old), -1, -1):
205-
r = slice(idx, idx + len(dot_old))
206-
if path[r] == dot_old and _valid_path_to(cr, path[:idx], model, target_model):
207-
path[r] = dot_new
208-
return ".".join(path)
216+
clean_path = functools.partial(_replace_path, cr, old, new, model, target_model)
209217

210218
def clean_term(term):
211219
if isinstance(term, basestring) or not isinstance(term[0], basestring):

src/util/fields.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import psycopg2
99
from psycopg2 import sql
10+
from psycopg2.extras import Json
1011

1112
try:
1213
from odoo import release
@@ -28,7 +29,7 @@ def make_index_name(table_name, column_name):
2829

2930

3031
from .const import ENVIRON
31-
from .domains import _adapt_one_domain, _valid_path_to, adapt_domains
32+
from .domains import _adapt_one_domain, _replace_path, _valid_path_to, adapt_domains
3233
from .exceptions import SleepyDeveloperError
3334
from .helpers import _dashboard_actions, _validate_model, table_of_model
3435
from .inherit import for_each_inherit
@@ -855,6 +856,32 @@ def _update_field_usage_multi(cr, models, old, new, domain_adapter=None, skip_in
855856
}
856857

857858
# ir.action.server
859+
if column_exists(cr, "ir_act_server", "update_path") and only_models:
860+
cr.execute(
861+
"""
862+
SELECT a.id,
863+
a.update_path,
864+
m.model
865+
FROM ir_act_server a
866+
JOIN ir_model m
867+
ON a.model_id = m.id
868+
WHERE a.state = 'object_write'
869+
AND a.update_path ~ %(old)s
870+
""",
871+
p,
872+
)
873+
to_update = {}
874+
for act_id, update_path, src_model in cr.fetchall():
875+
for dst_model in only_models:
876+
new_path = _replace_path(cr, old, new, src_model, dst_model, update_path)
877+
if new_path != update_path:
878+
to_update[act_id] = new_path
879+
if to_update:
880+
cr.execute(
881+
"UPDATE ir_act_server SET update_path = %s::jsonb->>id::text WHERE id IN %s",
882+
[Json(to_update), tuple(to_update)],
883+
)
884+
858885
# Modifying server action is dangerous.
859886
# The search pattern can be anywhere in the code, leading to invalid codes.
860887
# Moreover, limiting to some models will ignore some SA that should be modified.

0 commit comments

Comments
 (0)