Skip to content

Commit

Permalink
wip update alt_text
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Aug 29, 2023
1 parent 0cc31fe commit d79a4da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
20 changes: 19 additions & 1 deletion fs_image/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
20 changes: 20 additions & 0 deletions fs_image/tests/test_fs_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}})

0 comments on commit d79a4da

Please sign in to comment.