-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shopinvader_product_media: fix active media exclusion
Inactive media should be excluded from exported data. NOTE: this is probably needed on image relations too ;)
- Loading branch information
1 parent
0b031e8
commit 72bf132
Showing
9 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import shopinvader_variant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_variant_media |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
col1;col2;col3 | ||
1;2;3 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |