|
1 | 1 | # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). |
2 | 2 |
|
3 | 3 | 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 | + |
4 | 63 |
|
5 | 64 | import ast |
6 | 65 | import json |
@@ -46,4 +105,7 @@ def build_sql_object(match): |
46 | 105 |
|
47 | 106 | class MigrationScript(BaseMigrationScript): |
48 | 107 |
|
49 | | - _GLOBAL_FUNCTIONS = [upgrade_sql_constraints] |
| 108 | + _GLOBAL_FUNCTIONS = [ |
| 109 | + upgrade_sql_constraints, |
| 110 | + migrate_expression_to_domain, |
| 111 | + ] |
0 commit comments