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

:ambulance: several fixes

Fix issue with Switch and the use of Skip as a converter, also make switch send Skip when nothing matches its rules.

Fix issue with RelationToMany that always convert to Skip when send_empty is False.

Fix issue with chaining relation converter with empty values.
parent 2c74ad732a03
No related branches found
Tags 15.0.3.1.0
2 merge requests!30Merge 13.0 (13.0.3.0.2 ➔ 13.0.3.1.0),!27🚑 several fixes
......@@ -2,6 +2,15 @@
History
*******
3.0.2
=====
Fix issue with Switch and the use of Skip as a converter, also make switch send Skip when nothing matches its rules.
Fix issue with RelationToMany that always convert to Skip when send_empty is False.
Fix issue with chaining relation converter with empty values.
3.0.1
=====
......
......@@ -21,7 +21,7 @@
"name": "Converter",
"license": "AGPL-3",
"summary": "Convert odoo records to/from plain data structures.",
"version": "13.0.3.0.1",
"version": "13.0.3.0.2",
"category": "Hidden",
"author": "XCG Consulting",
"website": "https://odoo.consulting/",
......
......@@ -31,7 +31,8 @@
Odoo
:param message_formatter: method that will be used to format the value
in the message. Signature should be (value: Any, is_blank: bool).
In the case of datetime, isoformat will not be used if this is defined.
In the case of datetime and date, isoformat will not be used if this is
defined.
:param odoo_formatter: method that will be used to format the value
for odoo.
"""
......@@ -89,6 +90,9 @@
)
if not self._message_formatter:
value = value.isoformat()
if isinstance(value, datetime.date):
if not self._message_formatter:
value = value.isoformat()
if self._message_formatter:
value = self._message_formatter(value, False)
return value
......
......@@ -14,7 +14,7 @@
def __init__(
self,
field_name: str,
model_name: Optional[str],
model_name: str,
converter: Converter,
send_empty: bool = True,
context: Optional[ContextBuilder] = None,
......@@ -31,8 +31,11 @@
ctx = build_context(instance, ctx, self.context)
# do not use super, otherwise if empty, will convert that
relation_instance = getattr(instance, self.field_name)
if not self._send_empty:
return Skip
if not relation_instance:
if not self._send_empty:
return Skip
else:
relation_instance = instance.env[self.model_name]
return self.converter.odoo_to_message(relation_instance, ctx)
def message_to_odoo(
......
......@@ -2,7 +2,7 @@
from odoo import api, models
from .base import ContextBuilder, Converter, Skip
from .base import ContextBuilder, Converter, Skip, SkipType
from .validate import VALIDATION_SKIP, Validator
......@@ -58,5 +58,8 @@
) -> Any:
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)
if isinstance(converter, SkipType):
return converter
else:
return converter.odoo_to_message(instance, ctx)
......@@ -62,5 +65,5 @@
return None
return Skip
def message_to_odoo(
self,
......
......@@ -23,6 +23,7 @@
Field,
Model,
RelationToOne,
Skip,
Xref,
message_to_odoo,
)
......@@ -34,6 +35,12 @@
super(Test, cls).setUpClass()
cls.converter1 = RelationToOne("company_id", "res.company", Xref(None))
cls.converter2 = RelationToOne("action_id", "res.company", Xref(None))
cls.converter3 = RelationToOne(
"action_id", "res.company", Xref(None), send_empty=False
)
cls.converter4 = RelationToOne(
"company_id", "res.company", Xref(None), send_empty=False
)
def setUp(self):
super().setUp()
......@@ -46,7 +53,11 @@
message = self.converter1.odoo_to_message(self.user_root)
self.assertEqual(message, "base.main_company")
def test_many2one_skip_from_odoo(self):
message = self.converter4.odoo_to_message(self.user_admin)
self.assertEqual(message, "base.main_company")
def test_empty_many2one_from_odoo(self):
message = self.converter2.odoo_to_message(self.user_root)
self.assertEqual(message, "")
......@@ -49,7 +60,11 @@
def test_empty_many2one_from_odoo(self):
message = self.converter2.odoo_to_message(self.user_root)
self.assertEqual(message, "")
def test_empty_many2one_skip_from_odoo(self):
message = self.converter3.odoo_to_message(self.user_root)
self.assertEqual(message, Skip)
def test_many2one_to_odoo(self):
"""Ensure a sub-object linked from the main one gets updated when Odoo
receives a message.
......
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