diff --git a/odoo/addons/base/tests/test_image.py b/odoo/addons/base/tests/test_image.py index c5d55b4eee47c0608d6950b9d02a5d239b74135d..1b92a829e35846e35d674e702592339d80df8ea5 100644 --- a/odoo/addons/base/tests/test_image.py +++ b/odoo/addons/base/tests/test_image.py @@ -285,6 +285,31 @@ class TestImage(TransactionCase): """Test that image_data_uri is working as expected.""" self.assertEqual(tools.image_data_uri(self.base64_1x1_png), 'data:image/png;base64,' + self.base64_1x1_png.decode('ascii')) + def test_21_image_guess_size_from_field_name(self): + f = tools.image_guess_size_from_field_name + # Test case: empty field_name input + self.assertEqual(f(''), (0, 0)) + # Test case: custom field_name input + self.assertEqual(f('custom_field'), (0, 0)) + # Test case: field_name input that starts with 'x_' + self.assertEqual(f('x_field'), (0, 0)) + # Test case: field_name input that starts with 'x_' and ends with a number less than 16 + self.assertEqual(f('x_studio_image_1'), (0, 0)) + # Test case: field_name input that starts with 'x_' and ends with a number greater than 16 + self.assertEqual(f('x_studio_image_32'), (0, 0)) + # Test case: field_name input that has a suffix less than 16 + self.assertEqual(f('image_15'), (0, 0)) + # Test case: field_name input that has a suffix equal to 16 + self.assertEqual(f('image_16'), (16, 16)) + # Test case: field_name input that has a suffix greater than 16 + self.assertEqual(f('image_32'), (32, 32)) + # Test case: field_name input that has a suffix with 2 numbers + self.assertEqual(f('image_1920_1080'), (1080, 1080)) + # Test case: field_name input that has a float as suffix + self.assertEqual(f('image_32.5'), (0, 0)) + # Test case: field_name input that has a suffix greater than 16 but no underscore + self.assertEqual(f('image32'), (0, 0)) + def _assertAlmostEqualSequence(self, rgb1, rgb2, delta=10): self.assertEqual(len(rgb1), len(rgb2)) for index, t in enumerate(zip(rgb1, rgb2)): diff --git a/odoo/tools/image.py b/odoo/tools/image.py index 18380d1202d073a3fb5c515a1f4ff72a76defc3b..e70303bba1ea93f5e0c0b9a326b87ed1e0b4911b 100644 --- a/odoo/tools/image.py +++ b/odoo/tools/image.py @@ -498,20 +498,27 @@ def is_image_size_above(base64_source_1, base64_source_2): def image_guess_size_from_field_name(field_name): """Attempt to guess the image size based on `field_name`. - If it can't be guessed, return (0, 0) instead. - - :param field_name: the name of a field - :type field_name: string + If it can't be guessed or if it is a custom field: return (0, 0) instead. + :param str field_name: the name of a field :return: the guessed size :rtype: tuple (width, height) """ - suffix = '1024' if field_name == 'image' else field_name.split('_')[-1] + if field_name == 'image': + return (1024, 1024) + if field_name.startswith('x_'): + return (0, 0) try: - return (int(suffix), int(suffix)) + suffix = int(field_name.split('_')[-1]) except ValueError: return (0, 0) + if suffix < 16: + # If the suffix is less than 16, it's probably not the size + return (0, 0) + + return (suffix, suffix) + def image_data_uri(base64_source): """This returns data URL scheme according RFC 2397