Skip to content
Snippets Groups Projects
Commit 38198566 authored by Julien (jula)'s avatar Julien (jula)
Browse files

[FIX] tools: image fields size guess (backport)

Backport: https://github.com/odoo/odoo/pull/117343



__Current behavior before PR:__
The size of an image field is guessed using the field name. For instance, an image field with `field_name = "XXXX_123"` is resized to 123 pixels when fetched. This is can be an issue if a user creates an image field using studio in a form view. If the user sets the label of the field as "Image 1", the technical name will become `x_studio_image_1`. Therefore, the image field will be resized to 1 pixel width.

Note that in version anterior to 16, only the placeholder is resized since `image_guess_size_from_field_name` is never called when there is an actual image.

__Description of the fix:__
Refactor the `image_guess_size_from_field_name` method to return `(0, 0)` when the field name starts with `x_`.

closes odoo/odoo#118963

X-original-commit: c1d7eab2
Signed-off-by: default avatarSébastien Theys (seb) <seb@odoo.com>
parent 37538c87
Branches
Tags
No related merge requests found
......@@ -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)):
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment