Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix str object has no attribute input type error (#9345)
l3str4nge34a18d6bb242
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 2908f86bad..69cec23144 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -930,13 +930,28 @@ class ProductVariantCreate(ModelMutation):cleaned_input["product"])+ variant_attributes_ids = {+ graphene.Node.to_global_id("Attribute", attr_id)+ for attr_id in list(+ product_type.variant_attributes.all().values_list("pk", flat=True)+ )+ }+ attributes = cleaned_input.get("attributes")+ attributes_ids = {attr["id"] for attr in attributes or []}+ invalid_attributes = attributes_ids - variant_attributes_ids+ if len(invalid_attributes) > 0:+ raise ValidationError(+ "Given attributes are not a variant attributes.",+ code=ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.value,+ params={"attributes": invalid_attributes},+ )+# Run the validation only if product type is configurableif product_type.has_variants:# Attributes are provided as list of `AttributeValueInput` objects.# We need to transform them into the format they're stored in the# `Product` model, which is HStore field that maps attribute's PK to# the value's PK.- attributes = cleaned_input.get("attributes")try:if attributes:cleaned_attributes = cls.clean_attributes(attributes, product_type)@@ -956,6 +971,12 @@ class ProductVariantCreate(ModelMutation):)except ValidationError as exc:raise ValidationError({"attributes": exc})+ else:+ if attributes:+ raise ValidationError(+ "Cannot assign attributes for product type without variants",+ ProductErrorCode.INVALID.value,+ )if "sku" in cleaned_input:cleaned_input["sku"] = clean_variant_sku(cleaned_input.get("sku"))diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 58098a055e..35da5a61dc 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -6044,3 +6044,80 @@ def test_product_variant_deactivate_preorder_as_app()assert_no_permission(response)+++VARIANT_CREATE_MUTATION = """+ mutation variantCreate($input: ProductVariantCreateInput!) {+ productVariantCreate (input: $input)+ {+ productVariant {+ id+ }+ errors {+ field,+ message,+ code,+ attributes+ }+ }+ }+"""+++def test_variant_create_product_without_variant_attributes(+ product_with_product_attributes, staff_api_client, permission_manage_products+):+ product = product_with_product_attributes++ prod_id = graphene.Node.to_global_id("Product", product.pk)+ attr_id = graphene.Node.to_global_id(+ "Attribute", product.product_type.product_attributes.first().pk+ )+ input = {+ "sku": "my-sku",+ "product": prod_id,+ "attributes": [{"id": attr_id, "values": ["1"]}],+ }+ response = staff_api_client.post_graphql(+ VARIANT_CREATE_MUTATION,+ variables={"input": input},+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)++ errors = content["data"]["productVariantCreate"]["errors"]+ assert errors+ assert errors[0]["code"] == ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.name+ assert len(errors[0]["attributes"]) == 1+ assert errors[0]["attributes"][0] == attr_id+++def test_variant_create_product_with_variant_attributes_variant_flag_false(+ product_with_variant_attributes, staff_api_client, permission_manage_products+):+ product = product_with_variant_attributes++ product_type = product.product_type+ product_type.has_variants = False+ product_type.save()++ prod_id = graphene.Node.to_global_id("Product", product.pk)+ attr_id = graphene.Node.to_global_id(+ "Attribute", product.product_type.variant_attributes.first().pk+ )++ input = {+ "sku": "my-sku",+ "product": prod_id,+ "attributes": [{"id": attr_id, "values": ["1"]}],+ }+ response = staff_api_client.post_graphql(+ VARIANT_CREATE_MUTATION,+ variables={"input": input},+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)++ errors = content["data"]["productVariantCreate"]["errors"]+ assert errors+ assert errors[0]["code"] == ProductErrorCode.INVALID.namediff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex f7bcfd1d52..c2a359ee9a 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -1095,6 +1095,70 @@ def color_attribute(db):return attribute+def attribute_without_values():+ return Attribute.objects.create(+ slug="dropdown",+ name="Dropdown",+ type=AttributeType.PRODUCT_TYPE,+ filterable_in_storefront=True,+ filterable_in_dashboard=True,+ available_in_grid=True,+ visible_in_storefront=True,+ entity_type=None,+ )+++def product_type_with_product_attributes(attribute_without_values):+ product_type = ProductType.objects.create(+ name="product_type_with_product_attributes",+ slug="product-type-with-product-attributes",+ has_variants=False,+ is_shipping_required=False,+ weight=0,+ )+ product_type.product_attributes.add(attribute_without_values)+ return product_type+++def product_type_with_variant_attributes(attribute_without_values):+ product_type = ProductType.objects.create(+ name="product_type_with_variant_attributes",+ slug="product-type-with-variant-attributes",+ has_variants=False,+ is_shipping_required=False,+ weight=0,+ )+ product_type.variant_attributes.add(attribute_without_values)+ return product_type+++def product_with_product_attributes(+ product_type_with_product_attributes, non_default_category+):+ return Product.objects.create(+ name="product_with_product_attributes",+ slug="product-with-product-attributes",+ product_type=product_type_with_product_attributes,+ category=non_default_category,+ )+++def product_with_variant_attributes(+ product_type_with_variant_attributes, non_default_category+):+ return Product.objects.create(+ name="product_with_variant_attributes",+ slug="product-with-variant-attributes",+ product_type=product_type_with_variant_attributes,+ category=non_default_category,+ )++@pytest.fixturedef date_attribute(db):attribute = Attribute.objects.create(