Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix mixing up attribute values because of the same slug value (#15984)
Krzysztof Kwaśniak4386f322f659
diff --git a/saleor/attribute/tests/test_utils.py b/saleor/attribute/tests/test_utils.pyindex 26edbe00a3..4fb576cd0c 100644--- a/saleor/attribute/tests/test_utils.py+++ b/saleor/attribute/tests/test_utils.py@@ -2,8 +2,11 @@ import pytestfrom ...attribute.models import AssignedPageAttributeValuefrom ...product.models import ProductType+from .. import AttributeInputType, AttributeType+from ..models import Attribute, AttributeValuefrom ..utils import (associate_attribute_values_to_instance,+ validate_attribute_owns_values,)from .model_helpers import (get_page_attribute_values,@@ -13,6 +16,38 @@ from .model_helpers import ()+def attribute_1():+ attr = Attribute.objects.create(+ slug="attribute-1",+ name="Attribute 1",+ input_type=AttributeInputType.DROPDOWN,+ type=AttributeType.PRODUCT_TYPE,+ )+ AttributeValue.objects.create(+ attribute=attr,+ name="Value 1",+ slug="value-1",+ )+ return attr+++def attribute_2():+ attr = Attribute.objects.create(+ slug="attribute-2",+ name="Attribute 2",+ input_type=AttributeInputType.DROPDOWN,+ type=AttributeType.PRODUCT_TYPE,+ )+ AttributeValue.objects.create(+ attribute=attr,+ name="Value 1",+ slug="value-1",+ )+ return attr++def test_associate_attribute_to_non_product_instance(color_attribute):instance = ProductType()attribute = color_attribute@@ -212,3 +247,20 @@ def test_associate_attribute_to_instance_duplicated_values((new_color_value.pk, product.id),(multiselect_value.pk, product.id),}+++def test_validate_attribute_owns_values(attribute_1, attribute_2):+ # given+ attr_val_map = {+ attribute_1.id: [attribute_1.values.first()],+ attribute_2.id: [attribute_2.values.first()],+ }++ # when+ validate_attribute_owns_values(attr_val_map)++ # then+ assert attr_val_map == {+ attribute_1.id: [attribute_1.values.first()],+ attribute_2.id: [attribute_2.values.first()],+ }diff --git a/saleor/attribute/utils.py b/saleor/attribute/utils.pyindex c3ee658524..0d6857fa91 100644--- a/saleor/attribute/utils.py+++ b/saleor/attribute/utils.py@@ -69,8 +69,9 @@ def validate_attribute_owns_values(attr_val_map: dict[int, list]) -> None:values = AttributeValue.objects.filter(lookup)for value in values:- values_map[value.attribute_id].add(value.slug)- slug_value_to_value_map[value.slug] = value+ attr_id = value.attribute_id+ values_map[attr_id].add(value.slug)+ slug_value_to_value_map[attr_id, value.slug] = valuefor attribute_id, attr_values in attr_val_map.items():if values_map[attribute_id] != {v.slug for v in attr_values}:@@ -79,7 +80,7 @@ def validate_attribute_owns_values(attr_val_map: dict[int, list]) -> None:# id set. This is needed as `ignore_conflicts=True` flag in `bulk_create# is used in `AttributeValueManager`attr_val_map[attribute_id] = [- slug_value_to_value_map[v.slug] for v in attr_values+ slug_value_to_value_map[attribute_id, v.slug] for v in attr_values]diff --git a/saleor/graphql/attribute/tests/test_utils.py b/saleor/graphql/attribute/tests/test_utils.pyindex fd6cc9bf9e..ada2934111 100644--- a/saleor/graphql/attribute/tests/test_utils.py+++ b/saleor/graphql/attribute/tests/test_utils.py@@ -7,6 +7,7 @@ from ....attribute import AttributeInputTypefrom ....attribute.models import AttributeValuefrom ....page.error_codes import PageErrorCodefrom ....product.error_codes import ProductErrorCode+from ..enums import AttributeValueBulkActionEnumfrom ..utils import (AttributeAssignmentMixin,AttrValuesForSelectableFieldInput,@@ -2165,3 +2166,34 @@ def test_prepare_attribute_values_that_gives_the_same_slug(color_attribute):assert result[0] == existing_valueassert result[1].name == new_valueassert result[2].name == new_value_2+++def test_attribute_assignment_mixin_pre_save_multiselect_external_reference_action(+ color_attribute,+):+ # given+ color_attribute.input_type = AttributeInputType.MULTISELECT+ color_attribute.save(update_fields=["input_type"])++ values = AttrValuesInput(+ global_id=graphene.Node.to_global_id("Attribute", color_attribute.pk),+ content_type=None,+ references=[],+ multiselect=[+ AttrValuesForSelectableFieldInput(+ external_reference=value.external_reference+ )+ for value in color_attribute.values.all()+ ],+ )++ # when+ result = AttributeAssignmentMixin._pre_save_multiselect_values(+ None, color_attribute, values+ )++ # then+ assert result == [+ (AttributeValueBulkActionEnum.NONE, value)+ for value in color_attribute.values.all()+ ]diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.pyindex ade2810d1b..b445053d7f 100644--- a/saleor/graphql/attribute/utils.py+++ b/saleor/graphql/attribute/utils.py@@ -565,7 +565,7 @@ class AttributeAssignmentMixin:raise ValidationError("Attribute value with given externalReference can't be found")- return value+ attribute_values.append(value)if attr_value.id:_, attr_value_id = from_global_id_or_error(attr_value.id)