Skip to content

Commit

Permalink
[MIG] spec_driven_model: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed Jul 20, 2024
1 parent 00143e4 commit a7b52f1
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 66 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ exclude: |
# NOT INSTALLABLE ADDONS
# END NOT INSTALLABLE ADDONS
^l10n_br_nfe_spec/models/v4_0/| # (don't reformat generated code)
^spec_driven_model/tests/| # (tests include generated code)
# Files and folders generated by bots, to avoid loops
^setup/|/static/description/index\.html$|
# We don't want to mess with tool-generated files
Expand Down
12 changes: 6 additions & 6 deletions spec_driven_model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Spec Driven Model
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:dbff90223d9fb39623f2be2c25dbddc94e24a1ac269037c6d80ac35d1e51263b
!! source digest: sha256:7a60762120de2d0aa75c133393ab92467fdfd07bc140b199f292a250b0ffe461
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -17,13 +17,13 @@ Spec Driven Model
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--brazil-lightgray.png?logo=github
:target: https://github.com/OCA/l10n-brazil/tree/14.0/spec_driven_model
:target: https://github.com/OCA/l10n-brazil/tree/15.0/spec_driven_model
:alt: OCA/l10n-brazil
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/l10n-brazil-14-0/l10n-brazil-14-0-spec_driven_model
:target: https://translation.odoo-community.org/projects/l10n-brazil-15-0/l10n-brazil-15-0-spec_driven_model
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&target_branch=14.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -111,7 +111,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/l10n-brazil/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/l10n-brazil/issues/new?body=module:%20spec_driven_model%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/l10n-brazil/issues/new?body=module:%20spec_driven_model%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -155,6 +155,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-rvalyi|

This module is part of the `OCA/l10n-brazil <https://github.com/OCA/l10n-brazil/tree/14.0/spec_driven_model>`_ project on GitHub.
This module is part of the `OCA/l10n-brazil <https://github.com/OCA/l10n-brazil/tree/15.0/spec_driven_model>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion spec_driven_model/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Spec Driven Model",
"summary": """
Tools for specifications driven mixins (from xsd for instance)""",
"version": "14.0.5.2.2",
"version": "15.0.1.0.0",
"maintainers": ["rvalyi"],
"license": "LGPL-3",
"author": "Akretion,Odoo Community Association (OCA)",
Expand Down
5 changes: 4 additions & 1 deletion spec_driven_model/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def get_remaining_spec_models(cr, registry, module_name, spec_module):
all_spec_models = {
c._name
for name, c in inspect.getmembers(sys.modules[spec_module], inspect.isclass)
if c._name in registry
}

remaining_models = remaining_models.union(
Expand Down Expand Up @@ -160,16 +161,18 @@ def register_hook(env, module_name, spec_module, force=False):
fields,
)
)
inherit = list(spec_class._inherit) + ["spec.mixin"]
c = type(

Check warning on line 165 in spec_driven_model/hooks.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/hooks.py#L164-L165

Added lines #L164 - L165 were not covered by tests
name,
(SpecModel, spec_class),
{
"_name": name,
"_inherit": [spec_class._inherit, "spec.mixin"],
"_inherit": inherit,
"_original_module": "fiscal",
"_odoo_module": module_name,
"_spec_module": spec_module,
"_rec_name": rec_name,
"_module": module_name,
},
)
models.MetaModel.module_to_models[module_name] += [c]

Check warning on line 178 in spec_driven_model/hooks.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/hooks.py#L178

Added line #L178 was not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions spec_driven_model/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import spec_export
from . import spec_import
from . import spec_mixin

# from . import spec_view
from . import spec_models
from . import spec_import
from . import spec_export
5 changes: 3 additions & 2 deletions spec_driven_model/models/spec_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
_logger = logging.getLogger(__name__)


class AbstractSpecMixin(models.AbstractModel):
_inherit = "spec.mixin"
class SpecMixinExport(models.AbstractModel):
_name = "spec.mixin_export"
_description = "a mixin providing serialization features"

@api.model
def _get_binding_class(self, class_obj):
Expand Down
40 changes: 24 additions & 16 deletions spec_driven_model/models/spec_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
tz_datetime = re.compile(r".*[-+]0[0-9]:00$")


class AbstractSpecMixin(models.AbstractModel):
"""
class SpecMixinImport(models.AbstractModel):
_name = "spec.mixin_import"
_description = """
A recursive Odoo object builder that works along with the
GenerateDS object builder from the parsed XML.
Here we take into account the concrete Odoo objects where the schema
mixins where injected and possible matcher or builder overrides.
"""

_inherit = "spec.mixin"

@api.model
def build_from_binding(self, node, dry_run=False):
"""
Expand Down Expand Up @@ -115,7 +114,10 @@ def _build_attr(self, node, fields, vals, path, attr):
return False # ex: don't import NFe infRespTec

Check warning on line 114 in spec_driven_model/models/spec_import.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/models/spec_import.py#L114

Added line #L114 was not covered by tests
# example: company.nfe40_enderEmit related on partner_id
# then we need to set partner_id, not nfe40_enderEmit
key = fields[key].related[-1] # -1 works with _inherits
if isinstance(fields[key].related, list):
key = fields[key].related[-1] # -1 works with _inherits

Check warning on line 118 in spec_driven_model/models/spec_import.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/models/spec_import.py#L118

Added line #L118 was not covered by tests
else:
key = fields[key].related
comodel_name = fields[key].comodel_name
else:
clean_type = binding_type.lower()
Expand All @@ -139,24 +141,26 @@ def _build_attr(self, node, fields, vals, path, attr):
vals[key] = lines
else:
# m2o
new_value = comodel.build_attrs(value, path=child_path)
comodel_vals = comodel.build_attrs(value, path=child_path)
child_defaults = self._extract_related_values(vals, key)

new_value.update(child_defaults)
comodel_vals.update(child_defaults)
# FIXME comodel._build_many2one
self._build_many2one(comodel, vals, new_value, key, value, child_path)
self._build_many2one(
comodel, vals, comodel_vals, key, value, child_path
)

@api.model
def _build_string_not_simple_type(self, key, vals, value, node):
vals[key] = value

@api.model
def _build_many2one(self, comodel, vals, new_value, key, value, path):
def _build_many2one(self, comodel, vals, comodel_vals, key, value, path):
if comodel._name == self._name:
# stacked m2o
vals.update(new_value)
vals.update(comodel_vals)
else:
vals[key] = comodel.match_or_create_m2o(new_value, vals)
vals[key] = comodel.match_or_create_m2o(comodel_vals, vals)

@api.model
def get_concrete_model(self, comodel_name):
Expand Down Expand Up @@ -214,14 +218,18 @@ def _prepare_import_dict(

# reverse map related fields as much as possible
elif v.related is not None and vals.get(k) is not None:
if len(v.related) == 1:
vals[v.related[0]] = vals.get(k)
elif len(v.related) == 2 and k.startswith(self._field_prefix):
related_m2o = v.related[0]
if not hasattr(v, "__len__"):
related = v.related.split(".")
else:
related = v.related

Check warning on line 224 in spec_driven_model/models/spec_import.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/models/spec_import.py#L224

Added line #L224 was not covered by tests
if len(related) == 1:
vals[related[0]] = vals.get(k)
elif len(related) == 2 and k.startswith(self._field_prefix):
related_m2o = related[0]
# don't mess with _inherits write system
if not any(related_m2o == i[1] for i in model._inherits.items()):
key_vals = related_many2ones.get(related_m2o, {})
key_vals[v.related[1]] = vals.get(k)
key_vals[related[1]] = vals.get(k)
related_many2ones[related_m2o] = key_vals

# now we deal with the related m2o with compound related
Expand Down
1 change: 1 addition & 0 deletions spec_driven_model/models/spec_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SpecMixin(models.AbstractModel):

_description = "root abstract model meant for xsd generated fiscal models"
_name = "spec.mixin"
_inherit = ["spec.mixin_export", "spec.mixin_import"]
_stacking_points = {}
# _spec_module = 'override.with.your.python.module'
# _binding_module = 'your.pyhthon.binding.module'
Expand Down
68 changes: 40 additions & 28 deletions spec_driven_model/models/spec_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,48 @@ def _build_model(cls, pool, cr):
class as long as generated class inherit from some
spec.mixin.<schema_name> mixin.
"""
parents = cls._inherit
parents = [parents] if isinstance(parents, str) else (parents or [])
parents = [
item[0] if isinstance(item, list) else item for item in list(cls._inherit)
]
for parent in parents:
super_parents = pool[parent]._inherit
if isinstance(super_parents, str):
super_parents = [super_parents]
else:
super_parents = super_parents or []
super_parents = list(pool[parent]._inherit)
for super_parent in super_parents:
if super_parent.startswith("spec.mixin."):
cr.execute(
"SELECT name FROM ir_module_module "
"WHERE name=%s "
"AND state in ('to install', 'to upgrade', 'to remove')",
(pool[super_parent]._odoo_module,),
if not super_parent.startswith("spec.mixin."):
continue

cr.execute(
"SELECT name FROM ir_module_module "
"WHERE name=%s "
"AND state in ('to install', 'to upgrade', 'to remove')",
(pool[super_parent]._odoo_module,),
)
if cr.fetchall():
setattr(

Check warning on line 91 in spec_driven_model/models/spec_models.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/models/spec_models.py#L91

Added line #L91 was not covered by tests
pool,
"_%s_need_hook" % (pool[super_parent]._odoo_module,),
True,
)
if cr.fetchall():
setattr(
pool,
"_%s_need_hook" % (pool[super_parent]._odoo_module,),
True,
)

cls._map_concrete(parent, cls._name)
if not hasattr(pool[parent], "build_from_binding"):
pool[parent]._inherit = super_parents + ["spec.mixin"]
pool[parent].__bases__ = (pool["spec.mixin"],) + pool[
parent
].__bases__
return super()._build_model(pool, cr)
cls._map_concrete(parent, cls._name)

model_class = super()._build_model(pool, cr)
parents = [
item[0] if isinstance(item, list) else item for item in list(cls._inherit)
]
for parent in parents:
super_parents = list(pool[parent]._inherit)
for super_parent in super_parents:
if not super_parent.startswith("spec.mixin."):
continue
if "spec.mixin" not in [
c._name for c in model_class._BaseModel__base_classes
]:
model_class._BaseModel__base_classes = tuple(
[pool["spec.mixin"], pool["spec.mixin_export"]]
+ list(model_class._BaseModel__base_classes)
)
break
return model_class

@api.model
def _setup_base(self):
Expand All @@ -117,7 +129,7 @@ def _setup_fields(self):
relational fields pointing to such mixins should be remapped to the
proper concrete models where these mixins are injected.
"""
cls = type(self)
cls = self.env.registry[self._name]
for klass in cls.__bases__:
if (
not hasattr(klass, "_name")
Expand Down Expand Up @@ -165,7 +177,7 @@ def _setup_fields(self):
continue

Check warning on line 177 in spec_driven_model/models/spec_models.py

View check run for this annotation

Codecov / codecov/patch

spec_driven_model/models/spec_models.py#L177

Added line #L177 was not covered by tests
inv_name = field.inverse_name
for n, f in comodel._fields.items():
if n == inv_name and f.args.get("comodel_name"):
if n == inv_name and f.args and f.args.get("comodel_name"):
_logger.debug(
" MUTATING m2o %s.%s (%s) -> %s",
comodel._name.split(".")[-1],
Expand Down
2 changes: 1 addition & 1 deletion spec_driven_model/models/spec_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class SpecViewMixin(models.AbstractModel):
_inherit = "spec.mixin"
_name = "spec.mixin_view"

@api.model
def fields_view_get(
Expand Down
8 changes: 4 additions & 4 deletions spec_driven_model/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ <h1 class="title">Spec Driven Model</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:dbff90223d9fb39623f2be2c25dbddc94e24a1ac269037c6d80ac35d1e51263b
!! source digest: sha256:7a60762120de2d0aa75c133393ab92467fdfd07bc140b199f292a250b0ffe461
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-brazil/tree/14.0/spec_driven_model"><img alt="OCA/l10n-brazil" src="https://img.shields.io/badge/github-OCA%2Fl10n--brazil-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-brazil-14-0/l10n-brazil-14-0-spec_driven_model"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-brazil/tree/15.0/spec_driven_model"><img alt="OCA/l10n-brazil" src="https://img.shields.io/badge/github-OCA%2Fl10n--brazil-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-brazil-15-0/l10n-brazil-15-0-spec_driven_model"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<div class="section" id="intro">
<h1>Intro</h1>
<p>This module is a databinding framework for Odoo and XML data: it allows to go from XML to Odoo objects back and forth. This module started with the <a class="reference external" href="https://www.davekuhlman.org/generateDS.html">GenerateDS</a> pure Python databinding framework and is now being migrated to xsdata. So a good starting point is to read <a class="reference external" href="https://xsdata.readthedocs.io/">the xsdata documentation here</a></p>
Expand Down Expand Up @@ -438,7 +438,7 @@ <h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/l10n-brazil/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/l10n-brazil/issues/new?body=module:%20spec_driven_model%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/l10n-brazil/issues/new?body=module:%20spec_driven_model%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -473,7 +473,7 @@ <h1>Maintainers</h1>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/rvalyi"><img alt="rvalyi" src="https://github.com/rvalyi.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/l10n-brazil/tree/14.0/spec_driven_model">OCA/l10n-brazil</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/l10n-brazil/tree/15.0/spec_driven_model">OCA/l10n-brazil</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Loading

0 comments on commit a7b52f1

Please sign in to comment.