diff options
| author | Sébastien Theys <seb@odoo.com> | 2019-10-15 14:11:13 +0000 |
|---|---|---|
| committer | fw-bot <otis@odoo.com> | 2019-10-21 09:08:10 +0000 |
| commit | 38133ce0d1e71d1108aaea73323f2921b4876d45 (patch) | |
| tree | 9884703ca919581a6f11fa0224ae76bda7e4ec64 | |
| parent | 883fd9e045350512c437fe8e518f08e3b880b511 (diff) | |
[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
| -rw-r--r-- | odoo/addons/test_new_api/tests/test_new_fields.py | 5 | ||||
| -rw-r--r-- | odoo/fields.py | 10 |
2 files changed, 12 insertions, 3 deletions
diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index 805688f45ed..674dd0a029f 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -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' diff --git a/odoo/fields.py b/odoo/fields.py index 4abdc9dcf1f..f753371719a 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -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): |
