Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] [FIX]multi_image/multi_media and thumbnail orders #282

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions fs_file/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Fs File
=======

..
..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
Expand Down Expand Up @@ -158,7 +158,7 @@ Changelog
- Add a *url_path* property on the *FSFileValue* object. This property
allows you to easily get access to the relative path of the file on
the filesystem. This value is only available if the filesystem storage
is configured with a *Base URL* value. (`#281 <https://github.com/OCA/storage/issues/281>`_)
is configured with a *Base URL* value. (`#281 <https://github.com/OCA/storage/issues/281>`__)


**Bugfixes**
Expand All @@ -167,7 +167,7 @@ Changelog
object return *None* if the information is not available (instead of *False*).

The *url* property on the *FSFileValue* object returns the filesystem url nor
the url field of the attachment. (`#281 <https://github.com/OCA/storage/issues/281>`_)
the url field of the attachment. (`#281 <https://github.com/OCA/storage/issues/281>`__)

Bug Tracker
===========
Expand Down Expand Up @@ -212,7 +212,7 @@ promote its widespread use.

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-lmignon|
|maintainer-lmignon|

This module is part of the `OCA/storage <https://github.com/OCA/storage/tree/16.0/fs_file>`_ project on GitHub.

Expand Down
4 changes: 2 additions & 2 deletions fs_file/readme/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Add a *url_path* property on the *FSFileValue* object. This property
allows you to easily get access to the relative path of the file on
the filesystem. This value is only available if the filesystem storage
is configured with a *Base URL* value. (`#281 <https://github.com/OCA/storage/issues/281>`_)
is configured with a *Base URL* value. (`#281 <https://github.com/OCA/storage/issues/281>`__)


**Bugfixes**
Expand All @@ -15,4 +15,4 @@
object return *None* if the information is not available (instead of *False*).

The *url* property on the *FSFileValue* object returns the filesystem url nor
the url field of the attachment. (`#281 <https://github.com/OCA/storage/issues/281>`_)
the url field of the attachment. (`#281 <https://github.com/OCA/storage/issues/281>`__)
11 changes: 7 additions & 4 deletions fs_image_thumbnail/models/fs_image_thumbnail_mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from collections import OrderedDict

from slugify import slugify

from odoo import _, api, fields, models
Expand Down Expand Up @@ -186,21 +188,22 @@ def get_or_create_thumbnails(
*images: tuple[FSImageValue],
sizes: list[tuple[int, int]],
base_name: str = ""
) -> list["FsImageThumbnailMixin"]:
) -> OrderedDict[FSImageValue, list["FsImageThumbnailMixin"]]:
"""Get or create a thumbnail images from the given image.

:param images: the list of images we want to get or create thumbnails
:param sizes: the list of sizes to use to resize the image
(list of tuple (size_x, size_y))
:param base_name: the base name of the thumbnail image (without extension)
The base name must be set when multiple images are given.
:return: a dictionary where the key is the original image and the value is
a recordset of thumbnail images
:return: an ordered dictionary where the key is the original image and
the value is a recordset of thumbnail images. The order of the dict
is the order of the images passed to the method.
"""
base_name = self._get_slugified_base_name(*images, base_name=base_name)
thumbnails = self.get_thumbnails(*images, base_name=base_name)
thumbnails_by_attachment_id = thumbnails.partition("attachment_id")
ret = {}
ret = OrderedDict[FSImageValue, list["FsImageThumbnailMixin"]]()
for image in images:
thumbnails_by_size = {
(thumbnail.size_x, thumbnail.size_y): thumbnail
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions fs_image_thumbnail/readme/newsfragments/282.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The call to the method *get_or_create_thumbnails* on the *fs.image.thumbnail.mixin*
class returns now an ordered dictionary where the key is the original image and
the value is a recordset of thumbnail images. The order of the dict is the order
of the images passed to the method. This ensures that when you process the result
of the method you can be sure that the order of the images is the same as the
order of the images passed to the method.
14 changes: 6 additions & 8 deletions fs_product_multi_image/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ class ProductProduct(models.Model):
)
def _compute_variant_image_ids(self):
for variant in self:
img_relations = set()
# Not sure sorting is needed here
sorted_image_relations = variant.image_ids.sorted(
key=lambda i: (i.sequence, i.id)
variant_image_ids = variant.image_ids.filtered(
lambda i: i._match_variant(variant)
)
for image_rel in sorted_image_relations:
if image_rel._match_variant(variant):
img_relations.add(image_rel.id)
variant.variant_image_ids = list(img_relations) if img_relations else False
variant_image_ids = variant_image_ids.sorted(
key=lambda i: (i.sequence, i.name)
)
variant.variant_image_ids = variant_image_ids

@api.depends("variant_image_ids", "variant_image_ids.sequence")
def _compute_main_image_id(self):
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions fs_product_multi_image/readme/newsfragments/282.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensures the variant_image_ids are sorted by sequence and name. Before this
change, the order was random and could change between runs.
17 changes: 17 additions & 0 deletions fs_product_multi_image/tests/test_fs_product_multi_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ def test_add_image_for_white_and_black_variant(self):
# Black product should have the black image and the logo
self.assertEqual(self.product_b.variant_image_ids, image_bk + logo)

def test_image_variant_sequence(self):
logo, image_wh, image_bk = self._create_multiple_images()
# White product should have the white image and the logo
self.assertEqual(self.product_a.variant_image_ids, image_wh + logo)
# white product should have images sorted by sequence
self.assertListEqual(
self.product_a.variant_image_ids.mapped("sequence"),
[image_wh.sequence, logo.sequence],
)
# change sequence
image_wh.sequence = 20
logo.sequence = 10
self.assertListEqual(
self.product_a.variant_image_ids.mapped("sequence"),
[logo.sequence, image_wh.sequence],
)

def _test_main_images(self, expected):
for image, products in expected:
for prod in products:
Expand Down
14 changes: 5 additions & 9 deletions fs_product_multi_media/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ class ProductProduct(models.Model):
)
def _compute_variant_media_ids(self):
for variant in self:
media_relations = set()
# Not sure sorting is needed here
sorted_media_relations = variant.media_ids.sorted(
key=lambda i: (i.sequence, i.id)
variant_media_ids = variant.media_ids.filtered(
lambda i: i._match_variant(variant)
)
for media_rel in sorted_media_relations:
if media_rel._match_variant(variant):
media_relations.add(media_rel.id)
variant.variant_media_ids = (
list(media_relations) if media_relations else False
variant_media_ids = variant_media_ids.sorted(
key=lambda i: (i.sequence, i.name)
)
variant.variant_media_ids = variant_media_ids
Empty file.
2 changes: 2 additions & 0 deletions fs_product_multi_media/readme/newsfragments/282.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensures the variant_media_ids are sorted by sequence and name. Before this
change, the order was random and could change between runs.
17 changes: 17 additions & 0 deletions fs_product_multi_media/tests/test_fs_product_multi_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ def test_add_media_for_white_and_black_variant(self):
# Black product should have the media_b and the media_c
self.assertEqual(self.product_b.variant_media_ids, media_b + media_c)

def test_media_variant_sequence(self):
media_c, media_a, media_b = self._create_multiple_medias()
# White product should have the media_a and the media_c
self.assertEqual(self.product_a.variant_media_ids, media_a + media_c)
# white product should have medias sorted by sequence
self.assertListEqual(
self.product_a.variant_media_ids.mapped("sequence"),
[media_a.sequence, media_c.sequence],
)
# change sequence
media_c.sequence = 10
media_a.sequence = 20
self.assertListEqual(
self.product_a.variant_media_ids.mapped("sequence"),
[media_c.sequence, media_a.sequence],
)

def test_drop_template_attribute_value_propagation_to_media(self):
media_content_b = self.env["fs.product.media"].create(
{
Expand Down
Loading