Skip to content

Commit

Permalink
shopinvader_product_sale_packaging: fix empty barcode
Browse files Browse the repository at this point in the history
Never fail when a packaging has no barcode.
No matter where the data is coming from or how the object is
initialized.
  • Loading branch information
simahawk committed Mar 25, 2024
1 parent 1b50cce commit 66b0fb4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion shopinvader_product_sale_packaging/schemas/product_packaging.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 typing import Any

from odoo.addons.extendable_fastapi import StrictExtendableBaseModel
from odoo.addons.stock_packaging_calculator.models.product import Packaging

Expand All @@ -13,6 +15,12 @@ class SimpleProductPackaging(StrictExtendableBaseModel):
is_unit: bool
can_be_sold: bool

def __init__(self, /, **data: Any) -> None: # type: ignore
# Ensure `barcode` is set to None when empty, no matter if we use `from_packaging`.
if "barcode" in data and not data["barcode"]:
data["barcode"] = None
super().__init__(**data)

@classmethod
def from_packaging(cls, odoo_product, packaging, packaging_contained_mapping=None):
# packaging is a either a namedtuple of type Packaging, or a dict with the same keys
Expand Down Expand Up @@ -50,7 +58,7 @@ def from_packaging(cls, odoo_product, packaging, packaging_contained_mapping=Non
pkg["id"],
pkg["name"],
pkg["qty"],
pkg["barcode"] or None,
pkg["barcode"],
pkg["is_unit"],
),
)
Expand Down
7 changes: 7 additions & 0 deletions shopinvader_product_sale_packaging/tests/test_product_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def test_product_schema(self):
default_packaging = self._default_packaging()
self.assertEqual(res.packaging, default_packaging)

def test_product_schema_no_barcode(self):
self.pkg_pallet.barcode = False
res = ProductProduct.from_product_product(self.product_a)
self.assertEqual(len(res.packaging), 4)
default_packaging = self._default_packaging()
self.assertEqual(res.packaging, default_packaging)

def test_shopinvader_display(self):
self.pkg_big_box.shopinvader_display = False
res = ProductProduct.from_product_product(self.product_a)
Expand Down

0 comments on commit 66b0fb4

Please sign in to comment.