[FIX] fields: ensure image values are stored in cache as binary

Binary fields are supposed to be saved as binary in cache, and not as string
which is also supported as an input value.

Pr: #38292
X-original-commit: 64e0d106adccd8132cbfeb6b60da74b3feaf05e1
This commit is contained in:
Sébastien Theys
2019-10-21 09:08:10 +00:00
committed by fw-bot
parent 883fd9e045
commit 38133ce0d1
2 changed files with 12 additions and 3 deletions
@@ -13,6 +13,7 @@ from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.tests import common
from odoo.tools import mute_logger, float_repr
from odoo.tools.date_utils import add, subtract, start_of, end_of
from odoo.tools.image import image_data_uri
class TestFields(common.TransactionCase):
@@ -1503,6 +1504,10 @@ class TestFields(common.TransactionCase):
self.assertEqual(record_bin_size.image_512, b'1.02 Kb')
self.assertEqual(record_bin_size.image_256, b'424.00 bytes')
# ensure image_data_uri works (value must be bytes and not string)
self.assertEqual(record.image_256[:8], b'iVBORw0K')
self.assertEqual(image_data_uri(record.image_256)[:30], 'data:image/png;base64,iVBORw0K')
def test_95_binary_bin_size(self):
binary_value = base64.b64encode(b'content')
binary_size = b'7.00 bytes'
+7 -3
View File
@@ -1895,7 +1895,9 @@ class Binary(Field):
# If the client requests only the size of the field, we return that
# instead of the content. Presumably a separate request will be done
# to read the actual content, if necessary.
return human_size(value)
value = human_size(value)
# human_size can return False (-> None) or a string (-> encoded)
return value.encode() if value else None
return None if value is False else value
def convert_to_record(self, value, record):
@@ -2029,13 +2031,15 @@ class Image(Binary):
# does not resize the same way as its related field
new_value = self._image_process(value)
new_record_values.append((record, new_value))
record.env.cache.update(record, self, [value if self.related else new_value] * len(record))
cache_value = self.convert_to_cache(value if self.related else new_value, record)
record.env.cache.update(record, self, [cache_value] * len(record))
super(Image, self).create(new_record_values)
def write(self, records, value):
new_value = self._image_process(value)
super(Image, self).write(records, new_value)
records.env.cache.update(records, self, [value if self.related else new_value] * len(records))
cache_value = self.convert_to_cache(value if self.related else new_value, records)
records.env.cache.update(records, self, [cache_value] * len(records))
def _image_process(self, value):
if value and (self.max_width or self.max_height):