Skip to content
Snippets Groups Projects
xref.py 2.47 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

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
from odoo import models  # type: ignore[import-untyped]
oury.balde's avatar
oury.balde committed

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
from .base import Context, NewinstanceType, PostHookConverter
from .models.ir_model_data import _XREF_IMD_MODULE
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
# TODO dans quel cas ça ne pourrait pas être un instance getter???
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
class Xref(PostHookConverter):
    """This converter represents an external reference, using the standard xmlid with a
    custom module name.
    """

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    def __init__(
        self, module: str | None = _XREF_IMD_MODULE, is_instance_getter: bool = True
    ):
        self._module = module
Christophe de Vienne's avatar
Christophe de Vienne committed
        self._is_instance_getter = is_instance_getter

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    def odoo_to_message(self, instance: models.BaseModel, ctx: Context = None) -> Any:
        if not instance:
            return ""
        return instance.env["ir.model.data"].object_to_xmlid(
            instance, module=self._module
Christophe de Vienne's avatar
Christophe de Vienne committed

    def get_instance(
        self, odoo_env, message_data
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    ) -> None | models.BaseModel | NewinstanceType:
        if self._is_instance_getter:
            return odoo_env.ref(
                ".".join(["" if self._module is None else self._module, message_data]),
                raise_if_not_found=False,
            )
        return None
Christophe de Vienne's avatar
Christophe de Vienne committed

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    def post_hook(self, instance: models.BaseModel, message_data):
Christophe de Vienne's avatar
Christophe de Vienne committed
        # add xmlid to the newly created object
        instance.env["ir.model.data"].set_xmlid(
            instance, message_data, module=self._module, only_when_missing=True
Christophe de Vienne's avatar
Christophe de Vienne committed
        )

Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    @property
Christophe de Vienne's avatar
Christophe de Vienne committed
    def is_instance_getter(self) -> bool:
        return self._is_instance_getter