diff --git a/converter.py b/converter.py deleted file mode 100644 index c3797452927ded2d4242abfff7d1a286e3e8d8da_Y29udmVydGVyLnB5..0000000000000000000000000000000000000000 --- a/converter.py +++ /dev/null @@ -1,67 +0,0 @@ -############################################################################## -# -# 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")) diff --git a/models/mail_template.py b/models/mail_template.py index c3797452927ded2d4242abfff7d1a286e3e8d8da_bW9kZWxzL21haWxfdGVtcGxhdGUucHk=..98cccb0803deb1908a5c5a0bbc11721418b6472d_bW9kZWxzL21haWxfdGVtcGxhdGUucHk= 100644 --- a/models/mail_template.py +++ b/models/mail_template.py @@ -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) diff --git a/models/redner_substitution.py b/models/redner_substitution.py index c3797452927ded2d4242abfff7d1a286e3e8d8da_bW9kZWxzL3JlZG5lcl9zdWJzdGl0dXRpb24ucHk=..98cccb0803deb1908a5c5a0bbc11721418b6472d_bW9kZWxzL3JlZG5lcl9zdWJzdGl0dXRpb24ucHk= 100644 --- a/models/redner_substitution.py +++ b/models/redner_substitution.py @@ -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.