Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Try to guess attribute value type in API
Artur Smęt7e03eaae869c
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.pyindex eeaf331ef8..09217718e4 100644--- a/saleor/graphql/product/mutations/attributes.py+++ b/saleor/graphql/product/mutations/attributes.py@@ -20,6 +20,8 @@ class AttributeChoiceValueInput(graphene.InputObjectType):required=True, description='Internal name.')name = graphene.String(required=True, description='Name displayed in the interface.')+ value = graphene.String(+ required=True, description='Real value eg. HEX color.')class AttributeChoiceValueUpdateInput(graphene.InputObjectType):diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 8e93fa7a64..1008df5fbe 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -1,3 +1,5 @@+import re+import graphenefrom graphene import relayfrom graphene_django.filter import DjangoFilterConnectionField@@ -17,6 +19,13 @@ from ..utils import get_database_idfrom .filters import ProductFilterSet+class ProductAttribuetValueType(graphene.Enum):+ COLOR = 'COLOR'+ GRADIENT = 'GRADIENT'+ URL = 'URL'+ STRING = 'STRING'++def resolve_attribute_list(attributes):keys = list(attributes.keys())values = list(attributes.values())@@ -35,10 +44,23 @@ def resolve_attribute_list(attributes):return attributes_list+def resolve_attribute_value_type(attribute_value):+ color_pattern = '^(#[0-9a-fA-F]{3}|#(?:[0-9a-fA-F]{2}){2,4}|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))$' # noqa+ color_pattern = re.compile(color_pattern)+ if color_pattern.match(attribute_value):+ return ProductAttribuetValueType.COLOR+ if 'gradient(' in attribute_value:+ return ProductAttribuetValueType.GRADIENT+ if '://' in attribute_value:+ return ProductAttribuetValueType.URL+ return ProductAttribuetValueType.STRING++class ProductAttributeValue(CountableDjangoObjectType):name = graphene.String(description='Visible name for display purposes.')slug = graphene.String(description='Internal representation of an attribute name.')+ type = ProductAttribuetValueType(description='Type of value.')class Meta:description = 'Represents a value of an attribute.'@@ -46,6 +68,9 @@ class ProductAttributeValue(CountableDjangoObjectType):interfaces = [relay.Node]model = models.AttributeChoiceValue+ def resolve_type(self, info):+ return resolve_attribute_value_type(self.value)+class ProductAttribute(CountableDjangoObjectType):name = graphene.String(description='Visible name for display purposes.')diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 82b0624019..26d4136cc9 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -345,6 +345,7 @@ input AttributeChoiceValueInput {attribute: IDslug: String!name: String!+ value: String!}type AttributeChoiceValueUpdate {@@ -1119,6 +1120,13 @@ type Product implements Node {imageById(id: ID): ProductImage}+enum ProductAttribuetValueType {+ COLOR+ GRADIENT+ URL+ STRING+}+type ProductAttribute implements Node {id: ID!slug: String@@ -1156,7 +1164,9 @@ type ProductAttributeValue implements Node {id: ID!sortOrder: Int!name: String+ value: String!slug: String+ type: ProductAttribuetValueType}input ProductAttributesInput {diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex 455fbd69e2..6cd83fab71 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -1,4 +1,5 @@import json+from unittest.mock import Mockimport grapheneimport pytest@@ -7,6 +8,7 @@ from tests.utils import get_graphql_contentfrom saleor.product.models import (Category, ProductAttribute, AttributeChoiceValue)from saleor.graphql.product.utils import attributes_to_hstore+from saleor.graphql.product.types import resolve_attribute_value_type, ProductAttribuetValueTypedef test_attributes_to_hstore(product, color_attribute):@@ -165,12 +167,14 @@ def test_delete_product_attribute(admin_api_client, color_attribute):def test_create_attribute_choice_value(admin_api_client, color_attribute):attribute = color_attributequery = """- mutation createChoice($attribute: ID!, $name: String!, $slug: String!) {+ mutation createChoice($attribute: ID!, $name: String!, $slug: String!, $value: String!) {attributeChoiceValueCreate(- input: {attribute: $attribute, name: $name, slug: $slug}) {+ input: {attribute: $attribute, name: $name, slug: $slug, value: $value}) {attributeChoiceValue {nameslug+ type+ value}}}@@ -178,8 +182,9 @@ def test_create_attribute_choice_value(admin_api_client, color_attribute):attribute_id = graphene.Node.to_global_id('ProductAttribute', attribute.id)name = 'test name'slug = 'test-slug'+ value = 'test-string'variables = json.dumps(- {'name': name, 'slug': slug, 'attribute': attribute_id})+ {'name': name, 'slug': slug, 'value': value, 'attribute': attribute_id})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)@@ -188,6 +193,8 @@ def test_create_attribute_choice_value(admin_api_client, color_attribute):'data']['attributeChoiceValueCreate']['attributeChoiceValue']assert data['name'] == nameassert data['slug'] == slug+ assert data['value'] == value+ assert data['type'] == 'STRING'def test_update_attribute_choice_value(admin_api_client, pink_choice_value):@@ -239,3 +246,57 @@ def test_delete_attribute_choice_value(admin_api_client, color_attribute, pink_cassert 'errors' not in contentwith pytest.raises(value._meta.model.DoesNotExist):value.refresh_from_db()+[email protected]('raw_value, expected_type', [+ ('#0000', ProductAttribuetValueType.COLOR),+ ('#FF69B4', ProductAttribuetValueType.COLOR),+ ('rgb(255, 0, 0)', ProductAttribuetValueType.COLOR),+ ('hsl(0, 100%, 50%)', ProductAttribuetValueType.COLOR),+ ('hsla(120, 60%, 70%, 0.3)', ProductAttribuetValueType.COLOR),+ ('rgba(100%, 255, 0, 0)', ProductAttribuetValueType.COLOR),+ ('http://example.com', ProductAttribuetValueType.URL),+ ('https://example.com', ProductAttribuetValueType.URL),+ ('ftp://example.com', ProductAttribuetValueType.URL),+ ('example.com', ProductAttribuetValueType.STRING),+ ('Foo', ProductAttribuetValueType.STRING),+ ('linear-gradient(red, yellow)', ProductAttribuetValueType.GRADIENT),+ ('radial-gradient(#0000, yellow)', ProductAttribuetValueType.GRADIENT),+])+def test_resolve_attribute_value_type(raw_value, expected_type):+ assert resolve_attribute_value_type(raw_value) == expected_type+++def test_query_attribute_values(+ color_attribute, pink_choice_value, user_api_client):+ attribute_id = graphene.Node.to_global_id(+ 'ProductAttribute', color_attribute.id)+ query = """+ query getAttribute($id: ID!) {+ attributes(id: $id) {+ edges {+ node {+ id+ name+ values {+ name+ type+ value+ }+ }+ }+ }+ }+ """+ variables = json.dumps({'id': attribute_id})+ response = user_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['attributes']['edges'][0]['node']+ values = data['values']+ pink = [v for v in values if v['name'] == pink_choice_value.name]+ assert len(pink) == 1+ pink = pink[0]+ assert pink['value'] == '#FF69B4'+ assert pink['type'] == 'COLOR'+diff --git a/tests/conftest.py b/tests/conftest.pyindex f37c83b055..7878125471 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -255,7 +255,7 @@ def color_attribute(db): # pylint: disable=W0613@pytest.fixturedef pink_choice_value(color_attribute): # pylint: disable=W0613value = AttributeChoiceValue.objects.create(- slug='pink', name='Color', attribute=color_attribute)+ slug='pink', name='Pink', attribute=color_attribute, value='#FF69B4')return value