Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use slag instead of name for setting up attributes
Adam Janik40f05f1f4e67
diff --git a/saleor/dashboard/graphql/api.py b/saleor/dashboard/graphql/api.pyindex c3a54495f0..ef240a6d92 100644--- a/saleor/dashboard/graphql/api.py+++ b/saleor/dashboard/graphql/api.py@@ -51,9 +51,11 @@ class Query(graphene.ObjectType):def resolve_pages(self, info):return resolve_all_pages()+ @staff_member_requireddef resolve_product(self, info, id):return get_node(info, id, only_type=Product)+ @staff_member_requireddef resolve_products(self, info, category_id=None, **kwargs):if category_id is not None:category = get_node(info, category_id, only_type=Category)diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.pyindex 60defce6eb..91402bffdc 100644--- a/saleor/dashboard/graphql/product/mutations.py+++ b/saleor/dashboard/graphql/product/mutations.py@@ -48,7 +48,7 @@ class CategoryDelete(StaffMemberRequiredMutation, ModelDeleteMutation):class ValuesInput(InputObjectType):- name = graphene.String(required=True)+ slug = graphene.String(required=True)value = graphene.String(required=True)diff --git a/saleor/dashboard/graphql/utils.py b/saleor/dashboard/graphql/utils.pyindex 50133808fa..acca5e9bd1 100644--- a/saleor/dashboard/graphql/utils.py+++ b/saleor/dashboard/graphql/utils.py@@ -1,24 +1,25 @@from saleor.product import models+def get_attributes_dict_from_list(attributes):""":param attributes: list:return: dict- Takes list on form [{"name": "attr_name", "value": "attr_value"}, {...}]+ Takes list on form [{"slug": "attr_slug", "value": "attr_value"}, {...}]and converts into dictionary {attr_pk: value_pk}"""attr_ids = {}- attr_name_id = dict(- models.ProductAttribute.objects.values_list('name', 'id'))- value_name_id = dict(+ attr_slug_id = dict(+ models.ProductAttribute.objects.values_list('slug', 'id'))+ value_slug_id = dict(models.AttributeChoiceValue.objects.values_list('name', 'id'))for attribute in attributes:- attr_name = attribute.get('name')- if attr_name not in attr_name_id:+ attr_slug = attribute.get('slug')+ if attr_slug not in attr_slug_id:raise ValueError(- 'Unknown attribute name: %r' % (attr_name,))+ 'Unknown attribute slug: %r' % (attr_slug,))attr_value = attribute.get('value')- attr_ids[attr_name_id.get(- attr_name)] = value_name_id.get(attr_value)+ attr_ids[attr_slug_id.get(+ attr_slug)] = value_slug_id.get(attr_value)return attr_idsdiff --git a/tests/dashboard/test_graphql.py b/tests/dashboard/test_graphql.pyindex 454613cd77..5451f95f67 100644--- a/tests/dashboard/test_graphql.py+++ b/tests/dashboard/test_graphql.py@@ -320,12 +320,12 @@ def test_create_product(# Default attribute defined in product_type fixturecolor_attr = product_type.product_attributes.get(name='Color')- color_attr_name = color_attr.namecolor_attr_value = color_attr.values.first().name- color_attr_slug = color_attr.values.first().slug+ color_value_slug = color_attr.values.first().slug+ color_attr_slug = color_attr.slug# Add second attributeproduct_type.product_attributes.add(size_attribute)- size_attr_name = product_type.product_attributes.get(name='Size').name+ size_attr_slug = product_type.product_attributes.get(name='Size').slugnon_existent_attr_value = 'The cake is a lie'# test creating root product@@ -338,8 +338,8 @@ def test_create_product('isFeatured': product_isFeatured,'price': product_price,'attributes': [- {'name': color_attr_name, 'value': color_attr_value},- {'name': size_attr_name, 'value': non_existent_attr_value}]})+ {'slug': color_attr_slug, 'value': color_attr_value},+ {'slug': size_attr_slug, 'value': non_existent_attr_value}]})response = client.post(reverse('dashboard:api'), {'query': query, 'variables': variables})@@ -354,7 +354,7 @@ def test_create_product(assert data['product']['productType']['name'] == product_type.nameassert data['product']['category']['name'] == default_category.nameassert data['product']['attributes'][1]['value'] == None- assert data['product']['attributes'][0]['value'] == color_attr_slug+ assert data['product']['attributes'][0]['value'] == color_value_slugdef test_update_product(