[FIX] website_event_track: resolve issue with setting SVG as favicon

Steps to Produce:-
- Install `Advanced Events`
- Go to ` website` then `Configuration`
- Open `setting`
- Then change favicon and Select svg type file and click on save
- Traceback is here

Cause :-
- The traceback occurs when an SVG image is passed to the ImageProcess method.
 In this scenario, the method sets the image as false, and then attempts to
access its size, resulting in the traceback

Fix :-
- The issue has been resolved by implementing a condition check for the image
before accessing its properties. This ensures that the image is valid before
trying to retrieve its size, preventing the traceback from occurring.

sentry-4199175498

closes odoo/odoo#127958

X-original-commit: ebb1e89b367dced4a2157751de12a53bf41ae400
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Signed-off-by: Achraf Ben Azzouz (abz) <abz@odoo.com>
This commit is contained in:
Adesh Jolhe
2023-07-15 04:01:12 +02:00
committed by bit-odoo
parent 2d66c22a60
commit 8eda018910
2 changed files with 63 additions and 2 deletions
+5 -2
View File
@@ -42,12 +42,15 @@ class Website(models.Model):
def _compute_app_icon(self):
""" Computes a squared image based on the favicon to be used as mobile webapp icon.
App Icon should be in PNG format and size of at least 512x512.
If the favicon is an SVG image, it will be skipped and the app_icon will be set to False.
"""
for website in self:
if not website.favicon:
image = ImageProcess(base64.b64decode(website.favicon)) if website.favicon else None
if not (image and image.image):
website.app_icon = False
continue
image = ImageProcess(base64.b64decode(website.favicon))
w, h = image.image.size
square_size = w if w > h else h
image.crop_resize(square_size, square_size)
@@ -3,6 +3,10 @@
from odoo.tests import tagged
from odoo.tests.common import TransactionCase
from odoo import tools
from io import BytesIO
from PIL import Image
import base64
@tagged('post_install', '-at_install')
@@ -19,3 +23,57 @@ class TestWebsiteEvent(TransactionCase):
self.assertEqual(website2.events_app_name, 'Foo Events')
website2.write({'name': 'Bar'})
self.assertEqual(website2.events_app_name, 'Foo Events')
def test_compute_app_icon(self):
# Generate image data for JPEG
jpeg_image = Image.new('RGB', (60, 30), color=(73, 109, 137))
jpeg_io = BytesIO()
jpeg_image.save(jpeg_io, format='JPEG')
jpeg_image_data = jpeg_io.getvalue()
# Generate image data for JPG
jpg_image = Image.new('RGB', (60, 30), color=(73, 109, 137))
jpg_io = BytesIO()
jpg_image.save(jpg_io, format='JPEG')
jpg_image_data = jpg_io.getvalue()
# Generate image data for PNG
png_image = Image.new('RGB', (60, 30), color=(73, 109, 137))
png_io = BytesIO()
png_image.save(png_io, format='PNG')
png_image_data = png_io.getvalue()
# Generate image data for SVG
svg_image_data = b"""<svg xmlns="http://www.w3.org/2000/svg" width="60" height="30" version="1.1">
<rect width="100%" height="100%" fill="rgb(73, 109, 137)"/>
</svg>
"""
# Image data and their respective expected types
image_data = {
'png': png_image_data,
'jpg': jpg_image_data,
'jpeg': jpeg_image_data,
'svg': svg_image_data
}
for expected_type, image_data in image_data.items():
# Create a website record
website = self.env['website'].create({
'name': 'Test Website',
'favicon': base64.b64encode(image_data)
})
# Call the method to compute app_icon
website._compute_app_icon()
if expected_type in ['jpeg', 'png', 'jpg']:
# Check if app_icon is set
self.assertTrue(website.app_icon)
# Check if app_icon is a valid image
image = tools.base64_to_image(website.app_icon)
self.assertEqual(image.format.lower(), 'png')
else:
# For SVG images, ensure that the app_icon is not set
self.assertFalse(website.app_icon)