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 674dd0a029f..c485bb21252 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -1508,6 +1508,12 @@ class TestFields(common.TransactionCase): self.assertEqual(record.image_256[:8], b'iVBORw0K') self.assertEqual(image_data_uri(record.image_256)[:30], 'data:image/png;base64,iVBORw0K') + # ensure invalid image raises + with self.assertRaises(UserError): + record.write({ + 'image': 'invalid image', + }) + 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 f753371719a..fdb56fc898a 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -2022,6 +2022,7 @@ class Image(Binary): _slots = { 'max_width': 0, 'max_height': 0, + 'verify_resolution': True, } def create(self, record_values): @@ -2042,13 +2043,19 @@ class Image(Binary): 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): - value = image_process(value, size=(self.max_width, self.max_height)) - return value + return image_process(value, + size=(self.max_width, self.max_height), + verify_resolution=self.verify_resolution, + ) def _process_related(self, value): """Override to resize the related value before saving it on self.""" - return self._image_process(super()._process_related(value)) + try: + return self._image_process(super()._process_related(value)) + except UserError: + # Avoid the following `write` to fail if the related image was saved + # invalid, which can happen for pre-existing databases. + return False class Selection(Field):