From 8eda018910d6eccdf23a23ab8bf76ce8704fd33e Mon Sep 17 00:00:00 2001 From: Adesh Jolhe Date: Wed, 24 May 2023 13:02:51 +0000 Subject: [PATCH] [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) Signed-off-by: Achraf Ben Azzouz (abz) --- addons/website_event_track/models/website.py | 7 ++- .../tests/test_website_event.py | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/addons/website_event_track/models/website.py b/addons/website_event_track/models/website.py index c95f8e3a806..92e26048ae2 100644 --- a/addons/website_event_track/models/website.py +++ b/addons/website_event_track/models/website.py @@ -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) diff --git a/addons/website_event_track/tests/test_website_event.py b/addons/website_event_track/tests/test_website_event.py index 20f92538149..65cd5aaee6c 100644 --- a/addons/website_event_track/tests/test_website_event.py +++ b/addons/website_event_track/tests/test_website_event.py @@ -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""" + + + """ + # 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)