Skip to content

Commit bb2e4a9

Browse files
Lukas Tranlef-adhoc
authored andcommitted
[Add] improve regex to detect for change API check access right
fix test case
1 parent 1734786 commit bb2e4a9

File tree

3 files changed

+165
-5
lines changed

3 files changed

+165
-5
lines changed

odoo_module_migrate/migration_scripts/migrate_170_180.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def replace_chatter_self_closing(match):
8787
def replace_user_has_groups(
8888
logger, module_path, module_name, manifest_path, migration_steps, tools
8989
):
90-
files_to_process = tools.get_files(module_path, (".py"))
90+
files_to_process = tools.get_files(module_path, (".py",))
9191
replaces = {
9292
r"self\.user_has_groups\(\s*(['\"])([\w\.]+)\1\s*\)": r"self.env.user.has_group(\1\2\1)",
9393
r"self\.user_has_groups\(\s*(['\"])([^'\"]*[,!][^'\"]*?)\1\s*\)": r"self.env.user.has_groups(\1\2\1)",
@@ -106,16 +106,74 @@ def replace_access_methods(
106106
files_to_process = tools.get_files(module_path, (".py",))
107107

108108
replaces = {
109+
# .check_access_rights('write', False) -> .has_access('write')
110+
r"\.check_access_rights\(['\"](\w+)['\"],\s*False\)": r".has_access('\1')",
111+
# .check_access_rights('write', True) -> .check_access('write')
112+
r"\.check_access_rights\(['\"](\w+)['\"],\s*True\)": r".check_access('\1')",
113+
# .check_access_rights('write', raise_exception=False) -> .has_access('write')
109114
r"\.check_access_rights\(['\"](\w+)['\"],\s*raise_exception=False\)": r".has_access('\1')",
115+
# .check_access_rights(operation='write', raise_exception=False) -> .has_access('write')
116+
r"\.check_access_rights\(\s*operation\s*=\s*['\"](\w+)['\"],\s*raise_exception\s*=\s*False\s*\)": r".has_access('\1')",
117+
# .check_access_rights(operation, raise_exception=False) -> .has_access(operation)
118+
r"\.check_access_rights\((\w+),\s*raise_exception=False\)": r".has_access(\1)",
119+
# .check_access_rights(operation=operation, raise_exception=False) -> .has_access(operation)
120+
r"\.check_access_rights\(operation=(\w+),\s*raise_exception=False\)": r".has_access(\1)",
121+
# .check_access_rights('write') -> .check_access('write')
110122
r"\.check_access_rights\(['\"](\w+)['\"]\)": r".check_access('\1')",
123+
# .check_access_rights('write', raise_exception=True) -> .check_access('write')
124+
r"\.check_access_rights\(['\"](\w+)['\"],\s*raise_exception\s*=\s*True\)": r".check_access('\1')",
125+
# .check_access_rights(operation='write') -> .check_access('write')
126+
r"\.check_access_rights\(operation=['\"](\w+)['\"]\)": r".check_access('\1')",
127+
# .check_access_rights(operation) -> .check_access(operation)
128+
r"\.check_access_rights\((\w+)\)": r".check_access(\1)",
129+
# .check_access_rights(operation=operation) -> .check_access(operation)
130+
r"\.check_access_rights\(operation=(\w+)\)": r".check_access(\1)",
131+
# .check_access_rule('write') -> .check_access('write')
111132
r"\.check_access_rule\(['\"](\w+)['\"]\)": r".check_access('\1')",
112-
r"_filter_access_rule\(['\"](\w+)['\"]\)": r"_filter_access('\1')",
113-
r"_filter_access_rule_python\(['\"](\w+)['\"]\)": r"_filter_access('\1')",
133+
# .check_access_rule(operation='write') -> .check_access('write')
134+
r"\.check_access_rule\(operation=['\"](\w+)['\"]\)": r".check_access('\1')",
135+
# .check_access_rule(operation) -> .check_access(operation)
136+
r"\.check_access_rule\((\w+)\)": r".check_access(\1)",
137+
# .check_access_rule(operation=operation) -> .check_access(operation)
138+
r"\.check_access_rule\(operation=(\w+)\)": r".check_access(\1)",
139+
# ._filter_access_rule('write') -> ._filter_access('write')
140+
r"\._filter_access_rule\(['\"](\w+)['\"]\)": r"._filter_access('\1')",
141+
# ._filter_access_rule(operation='write') -> ._filter_access('write')
142+
r"\._filter_access_rule\(operation=['\"](\w+)['\"]\)": r"._filter_access('\1')",
143+
# ._filter_access_rule(operation) -> ._filter_access(operation)
144+
r"\._filter_access_rule\((\w+)\)": r"._filter_access(\1)",
145+
# ._filter_access_rule(operation=operation) -> ._filter_access(operation)
146+
r"\._filter_access_rule\(operation=(\w+)\)": r"._filter_access(\1)",
147+
# ._filter_access_rule_python('write') -> ._filter_access('write')
148+
r"\._filter_access_rule_python\(['\"](\w+)['\"]\)": r"._filter_access('\1')",
149+
# ._filter_access_rule_python(operation='write') -> ._filter_access('write')
150+
r"\._filter_access_rule_python\(operation=['\"](\w+)['\"]\)": r"._filter_access('\1')",
151+
# ._filter_access_rule_python(operation) -> ._filter_access(operation)
152+
r"\._filter_access_rule_python\((\w+)\)": r"._filter_access(\1)",
153+
# ._filter_access_rule_python(operation=operation) -> ._filter_access(operation)
154+
r"\._filter_access_rule_python\(operation=(\w+)\)": r"._filter_access(\1)",
155+
# def check_access_rights(self, operation, raise_exception=True) -> def check_access(self, operation: str) -> None
156+
r"def\s+check_access_rights\s*\(\s*self\s*,\s*operation\s*,\s*raise_exception\s*=\s*True\s*\)": r"def check_access(self, operation: str) -> None",
157+
# def check_access_rights(self, operation='read', raise_exception=True) -> def check_access(self, operation: str = 'read') -> None
158+
r"def\s+check_access_rights\s*\(\s*self\s*,\s*operation\s*=\s*['\"](\w+)['\"]\s*,\s*raise_exception\s*=\s*True\s*\)": r"def check_access(self, operation: str = '\1') -> None",
159+
# def filter_access_rule pattern
160+
r"def\s+filter_access_rule\s*\(\s*self\s*,\s*operation\s*\)": r"def filter_access(self, operation: str) -> None",
161+
r"def\s+filter_access_rule\s*\(\s*self\s*,\s*operation\s*=\s*['\"](\w+)['\"]s*\)": r"def filter_access(self, operation: str = '\1') -> None",
162+
# def filter_access_rule_python pattern
163+
r"def\s+filter_access_rule_python\s*\(\s*self\s*,\s*operation\s*\)": r"def filter_access(self, operation: str) -> None",
164+
r"def\s+filter_access_rule_python\s*\(\s*self\s*,\s*operation\s*=\s*['\"](\w+)['\"]\s*\)": r"def filter_access(self, operation: str = '\1') -> None",
114165
}
115166

116167
for file in files_to_process:
117168
try:
118169
tools._replace_in_file(file, replaces)
170+
# After replacement, check for remaining check_access_rights calls
171+
after_replace_content = tools._read_content(file)
172+
if "check_access_rights" in after_replace_content:
173+
logger.warning(
174+
f"[18.0] check_access_rights() is deprecated. "
175+
f"Use instead .has_access() or .check_access(). File {file}"
176+
)
119177
except Exception as e:
120178
logger.error(f"Error processing file {file}: {str(e)}")
121179

tests/data_result/module_170_180/models/res_partner.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,60 @@ def example_method_has_group(self):
3232
pass
3333

3434
def example_method_access(self):
35+
operation = 'read'
3536
self.env['account.move'].check_access('read')
37+
self.env['account.move'].check_access('read')
38+
self.env['account.move'].check_access(operation)
39+
self.env['account.move'].check_access(operation)
40+
3641
self.env['account.move'].check_access('write')
42+
self.env['account.move'].check_access('write')
43+
self.env['account.move'].check_access(operation)
44+
self.env['account.move'].check_access(operation)
45+
3746
self.env['account.move']._filter_access('read')
3847
self.env['account.move']._filter_access('read')
39-
48+
self.env['account.move']._filter_access(operation)
49+
self.env['account.move']._filter_access(operation)
50+
51+
self.env['account.move']._filter_access('read')
52+
self.env['account.move']._filter_access('read')
53+
self.env['account.move']._filter_access(operation)
54+
self.env['account.move']._filter_access(operation)
55+
56+
if not self.env['account.move'].has_access('read'):
57+
pass
58+
4059
if not self.env['account.move'].has_access('read'):
4160
pass
61+
62+
if not self.env['account.move'].has_access(operation):
63+
pass
64+
65+
if not self.env['account.move'].has_access(operation):
66+
pass
67+
68+
self.env["res.brand"].has_access('read')
69+
self.env["res.brand"].check_access('read')
70+
71+
self.env["res.brand"].has_access('write')
72+
73+
self.env["res.brand"].check_access('read')
74+
75+
def check_access(self, operation: str) -> None:
76+
pass
77+
78+
def check_access(self, operation: str = 'read') -> None:
79+
pass
80+
81+
def filter_access(self, operation: str) -> None:
82+
pass
83+
84+
def filter_access(self, operation: str = 'read') -> None:
85+
pass
86+
87+
def filter_access(self, operation: str) -> None:
88+
pass
89+
90+
def filter_access(self, operation: str = 'read') -> None:
91+
pass

tests/data_template/module_170/models/res_partner.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,62 @@ def example_method_has_group(self):
3232
pass
3333

3434
def example_method_access(self):
35+
operation = 'read'
3536
self.env['account.move'].check_access_rights('read')
37+
self.env['account.move'].check_access_rights(operation='read')
38+
self.env['account.move'].check_access_rights(operation)
39+
self.env['account.move'].check_access_rights(operation=operation)
40+
3641
self.env['account.move'].check_access_rule('write')
42+
self.env['account.move'].check_access_rule(operation='write')
43+
self.env['account.move'].check_access_rule(operation)
44+
self.env['account.move'].check_access_rule(operation=operation)
45+
3746
self.env['account.move']._filter_access_rule('read')
47+
self.env['account.move']._filter_access_rule(operation='read')
48+
self.env['account.move']._filter_access_rule(operation)
49+
self.env['account.move']._filter_access_rule(operation=operation)
50+
3851
self.env['account.move']._filter_access_rule_python('read')
39-
52+
self.env['account.move']._filter_access_rule_python(operation='read')
53+
self.env['account.move']._filter_access_rule_python(operation)
54+
self.env['account.move']._filter_access_rule_python(operation=operation)
55+
4056
if not self.env['account.move'].check_access_rights('read', raise_exception=False):
4157
pass
58+
59+
if not self.env['account.move'].check_access_rights(operation='read', raise_exception=False):
60+
pass
61+
62+
if not self.env['account.move'].check_access_rights(operation, raise_exception=False):
63+
pass
64+
65+
if not self.env['account.move'].check_access_rights(operation=operation, raise_exception=False):
66+
pass
67+
68+
self.env["res.brand"].check_access_rights("read", False)
69+
self.env["res.brand"].check_access_rights("read", True)
70+
71+
self.env["res.brand"].check_access_rights(
72+
operation="write", raise_exception=False
73+
)
74+
75+
self.env["res.brand"].check_access_rights("read", raise_exception=True)
76+
77+
def check_access_rights(self, operation, raise_exception=True):
78+
pass
79+
80+
def check_access_rights(self, operation="read", raise_exception=True):
81+
pass
82+
83+
def filter_access_rule(self, operation):
84+
pass
85+
86+
def filter_access_rule(self, operation='read'):
87+
pass
88+
89+
def filter_access_rule_python(self, operation):
90+
pass
91+
92+
def filter_access_rule_python(self, operation='read'):
93+
pass

0 commit comments

Comments
 (0)