summaryrefslogtreecommitdiff
path: root/addons/website_sale/models/product_image.py
diff options
context:
space:
mode:
Diffstat (limited to 'addons/website_sale/models/product_image.py')
-rw-r--r--addons/website_sale/models/product_image.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/addons/website_sale/models/product_image.py b/addons/website_sale/models/product_image.py
new file mode 100644
index 00000000..eea9afb6
--- /dev/null
+++ b/addons/website_sale/models/product_image.py
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, tools, _
+from odoo.exceptions import ValidationError
+
+from odoo.addons.website.tools import get_video_embed_code
+
+
+class ProductImage(models.Model):
+ _name = 'product.image'
+ _description = "Product Image"
+ _inherit = ['image.mixin']
+ _order = 'sequence, id'
+
+ name = fields.Char("Name", required=True)
+ sequence = fields.Integer(default=10, index=True)
+
+ image_1920 = fields.Image(required=True)
+
+ product_tmpl_id = fields.Many2one('product.template', "Product Template", index=True, ondelete='cascade')
+ product_variant_id = fields.Many2one('product.product', "Product Variant", index=True, ondelete='cascade')
+ video_url = fields.Char('Video URL',
+ 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:
+ image.embed_code = get_video_embed_code(image.video_url)
+
+ @api.constrains('video_url')
+ def _check_valid_video_url(self):
+ for image in self:
+ if image.video_url and not image.embed_code:
+ raise ValidationError(_("Provided video URL for '%s' is not valid. Please enter a valid video URL.", image.name))
+
+ @api.model_create_multi
+ def create(self, vals_list):
+ """
+ We don't want the default_product_tmpl_id from the context
+ to be applied if we have a product_variant_id set to avoid
+ having the variant images to show also as template images.
+ But we want it if we don't have a product_variant_id set.
+ """
+ context_without_template = self.with_context({k: v for k, v in self.env.context.items() if k != 'default_product_tmpl_id'})
+ normal_vals = []
+ variant_vals_list = []
+
+ for vals in vals_list:
+ if vals.get('product_variant_id') and 'default_product_tmpl_id' in self.env.context:
+ variant_vals_list.append(vals)
+ else:
+ normal_vals.append(vals)
+
+ return super().create(normal_vals) + super(ProductImage, context_without_template).create(variant_vals_list)