Skip to content
Snippets Groups Projects
list.py 2.3 KiB
Newer Older
##############################################################################
#
#    Converter Odoo module
#    Copyright © 2020 XCG Consulting <https://xcg-consulting.fr>
#
#    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/>.
#
##############################################################################

Axel Prel's avatar
Axel Prel committed
from typing import Any
Christophe de Vienne's avatar
Christophe de Vienne committed

from odoo import api, models  # type: ignore[import-untyped]
Christophe de Vienne's avatar
Christophe de Vienne committed

from .base import Context, ContextBuilder, Converter, SkipType, build_context
oury.balde's avatar
oury.balde committed

Christophe de Vienne's avatar
Christophe de Vienne committed

class List(Converter):
    """A converter that takes a list of converter"""

    def __init__(
        self,
Axel Prel's avatar
Axel Prel committed
        context: ContextBuilder | None = None,
Christophe de Vienne's avatar
Christophe de Vienne committed
    ):
Christophe de Vienne's avatar
Christophe de Vienne committed
        self._converters = converters
        self.context = context

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    def odoo_to_message(self, instance: models.BaseModel, ctx: Context = None) -> Any:
Christophe de Vienne's avatar
Christophe de Vienne committed
        ctx = build_context(instance, ctx, self.context)

        message_data = []

        for converter in self._converters:
            value = converter.odoo_to_message(instance, ctx)
            if not isinstance(value, SkipType):
Christophe de Vienne's avatar
Christophe de Vienne committed
                message_data.append(value)

        return message_data

    def message_to_odoo(
        self,
        odoo_env: api.Environment,
Christophe de Vienne's avatar
Christophe de Vienne committed
        phase: str,
        message_value: Any,
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
        instance: models.BaseModel,
Christophe de Vienne's avatar
Christophe de Vienne committed
        value_present: bool = True,
    ) -> dict | SkipType:
        result = {}
        for i, converter in enumerate(self._converters):
            new_values = converter.message_to_odoo(
                odoo_env, phase, message_value[i], instance, value_present
            )
            if not isinstance(new_values, SkipType):
                result.update(new_values)
        return result