From 1b85ab93ec647ed723dd72d11df7b3c8283d5f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Thu, 1 Aug 2019 11:20:36 +0000 Subject: [PATCH] [IMP] base, product, website_sale: move image zoom out of mixin This field is not necessary on most models so move it to the few places where it is needed to avoid computing it and storing it everywhere. PR: #34925 --- addons/product/models/product.py | 11 +++++------ addons/product/models/product_template.py | 9 ++++++++- addons/website_sale/models/product_image.py | 9 ++++++++- .../tests/test_website_sale_image.py | 14 +++++++------- addons/website_sale/views/templates.xml | 2 +- odoo/addons/base/models/image_mixin.py | 10 +--------- odoo/tools/image.py | 16 ++++++++-------- 7 files changed, 38 insertions(+), 33 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index ccefd82c3f3..400b434f775 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -131,7 +131,7 @@ class ProductProduct(models.Model): image_variant_256 = fields.Image("Variant Image 256", related="image_variant_max", max_width=256, max_height=256, store=True) image_variant_128 = fields.Image("Variant Image 128", related="image_variant_max", max_width=128, max_height=128, store=True) image_variant_64 = fields.Image("Variant Image 64", related="image_variant_max", max_width=64, max_height=64, store=True) - can_image_variant_be_zoomed = fields.Boolean("Can image variant be zoomed", compute='_compute_images', store=True) + can_image_variant_1024_be_zoomed = fields.Boolean("Can Variant Image 1024 be zoomed", compute='_compute_can_image_variant_1024_be_zoomed', store=True) # Computed fields that are used to create a fallback to the template if # necessary, it's recommended to display those fields to the user. @@ -141,13 +141,12 @@ class ProductProduct(models.Model): image_256 = fields.Image("Image 256", compute='_compute_image_256') image_128 = fields.Image("Image 128", compute='_compute_image_128') image_64 = fields.Image("Image 64", compute='_compute_image_64') - can_image_1024_be_zoomed = fields.Boolean("Can image be zoomed", compute='_compute_can_image_1024_be_zoomed') + can_image_1024_be_zoomed = fields.Boolean("Can Image 1024 be zoomed", compute='_compute_can_image_1024_be_zoomed') - @api.depends('image_variant_max') - def _compute_images(self): + @api.depends('image_variant_max', 'image_variant_1024') + def _compute_can_image_variant_1024_be_zoomed(self): for record in self: - image = record.image_variant_max - record.can_image_variant_be_zoomed = image and tools.is_image_size_above(image) + record.can_image_variant_1024_be_zoomed = record.image_variant_max and tools.is_image_size_above(record.image_variant_max, record.image_variant_1024) def _compute_image_1920(self): """Get the image from the template if no image is set on the variant.""" diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 1741c7ebe7f..101ed0c4039 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -149,6 +149,13 @@ class ProductTemplate(models.Model): item_ids = fields.One2many('product.pricelist.item', 'product_tmpl_id', 'Pricelist Items') + can_image_1024_be_zoomed = fields.Boolean("Can Image 1024 be zoomed", compute='_compute_can_image_1024_be_zoomed', store=True) + + @api.depends('image_1920', 'image_1024') + def _compute_can_image_1024_be_zoomed(self): + for template in self: + template.can_image_1024_be_zoomed = template.image_1920 and tools.is_image_size_above(template.image_1920, template.image_1024) + @api.depends('product_variant_ids') def _compute_product_variant_id(self): for p in self: @@ -379,7 +386,7 @@ class ProductTemplate(models.Model): 'image_256', 'image_128', 'image_64', - 'can_image_be_zoomed', + 'can_image_1024_be_zoomed', ]) return res diff --git a/addons/website_sale/models/product_image.py b/addons/website_sale/models/product_image.py index eafb03b9ace..562ebe79854 100644 --- a/addons/website_sale/models/product_image.py +++ b/addons/website_sale/models/product_image.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models, _ +from odoo import api, fields, models, tools, _ from odoo.exceptions import ValidationError from odoo.addons.website.tools import get_video_embed_code @@ -24,6 +24,13 @@ class ProductImage(models.Model): help='URL of a video for showcasing your product.') embed_code = fields.Char(compute="_compute_embed_code") + can_image_1024_be_zoomed = fields.Boolean("Can Image 1024 be zoomed", compute='_compute_can_image_1024_be_zoomed', store=True) + + @api.depends('image_1920', 'image_1024') + def _compute_can_image_1024_be_zoomed(self): + for image in self: + image.can_image_1024_be_zoomed = image.image_1920 and tools.is_image_size_above(image.image_1920, image.image_1024) + @api.depends('video_url') def _compute_embed_code(self): for image in self: diff --git a/addons/website_sale/tests/test_website_sale_image.py b/addons/website_sale/tests/test_website_sale_image.py index 8b8b233ea99..63342ce9f12 100644 --- a/addons/website_sale/tests/test_website_sale_image.py +++ b/addons/website_sale/tests/test_website_sale_image.py @@ -132,13 +132,13 @@ class TestWebsiteSaleImage(odoo.tests.HttpCase): product_red.image_1920 = red_image # Verify image_1920 size > 1024 can be zoomed - self.assertTrue(template.can_image_be_zoomed) - self.assertFalse(template.product_template_image_ids[0].can_image_be_zoomed) - self.assertFalse(template.product_template_image_ids[1].can_image_be_zoomed) - self.assertFalse(product_red.can_image_be_zoomed) - self.assertFalse(product_red.product_variant_image_ids[0].can_image_be_zoomed) - self.assertTrue(product_green.can_image_be_zoomed) - self.assertTrue(product_green.product_variant_image_ids[0].can_image_be_zoomed) + self.assertTrue(template.can_image_1024_be_zoomed) + self.assertFalse(template.product_template_image_ids[0].can_image_1024_be_zoomed) + self.assertFalse(template.product_template_image_ids[1].can_image_1024_be_zoomed) + self.assertFalse(product_red.can_image_1024_be_zoomed) + self.assertFalse(product_red.product_variant_image_ids[0].can_image_1024_be_zoomed) + self.assertTrue(product_green.can_image_1024_be_zoomed) + self.assertTrue(product_green.product_variant_image_ids[0].can_image_1024_be_zoomed) # jpeg encoding is changing the color a bit jpeg_blue = (65, 105, 227) diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index f1fffe54dbe..0e32d3c58c9 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -1724,7 +1724,7 @@
-
+
diff --git a/odoo/addons/base/models/image_mixin.py b/odoo/addons/base/models/image_mixin.py index d7611c895ff..e8a04cde5dc 100644 --- a/odoo/addons/base/models/image_mixin.py +++ b/odoo/addons/base/models/image_mixin.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import models, fields, api, tools +from odoo import models, fields class ImageMixin(models.AbstractModel): @@ -18,11 +18,3 @@ class ImageMixin(models.AbstractModel): image_256 = fields.Image("Image 256", related="image_1920", max_width=256, max_height=256, store=True) image_128 = fields.Image("Image 128", related="image_1920", max_width=128, max_height=128, store=True) image_64 = fields.Image("Image 64", related="image_1920", max_width=64, max_height=64, store=True) - - can_image_be_zoomed = fields.Boolean("Can image raw be zoomed", compute='_compute_images', store=True) - - @api.depends('image_1920') - def _compute_images(self): - for record in self: - image = record.image_1920 - record.can_image_be_zoomed = image and tools.is_image_size_above(image) diff --git a/odoo/tools/image.py b/odoo/tools/image.py index 77232fda658..2786f1f25fb 100644 --- a/odoo/tools/image.py +++ b/odoo/tools/image.py @@ -362,18 +362,18 @@ def image_to_base64(image, format, **params): return base64.b64encode(stream.getvalue()) -def is_image_size_above(base64_source, size=(1024, 1024)): - """Return whether or not the size of the given image `base64_source` is - above the provided `size` (tuple: width, height). +def is_image_size_above(base64_source_1, base64_source_2): + """Return whether or not the size of the given image `base64_source_1` is + above the size of the given image `base64_source_2`. """ - if not base64_source: + if not base64_source_1 or not base64_source_2: return False - if base64_source[:1] in (b'P', 'P'): + if base64_source_1[:1] in (b'P', 'P') or base64_source_2[:1] in (b'P', 'P'): # False for SVG return False - image = base64_to_image(base64_source) - width, height = image.size - return width > size[0] or height > size[1] + image_source = base64_to_image(base64_source_1) + image_target = base64_to_image(base64_source_2) + return image_source.width > image_target.width or image_source.height > image_target.height def image_guess_size_from_field_name(field_name):