Skip to content

Commit 6c7260a

Browse files
committed
[REF] spec_driven_model: s/generateds/xsdata/ refs
1 parent 0f9dd84 commit 6c7260a

File tree

5 files changed

+34
-50
lines changed

5 files changed

+34
-50
lines changed

spec_driven_model/models/spec_export.py

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _export_fields(self, xsd_fields, class_obj, export_dict):
5151
This method implements a dynamic dispatch checking if there is any
5252
method called _export_fields_CLASS_NAME to update the xsd_fields
5353
and export_dict variables, this way we allow controlling the
54-
flow of fields to export or injecting specific values ​​in the
54+
flow of fields to export or injecting specific values in the
5555
field export.
5656
"""
5757
self.ensure_one()
@@ -118,10 +118,6 @@ def _export_field(self, xsd_field, class_obj, field_spec, export_value=None):
118118
if field.comodel_name not in self._get_spec_classes():
119119
return False
120120
if hasattr(field, "xsd_choice_required"):
121-
# NOTE generateds-odoo would abusively have xsd_required=True
122-
# already in the spec file in this case.
123-
# In xsdata-odoo we introduced xsd_choice_required.
124-
# Here we make the legacy code compatible with xsdata-odoo:
125121
xsd_required = True
126122
return self._export_many2one(xsd_field, xsd_required, class_obj)
127123
elif self._fields[xsd_field].type == "one2many":
@@ -135,7 +131,7 @@ def _export_field(self, xsd_field, class_obj, field_spec, export_value=None):
135131
and self[xsd_field] is not False
136132
):
137133
if hasattr(field, "xsd_choice_required"):
138-
xsd_required = True # NOTE compat, see previous NOTE
134+
xsd_required = True
139135
return self._export_float_monetary(
140136
xsd_field, xsd_type, class_obj, xsd_required, export_value
141137
)
@@ -147,19 +143,19 @@ def _export_field(self, xsd_field, class_obj, field_spec, export_value=None):
147143
def _export_many2one(self, field_name, xsd_required, class_obj=None):
148144
self.ensure_one()
149145
if field_name in self._get_stacking_points().keys():
150-
return self._build_generateds(
146+
return self._build_binding(
151147
class_name=self._get_stacking_points()[field_name].comodel_name
152148
)
153149
else:
154-
return (self[field_name] or self)._build_generateds(
155-
class_obj._fields[field_name].comodel_name
150+
return (self[field_name] or self)._build_binding(
151+
class_name=class_obj._fields[field_name].comodel_name
156152
)
157153

158154
def _export_one2many(self, field_name, class_obj=None):
159155
self.ensure_one()
160156
relational_data = []
161157
for relational_field in self[field_name]:
162-
field_data = relational_field._build_generateds(
158+
field_data = relational_field._build_binding(
163159
class_name=class_obj._fields[field_name].comodel_name
164160
)
165161
relational_data.append(field_data)
@@ -192,8 +188,7 @@ def _export_datetime(self, field_name):
192188
).isoformat("T")
193189
)
194190

195-
# TODO rename _build_binding
196-
def _build_generateds(self, class_name=False, spec_schema=None, spec_version=None):
191+
def _build_binding(self, spec_schema=None, spec_version=None, class_name=None):
197192
"""
198193
Iterate over an Odoo record and its m2o and o2m sub-records
199194
using a pre-order tree traversal and map the Odoo record values
@@ -206,7 +201,7 @@ def _build_generateds(self, class_name=False, spec_schema=None, spec_version=Non
206201
self.ensure_one()
207202
if spec_schema and spec_version:
208203
self = self.with_context(
209-
self.env, spec_schema=spec_schema, spec_version=spec_version
204+
spec_schema=spec_schema, spec_version=spec_version
210205
)
211206
spec_prefix = self._spec_prefix(self._context)
212207
if not class_name:
@@ -229,27 +224,9 @@ def _build_generateds(self, class_name=False, spec_schema=None, spec_version=Non
229224
kwargs = {}
230225
binding_class = self._get_binding_class(class_obj)
231226
self._export_fields(xsd_fields, class_obj, export_dict=kwargs)
232-
if kwargs:
233-
sliced_kwargs = {
234-
key: kwargs.get(key)
235-
for key in binding_class.__dataclass_fields__.keys()
236-
if kwargs.get(key)
237-
}
238-
binding_instance = binding_class(**sliced_kwargs)
239-
return binding_instance
240-
241-
def export_xml(self):
242-
self.ensure_one()
243-
result = []
244-
if hasattr(self, f"_{self._spec_prefix(self._context)}_spec_settings"):
245-
binding_instance = self._build_generateds()
246-
result.append(binding_instance)
247-
return result
248-
249-
def export_ds(
250-
self, spec_schema, spec_version
251-
): # TODO change name -> export_binding!
252-
self.ensure_one()
253-
return self.with_context(
254-
spec_schema=spec_schema, spec_version=spec_version
255-
).export_xml()
227+
sliced_kwargs = {
228+
key: kwargs.get(key)
229+
for key in binding_class.__dataclass_fields__.keys()
230+
if kwargs.get(key)
231+
}
232+
return binding_class(**sliced_kwargs)

spec_driven_model/models/spec_import.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SpecMixinImport(models.AbstractModel):
2121
_name = "spec.mixin_import"
2222
_description = """
2323
A recursive Odoo object builder that works along with the
24-
GenerateDS object builder from the parsed XML.
24+
xsdata object builder from the parsed XML.
2525
Here we take into account the concrete Odoo objects where the schema
2626
mixins where injected and possible matcher or builder overrides.
2727
"""
@@ -31,7 +31,7 @@ def build_from_binding(self, spec_schema, spec_version, node, dry_run=False):
3131
"""
3232
Build an instance of an Odoo Model from a pre-populated
3333
Python binding object. Binding object such as the ones generated using
34-
generateDS can indeed be automatically populated from an XML file.
34+
xsdata can indeed be automatically populated from an XML file.
3535
This build method bridges the gap to build the Odoo object.
3636
3737
It uses a pre-order tree traversal of the Python bindings and for each

spec_driven_model/models/spec_view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def _build_spec_fragment(self, container=None):
133133
# TODO required only if visible
134134
@api.model
135135
def build_arch(self, lib_node, view_node, fields, depth=0):
136-
"""Creates a view arch from an generateds lib model arch"""
136+
"""Creates a view arch from an xsdata lib model arch"""
137137
# _logger.info("BUILD ARCH", lib_node)
138138
choices = set()
139139
wrapper_group = None

spec_driven_model/readme/DESCRIPTION.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Intro
22
~~~~~
33

4-
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 `GenerateDS <https://www.davekuhlman.org/generateDS.html>`_ pure Python databinding framework and is now being migrated to xsdata. So a good starting point is to read `the xsdata documentation here <https://xsdata.readthedocs.io/>`_
4+
This module is a databinding framework for Odoo and XML data: it allows to go from XML to Odoo objects back and forth. While having no hard dependency with it, it has been designed to be used with xsdata. So a good starting point is to read `the xsdata documentation here <https://xsdata.readthedocs.io/>`_
55

66
But what if instead of only generating Python structures from XML files you could actually generate full blown Odoo objects or serialize Odoo objects back to XML? This is what this module is for!
77

@@ -26,7 +26,7 @@ Now that you have generated these Odoo abstract bindings you should tell Odoo ho
2626

2727
Notice you should inherit from `spec_models.SpecModel` and not the usual `models.Model`.
2828

29-
**Field mapping**: You can then define two ways mapping between fields by overriding fields from Odoo or from the binding and using `_compute=` , `_inverse=` or simply `related=`.
29+
**Field mapping**: You can then define two ways mapping between fields by overriding fields from Odoo or from the binding using `_compute=` , `_inverse=` or simply `related=`.
3030

3131
**Relational fields**: simple fields are easily mapped this way. However what about relational fields? In your XSD schema, your electronic invoice is related to the `partner.binding.mixin` not to an Odoo `res.partner`. Don't worry, when `SpecModel` classes are instanciated for all relational fields, we look if their comodel have been injected into some existing Odoo model and if so we remap them to the proper Odoo model.
3232

@@ -36,7 +36,7 @@ Notice you should inherit from `spec_models.SpecModel` and not the usual `models
3636
StackedModel
3737
~~~~~~~~~~~~
3838

39-
Sadly real life XML is a bit more complex than that. Often XML structures are deeply nested just because it makes it easier for XSD schemas to validate them! for instance an electronic invoice line can be a nested structure with lots of tax details and product details. In a relational model like Odoo however you often want flatter data structures. This is where `StackedModel` comes to the rescue! It inherits from `SpecModel` and when you inherit from `StackedModel` you can inherit from all the generated mixins corresponding to the nested XML tags below some tag (here `invoice.line.binding.mixin`). All the fields corresponding to these XML tag attributes will be collected in your model and the XML parsing and serialization will happen as expected::
39+
Sadly real life XML is a bit more complex than that. Often XML structures are deeply nested just because it makes it easier for XSD schemas to validate them! for instance an electronic invoice line can be a nested structure with lots of tax details and product details. In a relational model like Odoo however you often want flatter data structures. This is where `StackedModel` comes to the rescue! It inherits from `SpecModel` and when you inherit from `StackedModel` you can inherit from all the generated mixins corresponding to the nested XML tags below some tag (here `invoice.line.binding.mixin`). All the fields corresponding to these XML tag attributes will be collected in your model and the XML parsing and serialization will happen as expected. Here is an example inspired from the Brazilian Electronic Invoice where the schema is called `nfe` and where we use the 2 digits `40` for its short version::
4040

4141

4242
from odoo.addons.spec_driven_model.models import spec_models
@@ -45,14 +45,21 @@ Sadly real life XML is a bit more complex than that. Often XML structures are de
4545
class InvoiceLine(spec_models.StackedModel):
4646
_inherit = [
4747
'account.move.line',
48-
'invoice.line.binding.mixin',
48+
'nfe.40.det',
4949
]
50-
_stacked = 'invoice.line.binding.mixin'
50+
_nfe40_spec_settings = {
51+
"module": "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00",
52+
"stacking_mixin": "nfe.40.det",
53+
"stacking_points": {},
54+
# all m2o below this level will be stacked even if not required:
55+
"stacking_force_paths": ("det.imposto.",),
56+
"stacking_skip_paths": ("nfe40_det_infNFe_id",),
57+
}
5158

52-
All many2one fields that are required in the XSD (xsd_required=True) will get their model stacked automatically and recursively. You can force non required many2one fields to be stacked using the `_force_stack_paths` attribute. On the contrary, you can avoid some required many2one fields to be stacked using the `stack_skip` attribute.
59+
All many2one fields that are required in the XSD (xsd_required=True) will get their model stacked automatically and recursively. You can force non required many2one fields to be stacked using the `stacking_force_paths` attribute. On the contrary, you can avoid some required many2one fields to be stacked using the `stacking_skip_paths` attribute.
5360

5461

55-
Hooks
56-
~~~~~
62+
Initialization hook
63+
~~~~~~~~~~~~~~~~~~~
5764

58-
Because XSD schemas can define lot's of different models, spec_driven_model comes with handy hooks that will automatically make all XSD mixins turn into concrete Odoo model (eg with a table) if you didn't inject them into existing Odoo models.
65+
Because XSD schemas can define lot's of different models, spec_driven_model comes with a handy _register_hook that will automatically make all XSD mixins turn into concrete Odoo model (eg with a table) if you didn't inject them into existing Odoo models.

spec_driven_model/tests/test_spec_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_create_export_import(self):
124124

125125
# 2nd we serialize it into a binding object:
126126
# (that could be further XML serialized)
127-
po_binding = po._build_generateds(spec_schema="poxsd", spec_version="10")
127+
po_binding = po._build_binding(spec_schema="poxsd", spec_version="10")
128128
self.assertEqual(
129129
[s.__name__ for s in type(po_binding).mro()],
130130
["PurchaseOrderType", "object"],

0 commit comments

Comments
 (0)