Skip to content

Commit 3275bc5

Browse files
committed
Merge PR #155 into master
Signed-off-by jjscarafia
2 parents 829be25 + a203ad8 commit 3275bc5

7 files changed

Lines changed: 104 additions & 1 deletion

File tree

odoo_module_migrate/migration_scripts/migrate_180_190.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
22

33
from odoo_module_migrate.base_migration_script import BaseMigrationScript
4+
import re
5+
6+
7+
def migrate_expression_to_domain(
8+
logger, module_path, module_name, manifest_path, migration_steps, tools
9+
):
10+
"""Convert odoo.osv.expression usage to odoo.fields.Domain"""
11+
files_to_process = tools.get_files(module_path, (".py",))
12+
13+
for file in files_to_process:
14+
try:
15+
content = tools._read_content(file)
16+
original_content = content
17+
18+
content = re.sub(
19+
r"from odoo\.osv import expression",
20+
"from odoo.fields import Domain",
21+
content,
22+
)
23+
24+
content = re.sub(
25+
r"from odoo\.osv\.expression import (AND|OR|AND, OR|OR, AND)",
26+
"from odoo.fields import Domain",
27+
content,
28+
)
29+
30+
content = re.sub(r"expression\.AND\(", "Domain.AND(", content)
31+
content = re.sub(r"expression\.OR\(", "Domain.OR(", content)
32+
33+
content = re.sub(r"(?<!\.)AND\(", "Domain.AND(", content)
34+
content = re.sub(r"(?<!\.)OR\(", "Domain.OR(", content)
35+
36+
content = re.sub(
37+
r"from odoo\.fields import Domain, (AND|OR|AND, OR|OR, AND)",
38+
"from odoo.fields import Domain",
39+
content,
40+
)
41+
42+
lines = content.split("\n")
43+
seen_domain_import = False
44+
cleaned_lines = []
45+
46+
for line in lines:
47+
if line.strip() == "from odoo.fields import Domain":
48+
if not seen_domain_import:
49+
cleaned_lines.append(line)
50+
seen_domain_import = True
51+
else:
52+
cleaned_lines.append(line)
53+
54+
content = "\n".join(cleaned_lines)
55+
56+
if content != original_content:
57+
tools._write_content(file, content)
58+
logger.info(f"Migrated expression imports to Domain in: {file}")
59+
60+
except Exception as e:
61+
logger.error(f"Error processing file {file}: {str(e)}")
62+
463

564
import ast
665
import json
@@ -46,4 +105,7 @@ def build_sql_object(match):
46105

47106
class MigrationScript(BaseMigrationScript):
48107

49-
_GLOBAL_FUNCTIONS = [upgrade_sql_constraints]
108+
_GLOBAL_FUNCTIONS = [
109+
upgrade_sql_constraints,
110+
migrate_expression_to_domain,
111+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import res_partner
2+
from . import sale_order

tests/data_result/module_180_190/models/res_partner.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from odoo import models, fields, api
2+
from odoo.fields import Domain
23

34

45
class ResPartner(models.Model):
@@ -36,3 +37,9 @@ def test_groups_id_usage(self):
3637
self.create(vals)
3738

3839
return records
40+
41+
def search_with_expression(self, domain):
42+
result = Domain.AND([domain, [('active', '=', True)]])
43+
result2 = Domain.AND([result, Domain.OR([[('is_company', '=', True)]])])
44+
45+
return self.search(result2)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from odoo import models
2+
from odoo.fields import Domain
3+
4+
5+
class SaleOrder(models.Model):
6+
_inherit = "sale.order"
7+
8+
def simple_search(self, base_domain):
9+
extra_domain = [('state', '=', 'sale')]
10+
result1 = Domain.AND([base_domain, extra_domain])
11+
result2 = Domain.OR([result1, [('state', '=', 'draft')]])
12+
return result2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import res_partner
2+
from . import sale_order

tests/data_template/module_180/models/res_partner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from odoo import models, fields, api
2+
from odoo.osv import expression
3+
from odoo.osv.expression import AND, OR
24

35

46
class ResPartner(models.Model):
@@ -29,3 +31,9 @@ def test_groups_id_usage(self):
2931
self.create(vals)
3032

3133
return records
34+
35+
def search_with_expression(self, domain):
36+
result = expression.AND([domain, [('active', '=', True)]])
37+
result2 = AND([result, OR([[('is_company', '=', True)]])])
38+
39+
return self.search(result2)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from odoo import models
2+
from odoo.osv import expression
3+
4+
5+
class SaleOrder(models.Model):
6+
_inherit = "sale.order"
7+
8+
def simple_search(self, base_domain):
9+
extra_domain = [('state', '=', 'sale')]
10+
result1 = expression.AND([base_domain, extra_domain])
11+
result2 = expression.OR([result1, [('state', '=', 'draft')]])
12+
return result2

0 commit comments

Comments
 (0)