Skip to content

Commit

Permalink
shopinvader_product_media: fix active media exclusion
Browse files Browse the repository at this point in the history
Inactive media should be excluded from exported data.

NOTE: this is probably needed on image relations too ;)
  • Loading branch information
simahawk authored and mmequignon committed Nov 12, 2024
1 parent 0b031e8 commit 72bf132
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions shopinvader_product_media/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
1 change: 1 addition & 0 deletions shopinvader_product_media/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import shopinvader_variant
24 changes: 24 additions & 0 deletions shopinvader_product_media/models/shopinvader_variant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Camptocamp SA (http://www.camptocamp.com)
# @author Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import api, fields, models


class ShopinvaderVariant(models.Model):
_inherit = "shopinvader.variant"

variant_media_ids = fields.Many2many(
"product.media.relation",
compute="_compute_variant_media_ids",
)

@api.depends(
"record_id.variant_media_ids.media_id.active",
)
def _compute_variant_media_ids(self):
for variant in self:
medias = variant.record_id.variant_media_ids.filtered(
lambda m: m.media_id.filtered("active")
)
variant.variant_media_ids = [(6, 0, medias.ids)]
1 change: 1 addition & 0 deletions shopinvader_product_media/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_variant_media
25 changes: 25 additions & 0 deletions shopinvader_product_media/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Camptocamp SA (http://www.camptocamp.com)
# @author Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import base64

from odoo.tools import file_open


class Mixin:
@staticmethod
def _get_file_content(
filename, base_path=None, as_binary=False, module="shopinvader_product_media"
):
with file_open("%s/tests/fixture/%s" % (module, filename), "rb") as f:
data = f.read()
if as_binary:
return data
return base64.b64encode(data)

@classmethod
def _create_storage_media(cls, name):
return cls.env["storage.media"].create(
{"name": name, "data": cls._get_file_content(name)}
)
2 changes: 2 additions & 0 deletions shopinvader_product_media/tests/fixture/test-media.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
col1;col2;col3
1;2;3
Binary file not shown.
1 change: 1 addition & 0 deletions shopinvader_product_media/tests/fixture/test-media.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
51 changes: 51 additions & 0 deletions shopinvader_product_media/tests/test_variant_media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Camptocamp SA (http://www.camptocamp.com)
# @author Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).


from odoo.tests import SavepointCase

from .common import Mixin


class ProductMediaCase(SavepointCase, Mixin):
@classmethod
def setUpClass(cls):
super().setUpClass()
lang = cls.env.ref("base.lang_en")
cls.template = cls.env.ref("product.product_product_4_product_template")
cls.s_template = cls.env["shopinvader.product"].create(
{"record_id": cls.template.id, "lang_id": lang.id}
)
cls.product_a = cls.env.ref("product.product_product_4")
cls.product_b = cls.env.ref("product.product_product_4b")
cls.product_c = cls.env.ref("product.product_product_4c")
cls.media1 = cls._create_storage_media("test-media.pdf")
cls.media2 = cls._create_storage_media("test-media.txt")
cls.media3 = cls._create_storage_media("test-media.csv")
cls.s_variant = cls.env["shopinvader.variant"].create(
{
"record_id": cls.product_a.id,
"shopinvader_product_id": cls.s_template.id,
"lang_id": lang.id,
}
)

def test_availability(self):
self.assertEqual(len(self.s_variant.variant_media_ids), 0)
rel1 = self.env["product.media.relation"].create(
{"product_tmpl_id": self.template.id, "media_id": self.media1.id}
)
self.assertEqual(self.s_variant.variant_media_ids, rel1)
rel2 = self.env["product.media.relation"].create(
{"product_tmpl_id": self.template.id, "media_id": self.media2.id}
)
self.assertEqual(self.s_variant.variant_media_ids, rel1 | rel2)
rel3 = self.env["product.media.relation"].create(
{"product_tmpl_id": self.template.id, "media_id": self.media3.id}
)
self.assertEqual(self.s_variant.variant_media_ids, rel1 | rel2 | rel3)
# Setting a media as inactive should exclude related relations
# on products
rel1.media_id.active = False
self.assertEqual(self.s_variant.variant_media_ids, rel2 | rel3)

0 comments on commit 72bf132

Please sign in to comment.