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

odt templates: fix file management with redner

- the file is correctly computed when fetched from redner
- the file name is standardized.
parent b3ce7077
No related branches found
No related tags found
1 merge request!67Implement caching and optimization for Redner template handling
......@@ -25,6 +25,7 @@
from odoo.exceptions import ValidationError
from ..redner import REDNER_API_PATH, Redner
from ..utils.mimetype import b64_to_extension
logger = logging.getLogger(__name__)
......@@ -35,7 +36,6 @@
COMPUTED_FIELDS = [
"body",
"description",
"template_data",
"slug",
"is_mjml",
"language",
......@@ -161,7 +161,12 @@
template_data = fields.Binary(
string="Libreoffice Template",
readonly=False,
compute="_compute_template",
compute="_compute_template_data",
)
template_data_filename = fields.Char(
string="Libreoffice Template Filename",
readonly=True,
)
def import_from_redner(self):
......@@ -206,7 +211,8 @@
cached_template = self.to_cache(record.id, template)
for f in COMPUTED_FIELDS + EDITABLE_FIELDS:
if f in cached_template:
setattr(record, f, cached_template[f])
new_val = cached_template[f]
setattr(record, f, new_val)
except Exception as e:
logger.error("Failed to read redner template :%s", e)
return
......@@ -218,7 +224,34 @@
for f in COMPUTED_FIELDS:
attr = getattr(record, f)
if not attr:
setattr(record, f, cached_template[f])
new_val = cached_template[f]
setattr(record, f, new_val)
@api.depends("template_data")
def _compute_template_data(self):
for record in self:
if not record.id or not record.redner_id:
continue
if not record.template_data:
try:
template = self.redner.templates.account_template_read(
record.redner_id
)
cached_template = self.to_cache(record.id, template)
if "template_data" in cached_template:
new_val = cached_template["template_data"]
encoded = base64.b64encode(new_val).decode("utf-8")
ext = ".odt" # default extension
try:
ext = b64_to_extension(encoded)
except Exception as e:
logger.error("Failed to read extension from file:%s", e)
return
record.template_data = encoded
record.template_data_filename = "template" + ext
except Exception as e:
logger.error("Failed to read redner template :%s", e)
return
def list_external_templates(self):
try:
......@@ -364,7 +397,7 @@
# compute if we should update redner or not
should_update_redner = False
for f in EDITABLE_FIELDS + COMPUTED_FIELDS:
for f in EDITABLE_FIELDS + COMPUTED_FIELDS + ["template_data"]:
# if we made a change in the record, update redner
if f in vals:
attr = getattr(self, f)
......
import base64
import mimetypes
import magic
def b64_to_extension(b64_string):
try:
# Decode the Base64 string into binary data
binary_data = base64.b64decode(b64_string)
# Use python-magic to determine the MIME type
mime = magic.Magic(mime=True)
mime_type = mime.from_buffer(binary_data)
# Get the file extension from the MIME type
return mimetypes.guess_extension(mime_type)
except Exception as e:
print(f"Error detecting file type: {e}")
return None
......@@ -114,5 +114,6 @@
readonly="not allow_modification_from_odoo"
>
<group>
<field name="template_data_filename" invisible="1" />
<field
name="template_data"
......@@ -117,4 +118,4 @@
<field
name="template_data"
filename="name"
filename="template_data_filename"
nolabel="1"
......@@ -120,4 +121,5 @@
nolabel="1"
widget="binary"
readonly="not allow_modification_from_odoo"
/>
</group>
......
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