Skip to content
Snippets Groups Projects
Commit 98cccb0803de authored by Axel Prel's avatar Axel Prel
Browse files

remove converter.py and use converter module image converters

parent c3797452927d
No related branches found
No related tags found
No related merge requests found
##############################################################################
#
# Redner Odoo module
# Copyright © 2016, 2025 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/>.
#
##############################################################################
import base64
from collections.abc import Mapping
from typing import Any
from odoo import models # type: ignore[import-untyped]
from odoo.addons.converter import Converter
from odoo.tools.mimetypes import guess_mimetype # type: ignore[import-untyped]
def image(value: bytes):
# get MIME type associated with the decoded_data.
image_base64 = base64.b64decode(value)
mimetype = guess_mimetype(image_base64)
return {"body": value.decode("ascii"), "mime-type": mimetype}
class ImageFile(Converter):
def __init__(self, fieldname):
self.fieldname = fieldname
def odoo_to_message(
self, instance: models.Model, ctx: Mapping | None = None
) -> Any:
value = getattr(instance, self.fieldname)
if not value:
return {}
return image(value)
class ImageDataURL(Converter):
def __init__(self, fieldname):
self.fieldname = fieldname
def odoo_to_message(
self, instance: models.Model, ctx: Mapping | None = None
) -> Any:
value = getattr(instance, self.fieldname)
if not value:
return ""
content = base64.b64decode(value)
mimetype = guess_mimetype(content)
return "data:{};base64,{}".format(mimetype, value.decode("ascii"))
......@@ -22,5 +22,6 @@
import logging
from odoo import _, fields, models # type: ignore[import-untyped]
from odoo.addons import converter
from odoo.exceptions import ValidationError # type: ignore[import-untyped]
......@@ -25,7 +26,5 @@
from odoo.exceptions import ValidationError # type: ignore[import-untyped]
from ..converter import image
_logger = logging.getLogger(__name__)
......@@ -124,5 +123,5 @@
def render_variable_hook(self, variables):
"""Override to add additional variables in mail "render template" func"""
variables.update({"image": lambda value: image(value)})
variables.update({"image": lambda value: converter.image(value)})
return super().render_variable_hook(variables)
......@@ -22,7 +22,6 @@
from odoo.addons import converter
from odoo.exceptions import ValidationError # type: ignore[import-untyped]
from ..converter import ImageDataURL, ImageFile
from ..utils.sorting import parse_sorted_field, sortkey
FIELD = "field"
......@@ -161,7 +160,7 @@
path, name = sub.value.rsplit(".", 1)
else:
path, name = None, sub.value
conv = ImageFile(name)
conv = converter.ImageFile(name)
if path:
conv = converter.relation(path.replace(".", "/"), conv)
elif sub.converter == "image-data-url":
......@@ -165,7 +164,7 @@
if path:
conv = converter.relation(path.replace(".", "/"), conv)
elif sub.converter == "image-data-url":
conv = ImageDataURL(sub.value)
conv = converter.ImageDataURL(sub.value)
elif sub.converter == "relation-to-many":
# Unpack the result of finding a field with its sort order into
# variable names.
......
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