Skip to content
Snippets Groups Projects

Backport 18.0 changes

Merged Vincent Hatakeyama requested to merge topic/17.0/backport into branch/17.0
6 files
+ 188
25
Compare changes
  • Side-by-side
  • Inline
Files
6
  • - Allow prefix on Xref converter
    - Add option to include module name in messages. Incoming and outgoing message value have the same comportment.
      For example, if __converter__ is used as the module, both generated messages and received message will contain __converter__.<name>.
      Previously, generated messages would use the module name while received one would not.
+ 28
16
@@ -19,4 +19,5 @@
##############################################################################
import uuid
from typing import Final
@@ -22,4 +23,4 @@
from odoo import api, models
from odoo import api, models # type: ignore[import-untyped]
# Xrefs are stored within "ir.model.data" with this module name.
@@ -24,5 +25,5 @@
# Xrefs are stored within "ir.model.data" with this module name.
_XREF_IMD_MODULE = "__converter__"
_XREF_IMD_MODULE: Final[str] = "__converter__"
@@ -27,6 +28,6 @@
class IrModelData(models.Model):
class IrModelData(models.BaseModel):
"""Add xref tools.
All done with the super-admin user to bypass security rules.
@@ -37,5 +38,5 @@
_inherit = "ir.model.data"
@api.model
def generate_name(self):
def generate_name(self, prefix: str = "") -> str:
"""Generate an xref for odoo record;
@@ -41,4 +42,5 @@
"""Generate an xref for odoo record;
It return a UUID from a string of 32 hex digit
:param prefix: prefix to use before the name.
:return: a UUID from a string of 32 hex digit
"""
@@ -43,5 +45,5 @@
"""
return uuid.uuid4().hex
return prefix + uuid.uuid4().hex
@api.model
@@ -46,7 +48,12 @@
@api.model
def object_to_module_and_name(self, record_set, module=_XREF_IMD_MODULE):
def object_to_module_and_name(
self,
record_set: models.BaseModel,
module: str | None = _XREF_IMD_MODULE,
prefix: str = "",
) -> tuple[str, str]:
"""Retrieve an xref pointing to the specified Odoo record; create one
when missing.
:param module: Name of the module to use. None to use any name, if no
xmlid exists "" will be used as the module name.
@@ -49,9 +56,8 @@
"""Retrieve an xref pointing to the specified Odoo record; create one
when missing.
:param module: Name of the module to use. None to use any name, if no
xmlid exists "" will be used as the module name.
:type module: Optional[str]
:rtype: Tuple[str, str]
:param prefix: prefix to use before the name.
:return: tuple module and name
"""
record_set.ensure_one()
@@ -62,6 +68,8 @@
]
if module is not None:
domain.append(("module", "=", module))
if prefix:
domain.append(("name", "=like", f"{prefix}%"))
# Find an existing xref. See class docstring for details.
imd = self.sudo().search(domain, limit=1)
@@ -69,10 +77,10 @@
return imd.module, imd.name
# Could not find an existing xref; create one.
name = self.generate_name()
name = self.generate_name(prefix)
if module is None:
module = ""
self.set_xmlid(record_set, name, module)
return module, name
@api.model
@@ -73,11 +81,16 @@
if module is None:
module = ""
self.set_xmlid(record_set, name, module)
return module, name
@api.model
def object_to_xmlid(self, record_set, module=_XREF_IMD_MODULE):
def object_to_xmlid(
self,
record_set: models.BaseModel,
module: str | None = _XREF_IMD_MODULE,
prefix: str = "",
) -> str:
"""Retrieve an xref pointing to the specified Odoo record; create one
when missing.
:param module: Name of the module to use. None to use any name, if no
xmlid exists "" will be used as the module name.
@@ -80,8 +93,7 @@
"""Retrieve an xref pointing to the specified Odoo record; create one
when missing.
:param module: Name of the module to use. None to use any name, if no
xmlid exists "" will be used as the module name.
:type module: Optional[str]
:rtype: xmlid
:param prefix: prefix to use before the name.
"""
return "{0[0]}.{0[1]}".format(
@@ -86,8 +98,8 @@
"""
return "{0[0]}.{0[1]}".format(
self.object_to_module_and_name(record_set, module)
self.object_to_module_and_name(record_set, module, prefix)
)
@api.model
def set_xmlid(
self,
@@ -89,8 +101,8 @@
)
@api.model
def set_xmlid(
self,
record_set: models.Model,
record_set: models.BaseModel,
name: str,
module: str = _XREF_IMD_MODULE,
@@ -95,6 +107,6 @@
name: str,
module: str = _XREF_IMD_MODULE,
only_when_missing: str = False,
only_when_missing: bool = False,
):
"""Save an external reference to the specified Odoo record.
:param module: Name of the module to use.
Loading