-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] spec_driven_model: register_hook when no ctx
- Loading branch information
Showing
3 changed files
with
39 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# Copyright 2019-TODAY Akretion - Raphael Valyi <[email protected]> | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html). | ||
|
||
from importlib import import_module | ||
|
||
from odoo import api, models | ||
from odoo.tools import frozendict | ||
|
||
|
@@ -58,12 +60,31 @@ def _get_concrete_model(self, model_name): | |
else: | ||
return self.env.get(model_name) | ||
|
||
def _spec_prefix(self): | ||
def _spec_prefix(self, split=False): | ||
""" | ||
_spec_prefix should be available for all generated specs mixins | ||
and it should be defined in SpecModel to avoid circular imports. | ||
Get spec_schema and spec_version from context or from class module | ||
""" | ||
return SpecModel._ensure_spec_prefix(self._context) | ||
if self._context.get("spec_schema") and self._context.get("spec_version"): | ||
spec_schema = self._context.get("spec_schema") | ||
spec_version = self._context.get("spec_version") | ||
if spec_schema and spec_version: | ||
spec_version = spec_version.replace(".", "")[:2] | ||
if split: | ||
return spec_schema, spec_version | ||
return f"{spec_schema}{spec_version}" | ||
|
||
for ancestor in type(self).mro(): | ||
if not ancestor.__module__.startswith("odoo.addons."): | ||
continue | ||
mod = import_module(".".join(ancestor.__module__.split(".")[:-1])) | ||
if hasattr(mod, "spec_schema"): | ||
spec_schema = mod.spec_schema | ||
spec_version = mod.spec_version.replace(".", "")[:2] | ||
if split: | ||
return spec_schema, spec_version | ||
return f"{spec_schema}{spec_version}" | ||
|
||
return None, None if split else None | ||
|
||
def _get_spec_property(self, spec_property="", fallback=None): | ||
""" | ||
|
@@ -82,22 +103,21 @@ def _register_hook(self): | |
their _auto_init method that will create their SQL DDL structure. | ||
""" | ||
res = super()._register_hook() | ||
if "spec_schema" not in self._context: | ||
spec_schema, spec_version = self._spec_prefix(split=True) | ||
if not spec_schema: | ||
return res | ||
|
||
spec_module = self._get_spec_property("odoo_module") | ||
if not spec_module: | ||
return res | ||
odoo_module = spec_module.split("_spec.")[0].split(".")[-1] | ||
load_key = f"_{spec_module}_loaded" | ||
if hasattr(self.env.registry, load_key): # already done for registry | ||
if hasattr(self.env.registry, load_key): # hook already done for registry | ||
return res | ||
setattr(self.env.registry, load_key, True) | ||
|
||
access_data = [] | ||
access_fields = [] | ||
relation_prefix = ( | ||
f"{self._context['spec_schema']}.{self._context['spec_version']}.%" | ||
) | ||
field_prefix = f"{self._context['spec_schema']}{self._context['spec_version']}_" | ||
field_prefix = f"{spec_schema}{spec_version}" | ||
relation_prefix = f"{spec_schema}.{spec_version}.%" | ||
self.env.cr.execute( | ||
"""SELECT DISTINCT relation FROM ir_model_fields | ||
WHERE relation LIKE %s;""", | ||
|
@@ -114,7 +134,6 @@ def _register_hook(self): | |
spec_class = StackedModel._odoo_name_to_class(name, spec_module) | ||
if spec_class is None: | ||
continue | ||
spec_class._module = "fiscal" # TODO use python_module ? | ||
fields = self.env[spec_class._name].fields_get_keys() | ||
rec_name = next( | ||
filter( | ||
|
@@ -135,8 +154,8 @@ def _register_hook(self): | |
) | ||
# we set _spec_schema and _spec_version because | ||
# _build_model will not have context access: | ||
model_type._spec_schema = self._context["spec_schema"] | ||
model_type._spec_version = self._context["spec_version"] | ||
model_type._spec_schema = spec_schema | ||
model_type._spec_version = spec_version | ||
models.MetaModel.module_to_models[odoo_module] += [model_type] | ||
|
||
# now we init these models properly | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters