# HG changeset patch # User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr> # Date 1660897130 -7200 # Fri Aug 19 10:18:50 2022 +0200 # Branch 13.0 # Node ID b9202f7d5dc1fe8c3d532c8c441c9f11b5911acb # Parent 2c74ad732a03919e7625037b349c01367fc6f904 🚑 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. diff --git a/NEWS.rst b/NEWS.rst --- a/NEWS.rst +++ b/NEWS.rst @@ -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 ===== diff --git a/__manifest__.py b/__manifest__.py --- a/__manifest__.py +++ b/__manifest__.py @@ -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/", diff --git a/field.py b/field.py --- a/field.py +++ b/field.py @@ -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 diff --git a/relation.py b/relation.py --- a/relation.py +++ b/relation.py @@ -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( diff --git a/switch.py b/switch.py --- a/switch.py +++ b/switch.py @@ -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,9 +58,12 @@ ) -> 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) - return None + return Skip def message_to_odoo( self, diff --git a/tests/test_relation.py b/tests/test_relation.py --- a/tests/test_relation.py +++ b/tests/test_relation.py @@ -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,10 +53,18 @@ 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, "") + 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.