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

Christophe de Vienne's avatar
Christophe de Vienne committed

from odoo import models
oury.balde's avatar
oury.balde committed

from .base import Converter, NewinstanceType
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???
Christophe de Vienne's avatar
Christophe de Vienne committed
class Xref(Converter):
    """This converter represents an external reference, using the standard xmlid with a
    custom module name.
    """

    def __init__(self, module: str = _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

    def odoo_to_message(
        self, instance: models.Model, ctx: Optional[dict] = None
Christophe de Vienne's avatar
Christophe de Vienne committed
    ) -> 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
    ) -> None | models.Model | NewinstanceType:
        return odoo_env.ref(
            ".".join([self._module, message_data]), raise_if_not_found=False
Christophe de Vienne's avatar
Christophe de Vienne committed

    def post_hook(self, instance: models.Model, message_data):
        # 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
        )

    def is_instance_getter(self) -> bool:
        return self._is_instance_getter