Skip to content

Commit

Permalink
Merge PR #115 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by sebastienbeau
  • Loading branch information
OCA-git-bot committed Feb 13, 2022
2 parents 56d9197 + 64694ea commit 21aa51a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions storage_image_product/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ def _compute_main_image_id(self):
record.main_image_id = record._get_main_image()

def _get_main_image(self):
match_image = self.variant_image_ids.filtered(
lambda i: i.attribute_value_ids
== self.mapped(
"product_template_attribute_value_ids.product_attribute_value_id"
)
)
if match_image:
return fields.first(
match_image.sorted(key=lambda i: (i.sequence, i.image_id))
).image_id
return fields.first(
self.variant_image_ids.sorted(key=lambda i: (i.sequence, i.image_id))
).image_id
36 changes: 36 additions & 0 deletions storage_image_product/tests/test_product_image_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,39 @@ def test_main_image_and_urls(self):
(self.logo_image, self.product_b),
)
self._test_main_images_and_urls(expected)

def test_main_image_attribute(self):
"""
Attach the image to the template and check the first image of the
variant is the one with same attributes
"""
self.env["product.image.relation"].create(
{
"product_tmpl_id": self.template.id,
"image_id": self.logo_image.id,
"sequence": 1,
}
)
self.env["product.image.relation"].create(
{
"product_tmpl_id": self.template.id,
"image_id": self.white_image.id,
"attribute_value_ids": [
(
6,
0,
[
self.env.ref("product.product_attribute_value_4").id,
self.env.ref("product.product_attribute_value_1").id,
],
)
],
"sequence": 10,
}
)
# The variant should not take the only with the lowest sequence but
# the one with same attributes
expected = ((self.white_image, self.product_b),)
self._test_main_images_and_urls(expected)
expected = ((self.logo_image, self.product_c + self.product_a),)
self._test_main_images_and_urls(expected)

0 comments on commit 21aa51a

Please sign in to comment.