|
28 | 28 | from .const import BIG_TABLE_THRESHOLD |
29 | 29 | from .exceptions import MigrationError |
30 | 30 | from .helpers import table_of_model |
31 | | -from .misc import chunks, log_progress, version_gte |
| 31 | +from .misc import chunks, log_progress, version_between, version_gte |
32 | 32 | from .pg import column_exists, get_columns |
33 | 33 |
|
34 | 34 | # python3 shims |
@@ -556,39 +556,48 @@ def custom_module_field_as_manual(env, rollback=True, do_flush=False): |
556 | 556 | rec_names = {key: model._rec_name for key, model in env.registry.models.items()} |
557 | 557 |
|
558 | 558 | # 3.5 patches |
559 | | - # 3.5.1 `_build_model` calls `check_pg_name` even if the table is not created/altered, and in some cases |
560 | | - # models that have been converted to manual have a too long name, and we dont have the `_table` info. |
| 559 | + patches = [] # Ideally a contextlib.ExitStack but we need to support here Python2.7 |
| 560 | + |
| 561 | + @contextmanager |
| 562 | + def all_patches(): |
| 563 | + for p in patches: |
| 564 | + p.start() |
| 565 | + yield |
| 566 | + for p in reversed(patches): |
| 567 | + p.stop() |
| 568 | + |
561 | 569 | if version_gte("9.0"): |
562 | | - with patch("odoo.models.check_pg_name", lambda name: None): |
563 | | - # 3.5.2: `display_name` is added automatically, as a base field, and depends on the field `name` |
564 | | - # Sometimes, a custom model has no `name` field or it couldn't be loaded (e.g. an invalid `related`) |
565 | | - # Mark it as manual so its skipped on loading fail. |
566 | | - from odoo.models import BaseModel |
567 | | - |
568 | | - try: |
569 | | - origin_add_magic_fields = BaseModel._add_magic_fields |
570 | | - |
571 | | - def _add_magic_fields(self): |
572 | | - res = origin_add_magic_fields(self) |
573 | | - if self._custom and "display_name" in self._fields: |
574 | | - self._fields["display_name"].manual = True |
575 | | - return res |
576 | | - |
577 | | - def patch_display_name(): |
578 | | - return patch.object(BaseModel, "_add_magic_fields", _add_magic_fields) |
579 | | - |
580 | | - except AttributeError: |
581 | | - # Since saas-14.4, _add_magic_fields() no longer exists. Moreover, |
582 | | - # '_rec_name' is automatically fixed when the field it refers to is |
583 | | - # dropped from the model's class. Therefore, 'display_name' no |
584 | | - # longer needs to become manual. |
585 | | - @contextmanager |
586 | | - def patch_display_name(): |
587 | | - yield |
588 | | - |
589 | | - with patch_display_name(): |
590 | | - # 4. Reload the registry with the models and fields converted to manual. |
591 | | - env.registry.setup_models(env.cr) |
| 570 | + # 3.5.1 `_build_model` calls `check_pg_name` even if the table is not created/altered, and in some cases |
| 571 | + # models that have been converted to manual have a too long name, and we dont have the `_table` info. |
| 572 | + patches.append(patch("odoo.models.check_pg_name", lambda name: None)) |
| 573 | + |
| 574 | + if version_between("9.0", "saas~14.3"): |
| 575 | + # 3.5.2: `display_name` is added automatically, as a base field, and depends on the field `name` |
| 576 | + # Sometimes, a custom model has no `name` field or it couldn't be loaded (e.g. an invalid `related`) |
| 577 | + # Mark it as manual so its skipped on loading fail. |
| 578 | + from odoo.models import BaseModel |
| 579 | + |
| 580 | + # Since saas-14.4, _add_magic_fields() no longer exists. Moreover, |
| 581 | + # '_rec_name' is automatically fixed when the field it refers to is |
| 582 | + # dropped from the model's class. Therefore, 'display_name' no |
| 583 | + # longer needs to become manual. |
| 584 | + origin_add_magic_fields = BaseModel._add_magic_fields |
| 585 | + |
| 586 | + def _add_magic_fields(self): |
| 587 | + res = origin_add_magic_fields(self) |
| 588 | + if self._custom and "display_name" in self._fields: |
| 589 | + self._fields["display_name"].manual = True |
| 590 | + return res |
| 591 | + |
| 592 | + patches.append(patch.object(BaseModel, "_add_magic_fields", _add_magic_fields)) |
| 593 | + |
| 594 | + if version_gte("saas~16.4"): |
| 595 | + # 3.5.3 allow loading manual fields |
| 596 | + patches.append(patch("odoo.addons.base.models.ir_model.IrModelFields._is_manual_name", lambda self, name: True)) |
| 597 | + |
| 598 | + with all_patches(): |
| 599 | + # 4. Reload the registry with the models and fields converted to manual. |
| 600 | + env.registry.setup_models(env.cr) |
592 | 601 |
|
593 | 602 | # 5. Do the operation. |
594 | 603 | yield |
|
0 commit comments