Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Make product variant attributes required in API
oldPadavan484cee9f5531
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex c9dd01e67a..77baa4e24e 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -332,7 +332,19 @@ class ProductVariantCreate(ModelMutation):model = models.ProductVariant@classmethod- def clean_input(cls, info, instance, input, errors):+ def check_all_product_type_attributes_provided(+ cls, attributes_qs, attributes_input, errors):+ product_type_attr_slugs = {attr.slug for attr in attributes_qs}+ attributes_input = {attr.slug for attr in attributes_input}+ missing_attributes = product_type_attr_slugs - attributes_input+ if missing_attributes:+ cls.add_error(+ errors, 'attributes',+ 'Missing attributes: %s' % ', '.join(missing_attributes))++ @classmethod+ def clean_input(+ cls, info, instance, input, errors, check_all_attrs_provided=True):cleaned_input = super().clean_input(info, instance, input, errors)# Attributes are provided as list of `AttributeValueInput` objects.@@ -348,6 +360,9 @@ class ProductVariantCreate(ModelMutation):if attributes and product_type:try:qs = product_type.variant_attributes.prefetch_related('values')+ if check_all_attrs_provided:+ cls.check_all_product_type_attributes_provided(+ qs, attributes, errors)attributes = attributes_to_hstore(attributes, qs)except ValueError as e:cls.add_error(errors, 'attributes', str(e))@@ -378,6 +393,11 @@ class ProductVariantUpdate(ProductVariantCreate):description = 'Updates an existing variant for product'model = models.ProductVariant+ @classmethod+ def clean_input(cls, info, instance, input, errors):+ return super(ProductVariantUpdate, cls).clean_input(+ info, instance, input, errors, check_all_attrs_provided=False)+class ProductVariantDelete(ModelDeleteMutation):class Arguments:diff --git a/tests/api/test_variant.py b/tests/api/test_variant.pyindex 3d2a832bd0..eaf41d0d4f 100644--- a/tests/api/test_variant.py+++ b/tests/api/test_variant.py@@ -150,6 +150,94 @@ def test_create_variant(admin_api_client, product, product_type):assert data['weight']['value'] == weight+def test_create_product_variant_not_all_attributes(+ admin_api_client, product, product_type, color_attribute):+ query = """+ mutation createVariant (+ $productId: ID!,+ $sku: String!,+ $priceOverride: Decimal,+ $costPrice: Decimal,+ $quantity: Int!,+ $attributes: [AttributeValueInput],+ $weight: WeightScalar,+ $trackInventory: Boolean!) {+ productVariantCreate(+ input: {+ product: $productId,+ sku: $sku,+ priceOverride: $priceOverride,+ costPrice: $costPrice,+ quantity: $quantity,+ attributes: $attributes,+ trackInventory: $trackInventory,+ weight: $weight+ }) {+ errors {+ field+ message+ }+ productVariant {+ name+ sku+ attributes {+ attribute {+ slug+ }+ value {+ slug+ }+ }+ quantity+ priceOverride {+ currency+ amount+ localized+ }+ costPrice {+ currency+ amount+ localized+ }+ weight {+ value+ unit+ }+ }+ }+ }++ """+ product_id = graphene.Node.to_global_id('Product', product.pk)+ sku = "1"+ price_override = 1.32+ cost_price = 3.22+ quantity = 10+ weight = 10.22+ variant_slug = product_type.variant_attributes.first().slug+ variant_value = 'test-value'+ product_type.variant_attributes.add(color_attribute)++ variables = json.dumps({+ 'productId': product_id,+ 'sku': sku,+ 'quantity': quantity,+ 'costPrice': cost_price,+ 'priceOverride': price_override,+ 'weight': weight,+ 'attributes': [+ {'slug': variant_slug, 'value': variant_value}],+ 'trackInventory': True})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ assert 'errors' in content['data']['productVariantCreate']+ assert content['data']['productVariantCreate']['errors'] == [{+ 'field': 'attributes', 'message': 'Missing attributes: color'}]+ assert not product.variants.filter(sku=sku).exists()++def test_update_product_variant(admin_api_client, product):query = """mutation updateVariant (