[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
This commit is contained in:
Sébastien Theys
2019-08-02 16:47:58 +00:00
parent 58a2ffa26f
commit 1b85ab93ec
7 changed files with 38 additions and 33 deletions
+5 -6
View File
@@ -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."""
+8 -1
View File
@@ -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
+8 -1
View File
@@ -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:
@@ -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)
+1 -1
View File
@@ -1724,7 +1724,7 @@
<div t-if="product_image._name == 'product.image' and product_image.embed_code" class="d-flex align-items-center justify-content-center h-100 embed-responsive embed-responsive-16by9">
<t t-raw="product_image.embed_code"/>
</div>
<div t-else="" t-field="product_image.image_1920" class="d-flex align-items-center justify-content-center h-100" t-options='{"widget": "image", "preview_image": "image_1024", "class": "product_detail_img mh-100", "alt-field": "name", "zoom": product_image.can_image_be_zoomed and "image_1920"}'/>
<div t-else="" t-field="product_image.image_1920" class="d-flex align-items-center justify-content-center h-100" t-options='{"widget": "image", "preview_image": "image_1024", "class": "product_detail_img mh-100", "alt-field": "name", "zoom": product_image.can_image_1024_be_zoomed and "image_1920"}'/>
</div>
</t>
</div>
+1 -9
View File
@@ -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)
+8 -8
View File
@@ -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):