[REF] product: Generalise the setting of variant fields

Extract the way images are set on a product into a new method to
allows having the same behavior with other variant fields.

Now also used to set an Intrastat code varying from the template one.

Task id #2369561

closes odoo/odoo#65348

Related: odoo/enterprise#15099
Related: odoo/upgrade#1996
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Nicolas (vin)
2021-03-02 15:08:25 +00:00
parent 2f3978bab7
commit fdd93cbb76
+21 -18
View File
@@ -154,30 +154,33 @@ class ProductProduct(models.Model):
for record in self:
record.can_image_variant_1024_be_zoomed = record.image_variant_1920 and tools.is_image_size_above(record.image_variant_1920, record.image_variant_1024)
def _set_template_field(self, template_field, variant_field):
for record in self:
if (
# We are trying to remove a field from the variant even though it is already
# not set on the variant, remove it from the template instead.
(not record[template_field] and not record[variant_field])
# We are trying to add a field to the variant, but the template field is
# not set, write on the template instead.
or (record[template_field] and not record.product_tmpl_id[template_field])
# There is only one variant, always write on the template.
or self.search_count([
('product_tmpl_id', '=', record.product_tmpl_id.id),
('active', '=', True),
]) <= 1
):
record[variant_field] = False
record.product_tmpl_id[template_field] = record[template_field]
else:
record[variant_field] = record[template_field]
def _compute_image_1920(self):
"""Get the image from the template if no image is set on the variant."""
for record in self:
record.image_1920 = record.image_variant_1920 or record.product_tmpl_id.image_1920
def _set_image_1920(self):
for record in self:
if (
# We are trying to remove an image even though it is already
# not set, remove it from the template instead.
not record.image_1920 and not record.image_variant_1920 or
# We are trying to add an image, but the template image is
# not set, write on the template instead.
record.image_1920 and not record.product_tmpl_id.image_1920 or
# There is only one variant, always write on the template.
self.search_count([
('product_tmpl_id', '=', record.product_tmpl_id.id),
('active', '=', True),
]) <= 1
):
record.image_variant_1920 = False
record.product_tmpl_id.image_1920 = record.image_1920
else:
record.image_variant_1920 = record.image_1920
return self._set_template_field('image_1920', 'image_variant_1920')
def _compute_image_1024(self):
"""Get the image from the template if no image is set on the variant."""