Skip to content

Commit

Permalink
Merge PR #1526 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by lmignon
  • Loading branch information
shopinvader-git-bot committed Apr 4, 2024
2 parents 76a13b8 + c276854 commit bdd0bff
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions shopinvader_product_attribute_set/schemas/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ProductProduct(product.ProductProduct, extends=True):
attribute_set: ProductAttributeSet | None = None
attributes: dict[str, str | bool | int | list[str]] = {}
attributes: dict[str, str | bool | int | float | list[str]] = {}
structured_attributes: list[ProductAttributeGroup] = []

@classmethod
Expand All @@ -32,7 +32,7 @@ def from_product_product(
@classmethod
def _compute_attributes(
cls, record: product_product.ProductProduct
) -> dict[str, str | bool | int | list[str]]:
) -> dict[str, str | bool | int | float | list[str]]:
attributes = {}
for attr in record.attribute_set_id.attribute_ids:
# all attr start with "x_" we remove it for the export
Expand Down
13 changes: 9 additions & 4 deletions shopinvader_product_attribute_set/schemas/product_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from enum import Enum

import pydantic
from extendable_pydantic import StrictExtendableBaseModel

from odoo.addons.product.models.product_product import ProductProduct
Expand All @@ -23,13 +24,18 @@ class ProductAttributeType(Enum):
float = "float"
date = "date"
datetime = "datetime"
# TODO: I'm not sure this value is handled properly
# as product[attr.name] will return a b64 string, not a binary.
binary = "binary"


class ProductAttribute(StrictExtendableBaseModel):
name: str
key: str
value: str | bool | int | list[str]
# Use strict types to avoid opinionated conversion of original values
value: pydantic.StrictInt | pydantic.StrictStr | pydantic.StrictFloat | bool | list[
str
]
type: ProductAttributeType

@classmethod
Expand All @@ -38,7 +44,7 @@ def _get_value_for_attribute(
product: ProductProduct,
attr: AttributeAttribute,
string_mode: bool = False,
) -> str | bool | int | list[str]:
) -> str | bool | int | float | list[str]:
if attr.attribute_type == "select":
return product[attr.name].display_name or ""
elif attr.attribute_type == "multiselect":
Expand All @@ -47,8 +53,7 @@ def _get_value_for_attribute(
return product[attr.name] and "true" or "false"
elif string_mode or attr.attribute_type in ("char", "text"):
return "%s" % (product[attr.name] or "")
else:
return product[attr.name] or ""
return product[attr.name] or ""

@classmethod
def from_product_attribute(
Expand Down
49 changes: 49 additions & 0 deletions shopinvader_product_attribute_set/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,52 @@ def test_product_data_select_multi_select_attributes(self):
}
],
)

def test_product_data_numbers(self):
attribute_int = self._create_attribute(
{
"attribute_type": "integer",
}
)
attribute_float = self._create_attribute(
{
"attribute_type": "float",
}
)
self.attr_set.attribute_ids = attribute_int | attribute_float
self.product.write(
{
"attribute_set_id": self.attr_set.id,
"x_integer": 10,
"x_float": 20.0,
}
)
self.reset_extendable_registry()
self.init_extendable_registry()
res = ProductProduct.from_product_product(self.product).model_dump()
self.assertEqual(
res.get("attributes"),
{"integer": 10, "float": 20.0},
)
self.assertListEqual(
res.get("structured_attributes"),
[
{
"group_name": "My Group",
"fields": [
{
"value": "10",
"name": "Attribute integer",
"key": "integer",
"type": ProductAttributeType.integer,
},
{
"value": "20.0",
"name": "Attribute float",
"key": "float",
"type": ProductAttributeType.float,
},
],
}
],
)

0 comments on commit bdd0bff

Please sign in to comment.