Skip to content
Snippets Groups Projects
Commit 98d47549 authored by Florent Aide's avatar Florent Aide
Browse files

add a switch converter

parent 25ab9315
No related branches found
No related tags found
1 merge request!13add a switch converter
......@@ -11,4 +11,5 @@
RelationToManyMap,
RelationToOne,
)
from .switch import Switch # noqa: F401
from .xref import Xref # noqa: F401
from typing import Any, Callable, List, Mapping, Optional, Tuple
from odoo import models
from .base import ContextBuilder, Converter
class Switch(Converter):
# example usage:
# AURION_REFERENTIAL: Switch(
# [(lambda e: e.is_xxx, DictConverter("__wave__", {})),
# (None, DictConverter("__event__", {}))]
# )
def __init__(
self,
converters: List[Tuple[Callable, Converter]],
context: Optional[ContextBuilder] = None,
):
super()
self._converters = converters
self.context = context
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):
return converter.odoo_to_message(self, instance, ctx)
# raise or return None
##############################################################################
#
# Converter, for Odoo
# Copyright (C) 2021 XCG Consulting <https://odoo.consulting>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo import tests
from odoo.addons.converter import DictConverter, Field, Switch
class Test(tests.TransactionCase):
def setUp(self):
super(Test, self).setUp()
def test_to_message(self):
# first make sure we have an instance to test on
self.assertTrue(self.env.ref("base.bank_bnp").active)
converter = Switch(
[
(
lambda e: e.active,
DictConverter(
"__activebanks__",
{"name": Field("name"), "active": Field("active")},
),
),
(
None,
DictConverter(
"__inactivebanks__",
{"name": Field("name"), "active": Field("active")},
),
),
]
)
msg = converter.odoo_to_message()
self.assertEqual("__activebanks__", msg.get("type"))
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