Debian Codegraph @

addons/website_sale/models/product_image.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import base64
4
5 from odoo import _, api, fields, models
6 from odoo.exceptions import ValidationError
7 from odoo.tools.image import is_image_size_above
8
9 from odoo.addons.html_editor.tools import get_video_embed_code, get_video_thumbnail
10
11
12 class ProductImage(models.Model):
13 _name = 'product.image'
14 _description = "Product Image"
15 _inherit = ['image.mixin']
16 _order = 'sequence, id'
17
18 name = fields.Char(string="Name", required=True)
19 sequence = fields.Integer(default=10)
20
21 image_1920 = fields.Image()
22
23 product_tmpl_id = fields.Many2one(
24 string="Product Template", comodel_name='product.template', ondelete='cascade', index=True,
25 )
26 product_variant_id = fields.Many2one(
27 string="Product Variant", comodel_name='product.product', ondelete='cascade', index=True,
28 )
29 video_url = fields.Char(
30 string="Video URL",
31 help="URL of a video for showcasing your product.",
32 )
33 embed_code = fields.Html(compute='_compute_embed_code', sanitize=False)
34
35 can_image_1024_be_zoomed = fields.Boolean(
36 string="Can Image 1024 be zoomed",
37 compute='_compute_can_image_1024_be_zoomed',
38 store=True,
39 )
40
41 #=== COMPUTE METHODS ===#
42
43 @api.depends('image_1920', 'image_1024')
44 def _compute_can_image_1024_be_zoomed(self):
45 for image in self:
46 image.can_image_1024_be_zoomed = image.image_1920 and is_image_size_above(image.image_1920, image.image_1024)
47
48 @api.depends('video_url')
49 def _compute_embed_code(self):
50 for image in self:
51 image.embed_code = image.video_url and get_video_embed_code(image.video_url) or False
52
53 #=== ONCHANGE METHODS ===#
54
55 @api.onchange('video_url')
56 def _onchange_video_url(self):
57 if not self.image_1920:
58 thumbnail = get_video_thumbnail(self.video_url)
59 self.image_1920 = thumbnail and base64.b64encode(thumbnail) or False
60
61 #=== CONSTRAINT METHODS ===#
62
63 @api.constrains('video_url')
64 def _check_valid_video_url(self):
65 for image in self:
66 if image.video_url and not image.embed_code:
67 raise ValidationError(_("Provided video URL for '%s' is not valid. Please enter a valid video URL.", image.name))
68
69 #=== CRUD METHODS ===#
70
71 @api.model_create_multi
72 def create(self, vals_list):
73 """
74 We don't want the default_product_tmpl_id from the context
75 to be applied if we have a product_variant_id set to avoid
76 having the variant images to show also as template images.
77 But we want it if we don't have a product_variant_id set.
78 """
79 context_without_template = self.with_context({k: v for k, v in self.env.context.items() if k != 'default_product_tmpl_id'})
80 normal_vals = []
81 variant_vals_list = []
82
83 for vals in vals_list:
84 if vals.get('product_variant_id') and 'default_product_tmpl_id' in self.env.context:
85 variant_vals_list.append(vals)
86 else:
87 normal_vals.append(vals)
88
89 return super().create(normal_vals) + super(ProductImage, context_without_template).create(variant_vals_list)