Skip to content
Snippets Groups Projects
Commit d5b3bcb01e3a authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

Update switch converter from afp_sync

parent b512d148cb78
No related branches found
No related tags found
1 merge request!17Update switch converter from afp_sync
from typing import Any, Callable, List, Mapping, Optional, Tuple
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple
from odoo import api, exceptions, models
......@@ -2,3 +4,4 @@
from odoo import models
from .base import ContextBuilder, Converter, Skip
from .validate import NotInitialized, Validator
......@@ -4,5 +7,5 @@
from .base import ContextBuilder, Converter
import jsonschema
class Switch(Converter):
......@@ -12,11 +15,20 @@
The function argument is the model instance.
Example usage:
AURION_REFERENTIAL: Switch(
[(lambda e: e.is_xxx, Model("__wave__", {})),
(None, Model("__event__", {}))]
)
.. code-block:: python
AURION_REFERENTIAL: Switch(
[
(
lambda e: e.is_xxx,
lambda p: "wave_code" in p,
Model("__wave__", {}),
),
(None, None, Model("__event__", {})),
]
)
"""
def __init__(
self,
......@@ -19,7 +31,14 @@
"""
def __init__(
self,
converters: List[Tuple[Callable[[models.Model], bool], Converter]],
converters: List[
Tuple[
Callable[[models.Model], bool],
Callable[[Any], bool],
Converter,
]
],
validator: Optional[Validator] = None,
context: Optional[ContextBuilder] = None,
):
......@@ -24,6 +43,11 @@
context: Optional[ContextBuilder] = None,
):
"""
:param converters: is a 3 tuple composed of:
out condition, in condition, and chosen converter
:param validator:
:param context:
"""
super()
self._converters = converters
self.context = context
......@@ -27,7 +51,16 @@
super()
self._converters = converters
self.context = context
if validator:
if validator.initialized:
self.validator = validator
else:
raise NotInitialized(
"you must initialize() the validator before passing it"
)
else:
self.validator = None
def odoo_to_message(
self, instance: models.Model, ctx: Optional[Mapping] = None
) -> Any:
......@@ -30,10 +63,10 @@
def odoo_to_message(
self, instance: models.Model, ctx: Optional[Mapping] = None
) -> Any:
for cond, converter in self._converters:
if cond is None or cond(instance):
for out_cond, _in_cond, converter in self._converters:
if out_cond is None or out_cond(instance):
return converter.odoo_to_message(instance, ctx)
return None
......@@ -36,5 +69,64 @@
return converter.odoo_to_message(instance, ctx)
return None
# TODO this model is missing the message_to_odoo implementation
def message_to_odoo(
self,
odoo_env: api.Environment,
phase: str,
message_value: Any,
instance: models.Model,
value_present: bool = True,
) -> Dict:
for _out_cond, in_cond, converter in self._converters:
if in_cond is None or in_cond(message_value):
return converter.message_to_odoo(
odoo_env,
phase,
message_value,
instance,
value_present=value_present,
)
return Skip
def is_instance_getter(self) -> bool:
for _out_cond, _in_cond, converter in self._converters:
if converter.is_instance_getter():
return True
return False
def get_instance(self, odoo_env, message_data):
for _out_cond, in_cond, converter in self._converters:
if in_cond is None or in_cond(message_data):
return converter.get_instance(odoo_env, message_data)
def odoo_to_message_validate(
self,
instance: models.Model,
synchronizer: Optional[models.Model] = None,
validation: bool = True,
):
data = self.odoo_to_message(instance)
for out_cond, _in_cond, converter in self._converters:
if out_cond is None or out_cond(instance):
if validation and converter._jsonschema is not None:
if not synchronizer:
raise exceptions.UserError(
"missing synchronizer for validation"
)
if self.validator is None:
self.validator = synchronizer.get_validator()
if isinstance(converter._jsonschema, str):
try:
self.validator.validate(
converter._jsonschema, data
)
except jsonschema.exceptions.ValidationError as e:
return data, str(e)
return data, None
......@@ -22,6 +22,10 @@
from .. import Field, Model, Switch
def falser(*a, **kw) -> bool:
return False
class Test(tests.TransactionCase):
def test_to_message(self):
# first make sure we have an instance to test on
......@@ -31,6 +35,7 @@
[
(
lambda e: e.active,
falser,
Model(
"__activebanks__",
{"name": Field("name"), "active": Field("active")},
......@@ -38,6 +43,7 @@
),
(
None,
falser,
Model(
"__inactivebanks__",
{"name": Field("name"), "active": Field("active")},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment