From d79a4da965366a5baa2891aa3dc15732d9eac4a3 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 29 Aug 2023 09:26:02 +0200 Subject: [PATCH] wip update alt_text --- fs_image/fields.py | 20 +++++++++++++++++++- fs_image/tests/test_fs_image.py | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fs_image/fields.py b/fs_image/fields.py index 2cb70d3bf1..3bb8585d38 100644 --- a/fs_image/fields.py +++ b/fs_image/fields.py @@ -4,6 +4,8 @@ from contextlib import contextmanager from io import BytesIO, IOBase +from odoo import _ +from odoo.exceptions import UserError from odoo.tools.image import image_process from odoo.addons.fs_attachment.models.ir_attachment import IrAttachment @@ -137,6 +139,9 @@ def create(self, record_values): return super().create(record_values) def write(self, records, value): + if isinstance(value, dict) and "content" not in value: + # we are writing on the alt_text field only + return self._update_alt_text(records, value) with self._set_image_process_mode(): return super().write(records, value) @@ -195,8 +200,21 @@ def _set_image_process_mode(self): finally: self._image_process_mode = False - def _process_related(self, value): + def _process_related(self, value: FSImageValue): """Override to resize the related value before saving it on self.""" value = super()._process_related(value) new_value = BytesIO(self._image_process(value)) return FSImageValue(value=new_value, alt_text=value.alt_text, name=value.name) + + def _update_alt_text(self, records, value: dict): + for record in records: + if not record.fs_image: + raise UserError( + _( + "Cannot set alt_text on empty image (record %(record)s.%(field_name)s)", + record=record, + field_name=self.name, + ) + ) + record.fs_image.alt_text = value["alt_text"] + return True diff --git a/fs_image/tests/test_fs_image.py b/fs_image/tests/test_fs_image.py index 378d39c6a8..296c6420d8 100644 --- a/fs_image/tests/test_fs_image.py +++ b/fs_image/tests/test_fs_image.py @@ -8,6 +8,7 @@ from odoo_test_helper import FakeModelLoader from PIL import Image +from odoo.exceptions import UserError from odoo.tests.common import TransactionCase from odoo.addons.fs_storage.models.fs_storage import FSStorage @@ -200,3 +201,22 @@ def test_related_with_b64(self): self.assert_image_size(instance.fs_image.getvalue(), 4000, 2000) self.assert_image_size(instance.fs_image_1024.getvalue(), 1024, 512) self.assert_image_size(instance.fs_image_512.getvalue(), 512, 256) + + def test_write_alt_text(self): + instance = self.env["test.image.model"].create( + {"fs_image": FSImageValue(name=self.filename, value=self.image_w)} + ) + instance.fs_image.alt_text = "test" + self.assertEqual(instance.fs_image.alt_text, "test") + + def test_write_alt_text_with_dict(self): + instance = self.env["test.image.model"].create( + {"fs_image": FSImageValue(name=self.filename, value=self.image_w)} + ) + instance.write({"fs_image": {"alt_text": "test"}}) + self.assertEqual(instance.fs_image.alt_text, "test") + + def test_write_alt_text_on_empty_with_dict(self): + instance = self.env["test.image.model"].create({}) + with self.assertRaisesRegex(UserError, "Cannot set alt_text on empty image"): + instance.write({"fs_image": {"alt_text": "test"}})