Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Merge branch 'master' into feature/swatch-attribute

IKarbowiak7b0d0644697d
diff --cc CHANGELOG.md
index 68776b4cc3,1481ea3fae..314c62772e
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -69,7 -72,60 +72,61 @@@ All notable, unreleased changes to thi
- Use GraphQL IDs instead of database IDs in export - #7240 by @IKarbowiak
- Fix draft order tax mismatch - #7226 by @IKarbowiak
- Introduce `calculate_order_line_total` plugin method
+- Introduce swatch attributes - #7261 by @IKarbowiak
+ - Update core logging for better Celery tasks handling - #7251 by @tomaszszymanski129
+ - Raise ValidationError when refund cannot be performed - #7260 by @IKarbowiak
+ - Extend order with origin and original order values - #7326 by @IKarbowiak
+ - Fix customer addresses missing after customer creation - #7327 by @tomaszszymanski129
+ - Extend order webhook payload with fulfillment fields - #7364, #7347 by @korycins
+ - fulfillments extended with:
+ - total_refund_amount
+ - shipping_refund_amount
+ - lines
+ - fulfillment lines extended with:
+ - total_price_net_amount
+ - total_price_gross_amount
+ - undiscounted_unit_price_net
+ - undiscounted_unit_price_gross
+ - unit_price_net
+ - Extend order payload with undiscounted prices and add psp_reference to payment model - #7339 by @IKarbowiak
+ - order payload extended with the following fields:
+ - `undiscounted_total_net_amount`
+ - `undiscounted_total_gross_amount`
+ - `psp_reference` on `payment`
+ - order lines extended with:
+ - `undiscounted_unit_price_net_amount`
+ - `undiscounted_unit_price_gross_amount`
+ - `undiscounted_total_price_net_amount`
+ - `undiscounted_total_price_gross_amount`
+ - Copy metadata fields when creating reissue - #7358 by @IKarbowiak
+ - Add payment webhooks - #7044 by @maarcingebala
+ - Fix invoice generation - #7376 by @tomaszszymanski129
+ - Allow defining only one field in translations - #7363 by @IKarbowiak
+ - Trigger `checkout_updated` hook for checkout meta mutations - #7392 by @maarcingebala
+ - Optimize `inputType` resolver on `AttributeValue` type - 7396 by @tomaszszymanski129
+ - Allow filtering pages by ids - #7393 by @IKarbowiak
+ - Refactor account filters - 7419 by @tomaszszymanski129
+ - Fix validate `min_spent` on vouchers to use net or gross value depends on `settings.display_gross_prices` - #7408 by @d-wysocki
+ - Fix invoice generation - #7376 by tomaszszymanski129
+ - Unify channel ID params #7378
+ - targetChannel from ChannelDeleteInput changed to channelId
+ - `channel` from `DraftOrderCreateInput` changed to channelId
+ - `channel` from `DraftOrderInput` changed to channelId
+ - `channel` from `pluginUpdate` changed to channelId
+ - Compress celery tasks related with `user_emails` and `webhooks` - #7445 by d-wysocki
+ - Order events performance - #7424 by tomaszszymanski129
+ - Add hash to uploading images #7453 by @IKarbowiak
+ - Add file format validation for uploaded images - #7447 by @IKarbowiak
+ - Add boolean attributes - #7454 by @piotrgrundas
+ - Fix attaching params for address form errors - #7485 by @IKarbowiak
+ - Update draft order validation - #7253 by @IKarbowiak
+ - Extend Order type with errors: [OrderError!]! field
+ - Create tasks for deleting order lines by deleting products or variants
+ - Fix doubled checkout total price for one line and zero shipping price - #7532 by @IKarbowiak
+ - Deprecate nested objects in TranslatableContent types - #7522 by @IKarbowiak
+ - Fix performance for User type on resolvers: orders, gift cards, events - #7574 by @tomaszszymanski129
+ - Fix failing account mutations for app - #7569 by @IKarbowiak
+ - Modify order of auth middleware calls - #7572 by @tomaszszymanski129
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --cc saleor/attribute/__init__.py
index 729b0d38bf,35f99d668f..2bd5173026
--- a/saleor/attribute/__init__.py
+++ b/saleor/attribute/__init__.py
@@@ -10,7 -10,7 +10,8 @@@ class AttributeInputType
REFERENCE = "reference"
NUMERIC = "numeric"
RICH_TEXT = "rich-text"
+ SWATCH = "swatch"
+ BOOLEAN = "boolean"
CHOICES = [
(DROPDOWN, "Dropdown"),
@@@ -19,29 -19,45 +20,49 @@@
(REFERENCE, "Reference"),
(NUMERIC, "Numeric"),
(RICH_TEXT, "Rich Text"),
+ (SWATCH, "Swatch"),
+ (BOOLEAN, "Boolean"),
+ ]
+
+ # list of the input types that can be used in variant selection
- ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN, BOOLEAN]
++ ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN, BOOLEAN, SWATCH]
+
+ TYPES_WITH_CHOICES = [
+ DROPDOWN,
+ MULTISELECT,
++ SWATCH,
+ ]
+
+ # list of the input types that are unique per instances
+ TYPES_WITH_UNIQUE_VALUES = [
+ FILE,
+ REFERENCE,
+ RICH_TEXT,
+ NUMERIC,
]
- ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN, SWATCH]
-# list of input types that are allowed for given attribute property
ATTRIBUTE_PROPERTIES_CONFIGURATION = {
"filterable_in_storefront": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
+ AttributeInputType.BOOLEAN,
],
"filterable_in_dashboard": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
+ AttributeInputType.BOOLEAN,
],
"available_in_grid": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
+ AttributeInputType.BOOLEAN,
],
"storefront_search_position": [
AttributeInputType.DROPDOWN,
diff --cc saleor/attribute/migrations/0014_auto_20210719_0931.py
index 4c064415b2,0000000000..dd47091011
mode 100644,000000..100644
--- a/saleor/attribute/migrations/0014_auto_20210719_0931.py
+++ b/saleor/attribute/migrations/0014_auto_20210719_0931.py
@@@ -1,35 -1,0 +1,36 @@@
- # Generated by Django 3.1.8 on 2021-04-29 10:13
++# Generated by Django 3.2.2 on 2021-07-19 09:31
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
- ("attribute", "0010_auto_20210412_0736"),
++ ("attribute", "0013_auto_20210609_2245"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="attribute",
+ name="input_type",
+ field=models.CharField(
+ choices=[
+ ("dropdown", "Dropdown"),
+ ("multiselect", "Multi Select"),
+ ("file", "File"),
+ ("reference", "Reference"),
+ ("numeric", "Numeric"),
+ ("rich-text", "Rich Text"),
+ ("swatch", "Swatch"),
++ ("boolean", "Boolean"),
+ ],
+ default="dropdown",
+ max_length=50,
+ ),
+ ),
+ migrations.AlterField(
+ model_name="attributevalue",
+ name="value",
+ field=models.CharField(blank=True, default="", max_length=9),
+ ),
+ ]
diff --cc saleor/csv/tests/export/products_data/test_get_products_data.py
index ce4c51202b,907ae65356..7632c187d5
--- a/saleor/csv/tests/export/products_data/test_get_products_data.py
+++ b/saleor/csv/tests/export/products_data/test_get_products_data.py
@@@ -240,8 -240,8 +240,9 @@@ def test_get_products_data_for_specifie
product_with_variant_with_two_attributes,
rich_text_attribute,
color_attribute,
+ boolean_attribute,
variant_with_many_stocks,
+ swatch_attribute,
):
# given
product.variants.add(variant_with_many_stocks)
@@@ -251,7 -251,7 +252,8 @@@
product_type_product_reference_attribute,
numeric_attribute,
rich_text_attribute,
+ swatch_attribute,
+ boolean_attribute,
)
product.product_type.product_attributes.add(
file_attribute,
@@@ -259,7 -259,17 +261,18 @@@
product_type_product_reference_attribute,
numeric_attribute,
rich_text_attribute,
+ swatch_attribute,
+ boolean_attribute,
+ )
+
+ # add boolean attribute
+ associate_attribute_values_to_instance(
+ variant_with_many_stocks,
+ boolean_attribute,
+ boolean_attribute.values.first(),
+ )
+ associate_attribute_values_to_instance(
+ product, boolean_attribute, boolean_attribute.values.first()
)
# add rich text attribute
diff --cc saleor/csv/tests/export/products_data/test_handle_relations_data.py
index dc9f2bcaec,5ff6db7868..d491bd4ae3
--- a/saleor/csv/tests/export/products_data/test_handle_relations_data.py
+++ b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
@@@ -951,6 -944,32 +957,34 @@@ def test_add_rich_text_attribute_info_t
assert result[pk][expected_header] == {"Dummy"}
+ def test_add_boolean_attribute_info_to_data(product):
+ # given
+ pk = product.pk
+ slug = "xx_false"
+ attribute_data = AttributeData(
+ slug=slug,
+ value=None,
++ value_slug=None,
++ value_name=None,
+ file_url=None,
+ input_type="boolean",
+ entity_type=None,
+ unit=None,
+ rich_text=None,
+ boolean=False,
+ )
+ input_data = {pk: {}}
+
+ # when
+ result = add_attribute_info_to_data(
+ product.pk, attribute_data, "product attribute", input_data
+ )
+
+ # then
+ expected_header = f"{slug} (product attribute)"
+ assert result[pk][expected_header] == {"False"}
+
+
def test_add_reference_attribute_info_to_data(product, page):
# given
pk = product.pk
@@@ -1063,34 -1076,6 +1101,35 @@@ def test_add_numeric_attribute_info_to_
# then
expected_header = f"{numeric_attribute.slug} (product attribute)"
+ assert result[pk][expected_header] == {name}
+
+
+def test_add_swatch_attribute_file_info_to_data(product, swatch_attribute):
+ # given
+ pk = product.pk
+ slug = "white"
+ value = "#ffffff"
+ attribute_data = AttributeData(
+ slug=swatch_attribute.slug,
+ value_slug=slug,
+ value_name=None,
+ value=value,
+ file_url=None,
+ input_type="swatch",
+ entity_type=None,
+ unit=None,
+ rich_text=None,
++ boolean=None,
+ )
+ input_data = {pk: {}}
+
+ # when
+ result = add_attribute_info_to_data(
+ product.pk, attribute_data, "product attribute", input_data
+ )
+
+ # then
+ expected_header = f"{swatch_attribute.slug} (product attribute)"
assert result[pk][expected_header] == {value}
@@@ -1137,6 -1118,33 +1177,35 @@@ def test_add_attribute_info_to_data_no_
entity_type=None,
rich_text=None,
unit=None,
+ boolean=None,
+ )
+ input_data = {pk: {}}
+
+ # when
+ result = add_attribute_info_to_data(
+ product.pk, attribute_data, "product attribute", input_data
+ )
+
+ # then
+ expected_header = f"{slug} (product attribute)"
+ assert result[pk][expected_header] == {""}
+
+
+ def test_add_attribute_info_to_data_no_boolean_for_boolean_attribute(product):
+ # given
+ pk = product.pk
+ slug = "xxx_None"
+ attribute_data = AttributeData(
+ slug=slug,
+ value=None,
++ value_slug=None,
++ value_name=None,
+ file_url=None,
+ input_type="boolean",
+ entity_type=None,
+ rich_text=None,
+ unit=None,
+ boolean=None,
)
input_data = {pk: {}}
@@@ -1177,34 -1184,6 +1247,35 @@@ def test_add_attribute_info_to_data_no_
assert result[pk][expected_header] == {""}
+def test_add_swatch_attribute_value_info_to_data(product, numeric_attribute):
+ # given
+ pk = product.pk
+ slug = "Logo"
+ test_url = "test.txt"
+ attribute_data = AttributeData(
+ slug=numeric_attribute.slug,
+ value_slug=slug,
+ value_name=None,
+ value=None,
+ file_url=test_url,
+ input_type="swatch",
+ entity_type=None,
+ unit=None,
+ rich_text=None,
++ boolean=None,
+ )
+ input_data = {pk: {}}
+
+ # when
+ result = add_attribute_info_to_data(
+ product.pk, attribute_data, "product attribute", input_data
+ )
+
+ # then
+ expected_header = f"{numeric_attribute.slug} (product attribute)"
+ assert result[pk][expected_header] == {"http://mirumee.com/media/" + test_url}
+
+
def test_add_warehouse_info_to_data(product):
# given
pk = product.pk
diff --cc saleor/csv/tests/export/products_data/utils.py
index 4ef1cfa293,6fe0827a6b..0b6c98a756
--- a/saleor/csv/tests/export/products_data/utils.py
+++ b/saleor/csv/tests/export/products_data/utils.py
@@@ -44,12 -44,10 +44,14 @@@ def get_attribute_value(assigned_attrib
value += f" {attribute.unit}"
elif attribute.input_type == AttributeInputType.RICH_TEXT:
value = clean_editor_js(value_instance.rich_text, to_string=True)
+ elif attribute.input_type == AttributeInputType.SWATCH:
+ value = (
+ value_instance.file_url if value_instance.file_url else value_instance.value
+ )
+ elif attribute.input_type == AttributeInputType.BOOLEAN:
+ value = str(value_instance.boolean)
else:
- value = value_instance.slug
+ value = value_instance.name or value_instance.slug
return value
diff --cc saleor/csv/utils/__init__.py
index d666e5e7b8,e06f39ada6..f1c12fdfdf
--- a/saleor/csv/utils/__init__.py
+++ b/saleor/csv/utils/__init__.py
@@@ -21,11 -21,10 +21,12 @@@ class ProductExportFields
}
PRODUCT_ATTRIBUTE_FIELDS = {
- "value": "attributes__values__slug",
+ "value_slug": "attributes__values__slug",
+ "value_name": "attributes__values__name",
"file_url": "attributes__values__file_url",
"rich_text": "attributes__values__rich_text",
+ "value": "attributes__values__value",
+ "boolean": "attributes__values__boolean",
"slug": "attributes__assignment__attribute__slug",
"input_type": "attributes__assignment__attribute__input_type",
"entity_type": "attributes__assignment__attribute__entity_type",
@@@ -50,11 -49,10 +51,12 @@@
}
VARIANT_ATTRIBUTE_FIELDS = {
- "value": "variants__attributes__values__slug",
+ "value_slug": "variants__attributes__values__slug",
+ "value_name": "variants__attributes__values__name",
"file_url": "variants__attributes__values__file_url",
"rich_text": "variants__attributes__values__rich_text",
+ "value": "variants__attributes__values__value",
+ "boolean": "variants__attributes__values__boolean",
"slug": "variants__attributes__assignment__attribute__slug",
"input_type": "variants__attributes__assignment__attribute__input_type",
"entity_type": "variants__attributes__assignment__attribute__entity_type",
diff --cc saleor/csv/utils/products_data.py
index 5896e03c11,7703281c6f..b47c4b02b4
--- a/saleor/csv/utils/products_data.py
+++ b/saleor/csv/utils/products_data.py
@@@ -298,11 -298,10 +298,12 @@@ AttributeData = namedtuple
"input_type",
"entity_type",
"unit",
+ "value_slug",
+ "value_name",
+ "value",
+ "file_url",
"rich_text",
+ "boolean",
],
)
@@@ -398,51 -396,37 +400,55 @@@ def add_attribute_info_to_data
"""
slug = attribute_data.slug
header = None
- if slug:
- header = f"{slug} ({attribute_owner})"
- input_type = attribute_data.input_type
- if input_type == AttributeInputType.FILE:
- file_url = attribute_data.file_url
- value = (
- build_absolute_uri(urljoin(settings.MEDIA_URL, file_url))
- if file_url
- else ""
+
+ if not slug:
+ return result_data
+
+ header = f"{slug} ({attribute_owner})"
+ value = prepare_attribute_value(attribute_data)
+
+ if header in result_data[pk]:
+ result_data[pk][header].add(value) # type: ignore
+ else:
+ result_data[pk][header] = {value}
+
+ return result_data
+
+
+def prepare_attribute_value(attribute_data: AttributeData):
+ """Prepare value of attribute value depending on the attribute input type."""
+ input_type = attribute_data.input_type
+ if input_type == AttributeInputType.FILE:
+ file_url = attribute_data.file_url
+ value = (
+ build_absolute_uri(urljoin(settings.MEDIA_URL, file_url))
+ if file_url
+ else ""
+ )
+ elif input_type == AttributeInputType.REFERENCE and attribute_data.value_slug:
+ reference_id = attribute_data.value_slug.split("_")[1]
+ value = f"{attribute_data.entity_type}_{reference_id}"
+ elif input_type == AttributeInputType.NUMERIC:
+ value = f"{attribute_data.value_name}"
+ if attribute_data.unit:
+ value += f" {attribute_data.unit}"
+ elif input_type == AttributeInputType.RICH_TEXT:
+ value = clean_editor_js(attribute_data.rich_text, to_string=True)
++ elif (
++ input_type == AttributeInputType.BOOLEAN and attribute_data.boolean is not None
++ ):
++ value = str(attribute_data.boolean)
+ elif input_type == AttributeInputType.SWATCH:
+ if attribute_data.file_url:
+ value = build_absolute_uri(
+ urljoin(settings.MEDIA_URL, attribute_data.file_url)
)
- elif input_type == AttributeInputType.REFERENCE and attribute_data.value:
- reference_id = attribute_data.value.split("_")[1]
- value = f"{attribute_data.entity_type}_{reference_id}"
- elif input_type == AttributeInputType.NUMERIC:
- value = f"{attribute_data.value}"
- if attribute_data.unit:
- value += f" {attribute_data.unit}"
- elif input_type == AttributeInputType.RICH_TEXT:
- value = clean_editor_js(attribute_data.rich_text, to_string=True)
- elif (
- input_type == AttributeInputType.BOOLEAN
- and attribute_data.boolean is not None
- ):
- value = str(attribute_data.boolean)
else:
- value = attribute_data.value if attribute_data.value else ""
- if header in result_data[pk]:
- result_data[pk][header].add(value) # type: ignore
- else:
- result_data[pk][header] = {value}
- return result_data
+ value = attribute_data.value
+ else:
+ value = attribute_data.value_name or attribute_data.value_slug or ""
+
+ return value
def add_warehouse_info_to_data(
diff --cc saleor/graphql/attribute/tests/mutations/test_attribute_create.py
index 26643a4e40,36d91376ce..bc66f038bf
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_create.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_create.py
@@@ -27,13 -27,13 +27,17 @@@ CREATE_ATTRIBUTE_MUTATION = ""
filterableInDashboard
availableInGrid
storefrontSearchPosition
- values {
- name
- slug
- value
- file {
- url
- contentType
+ choices(first: 10) {
+ edges {
+ node {
+ name
+ slug
+ value
++ file {
++ url
++ contentType
++ }
+ }
}
}
productTypes(first: 10) {
@@@ -92,10 -93,11 +96,10 @@@ def test_create_attribute_and_attribute
), "The attribute should not have been assigned to a product type"
# Check if the attribute values were correctly created
- assert len(data["attribute"]["values"]) == 1
+ assert len(data["attribute"]["choices"]) == 1
assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
- assert data["attribute"]["values"][0]["name"] == name
- assert data["attribute"]["values"][0]["slug"] == slugify(name)
+ assert data["attribute"]["choices"]["edges"][0]["node"]["name"] == name
+ assert data["attribute"]["choices"]["edges"][0]["node"]["slug"] == slugify(name)
- assert data["attribute"]["choices"]["edges"][0]["node"]["value"] == value
def test_create_numeric_attribute_and_attribute_values(
@@@ -202,284 -202,6 +204,284 @@@ def test_create_numeric_attribute_and_a
assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+def test_create_swatch_attribute_and_attribute_values_only_name_provided(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
- assert len(data["attribute"]["values"]) == 1
++ assert len(data["attribute"]["choices"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
- assert data["attribute"]["values"][0]["name"] == name
- assert data["attribute"]["values"][0]["slug"] == slugify(name)
++ assert data["attribute"]["choices"]["edges"][0]["node"]["name"] == name
++ assert data["attribute"]["choices"]["edges"][0]["node"]["slug"] == slugify(name)
+
+
+def test_create_swatch_attribute_and_attribute_values_with_file(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Logo"
+ file_url = "http://mirumee.com/test_media/test_logo.png"
+ content_type = "image/png"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [
+ {"name": name, "fileUrl": file_url, "contentType": content_type}
+ ],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
- assert len(data["attribute"]["values"]) == 1
++ assert len(data["attribute"]["choices"]["edges"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
- assert data["attribute"]["values"][0]["name"] == name
- assert data["attribute"]["values"][0]["slug"] == slugify(name)
- assert data["attribute"]["values"][0]["file"] == {
++ assert data["attribute"]["choices"]["edges"][0]["node"]["name"] == name
++ assert data["attribute"]["choices"]["edges"][0]["node"]["slug"] == slugify(name)
++ assert data["attribute"]["choices"]["edges"][0]["node"]["file"] == {
+ "url": file_url,
+ "contentType": content_type,
+ }
+
+
+def test_create_swatch_attribute_and_attribute_values_with_value(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ value = "#ffc0cb"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, "value": value}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
- assert len(data["attribute"]["values"]) == 1
++ assert len(data["attribute"]["choices"]["edges"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
- assert data["attribute"]["values"][0]["name"] == name
- assert data["attribute"]["values"][0]["slug"] == slugify(name)
- assert data["attribute"]["values"][0]["file"] is None
- assert data["attribute"]["values"][0]["value"] == value
++ assert data["attribute"]["choices"]["edges"][0]["node"]["name"] == name
++ assert data["attribute"]["choices"]["edges"][0]["node"]["slug"] == slugify(name)
++ assert data["attribute"]["choices"]["edges"][0]["node"]["file"] is None
++ assert data["attribute"]["choices"]["edges"][0]["node"]["value"] == value
+
+
+def test_create_swatch_attribute_and_attribute_values_file_and_value_provided(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, "value": "#A8A8A8", "fileUrl": file_url}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeCreate"]
+ errors = data["errors"]
+ assert not data["attribute"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "values"
+ assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+
+
+ "field, value", [("fileUrl", "test.jpg"), ("value", "blue"), ("contentType", "jpg")]
+)
+def test_create_not_swatch_attribute_provide_not_valid_data(
+ field,
+ value,
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Test"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, field: value}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.DROPDOWN.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeCreate"]
+ errors = data["errors"]
+ assert not data["attribute"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "values"
+ assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+
+
def test_create_attribute_with_file_input_type(
staff_api_client,
permission_manage_product_types_and_attributes,
diff --cc saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
index 27745675d8,507321b010..654838ee30
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
@@@ -40,12 -36,12 +40,16 @@@ CREATE_ATTRIBUTE_VALUE_MUTATION = ""
code
}
attribute {
- values {
- name
- value
- file {
- url
- contentType
+ choices(first: 10) {
+ edges {
+ node {
+ name
+ value
++ file {
++ url
++ contentType
++ }
+ }
}
}
}
@@@ -66,33 -62,7 +70,35 @@@ def test_create_attribute_value
query = CREATE_ATTRIBUTE_VALUE_MUTATION
attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
name = "test name"
- value = "test value"
+ variables = {"name": name, "attributeId": attribute_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+ assert not data["errors"]
+
+ attr_data = data["attributeValue"]
+ assert attr_data["name"] == name
+ assert attr_data["slug"] == slugify(name)
- assert name in [value["name"] for value in data["attribute"]["values"]]
++ assert name in [
++ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
++ ]
+
+
+def test_create_swatch_attribute_value_with_value(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ value = "#ffffff"
variables = {"name": name, "value": value, "attributeId": attribute_id}
# when
@@@ -108,114 -78,14 +114,120 @@@
attr_data = data["attributeValue"]
assert attr_data["name"] == name
assert attr_data["slug"] == slugify(name)
- assert name in [value["name"] for value in data["attribute"]["values"]]
- assert value in [value["value"] for value in data["attribute"]["values"]]
+ assert name in [
+ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
+ ]
+ assert value in [
+ value["node"]["value"] for value in data["attribute"]["choices"]["edges"]
+ ]
+def test_create_swatch_attribute_value_with_file(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ file = "http://mirumee.com/test_media/test_file.jpeg"
+ content_type = "image/jpeg"
+ variables = {
+ "name": name,
+ "fileUrl": file,
+ "contentType": content_type,
+ "attributeId": attribute_id,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+ assert not data["errors"]
+
+ attr_data = data["attributeValue"]
+ assert attr_data["name"] == name
+ assert attr_data["slug"] == slugify(name)
- assert name in [value["name"] for value in data["attribute"]["values"]]
++ assert name in [
++ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
++ ]
+ assert {"url": file, "contentType": content_type} in [
- value["file"] for value in data["attribute"]["values"]
++ value["node"]["file"] for value in data["attribute"]["choices"]["edges"]
+ ]
+
+
+def test_create_swatch_attribute_value_with_value_and_file(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ value = "#ffffff"
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {
+ "name": name,
+ "value": value,
+ "fileUrl": file_url,
+ "attributeId": attribute_id,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 2
+ assert {error["code"] for error in data["errors"]} == {
+ AttributeErrorCode.INVALID.name,
+ AttributeErrorCode.INVALID.name,
+ }
+ assert {error["field"] for error in data["errors"]} == {"fileUrl", "value"}
+
+
+ "field, value",
+ [
+ ("value", "#ffffff"),
+ ("fileUrl", "http://mirumee.com/test_media/test_file.jpeg"),
+ ("contentType", "jpeg"),
+ ],
+)
+def test_create_attribute_value_provide_not_allowed_input_data(
+ field, value, staff_api_client, color_attribute, permission_manage_products
+):
+ # given
+ attribute = color_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ variables = {"name": name, field: value, "attributeId": attribute_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == AttributeErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == field
+
+
def test_create_attribute_value_not_unique_name(
staff_api_client, color_attribute, permission_manage_products
):
diff --cc saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
index f5c17dcfe7,3ad0631420..d1458325e0
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
@@@ -18,15 -17,14 +18,19 @@@ mutation AttributeValueUpdate
attributeValue {
name
slug
+ value
+ file {
+ url
+ contentType
+ }
}
attribute {
- values {
- name
+ choices(first: 10) {
+ edges {
+ node {
+ name
+ }
+ }
}
}
}
@@@ -85,128 -85,3 +91,134 @@@ def test_update_attribute_value_name_no
assert data["errors"][0]["message"]
assert data["errors"][0]["field"] == "name"
assert data["errors"][0]["code"] == AttributeErrorCode.ALREADY_EXISTS.name
+
+
+def test_update_swatch_attribute_value(
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(value__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ name = "New name"
+ variables = {"input": {"name": name, "value": "", "fileUrl": ""}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == ""
+ assert data["attributeValue"]["file"] is None
- assert value.name in [value["name"] for value in data["attribute"]["values"]]
++ assert value.name in [
++ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
++ ]
+
+
[email protected]("additional_field", [{"value": ""}, {"value": None}, {}])
+def test_update_swatch_attribute_value_clear_value(
+ additional_field,
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(value__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {"input": {"fileUrl": file_url, **additional_field}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == ""
+ assert data["attributeValue"]["file"]["url"] == file_url
- assert value.name in [value["name"] for value in data["attribute"]["values"]]
++ assert value.name in [
++ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
++ ]
+
+
[email protected]("additional_field", [{"fileUrl": ""}, {"fileUrl": None}, {}])
+def test_update_swatch_attribute_value_clear_file_value(
+ additional_field,
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(file_url__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ input_value = "#ffffff"
+ variables = {"input": {"value": input_value, **additional_field}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == input_value
+ assert data["attributeValue"]["file"] is None
- assert value.name in [value["name"] for value in data["attribute"]["values"]]
++ assert value.name in [
++ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]
++ ]
+
+
+ "field, input_value",
+ [
+ ("value", "#ffffff"),
+ ("fileUrl", "http://mirumee.com/test_media/test_file.jpeg"),
+ ("contentType", "jpeg"),
+ ],
+)
+def test_update_attribute_value_invalid_input_data(
+ field,
+ input_value,
+ staff_api_client,
+ pink_attribute_value,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = pink_attribute_value
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ name = "Crimson name"
+ variables = {"input": {"name": name, field: input_value}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == AttributeErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == field
diff --cc saleor/graphql/attribute/tests/queries/test_attribute_query.py
index 500aff30a5,2c9ebc54a9..bc14be7406
--- a/saleor/graphql/attribute/tests/queries/test_attribute_query.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
@@@ -58,32 -62,36 +62,37 @@@ def test_get_single_attribute_by_slug_a
QUERY_ATTRIBUTE = """
- query($id: ID!) {
- attribute(id: $id) {
- id
- slug
- name
- inputType
- entityType
- type
- unit
- values {
+ query($id: ID!, $query: String) {
+ attribute(id: $id) {
+ id
slug
- value
+ name
inputType
- file {
- url
- contentType
+ entityType
+ type
+ unit
+ choices(first: 10, filter: {search: $query}) {
+ edges {
+ node {
+ slug
+ inputType
++ value
+ file {
+ url
+ contentType
+ }
+ }
+ }
+
}
+ valueRequired
+ visibleInStorefront
+ filterableInStorefront
+ filterableInDashboard
+ availableInGrid
+ storefrontSearchPosition
}
- valueRequired
- visibleInStorefront
- filterableInStorefront
- filterableInDashboard
- availableInGrid
- storefrontSearchPosition
}
- }
"""
@@@ -344,66 -364,6 +365,68 @@@ def test_get_single_numeric_attribute_b
)
+def test_get_single_swatch_attribute_by_staff(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ attribute_gql_id = graphene.Node.to_global_id("Attribute", swatch_attribute.id)
+ query = QUERY_ATTRIBUTE
+ content = get_graphql_content(
+ staff_api_client.post_graphql(query, {"id": attribute_gql_id})
+ )
+
+ assert content["data"]["attribute"], "Should have found an attribute"
+ assert content["data"]["attribute"]["id"] == attribute_gql_id
+ assert content["data"]["attribute"]["slug"] == swatch_attribute.slug
+ assert (
+ content["data"]["attribute"]["inputType"] == swatch_attribute.input_type.upper()
+ )
+ assert content["data"]["attribute"]["unit"] is None
+ assert (
+ content["data"]["attribute"]["valueRequired"] == swatch_attribute.value_required
+ )
+ assert (
+ content["data"]["attribute"]["visibleInStorefront"]
+ == swatch_attribute.visible_in_storefront
+ )
+ assert (
+ content["data"]["attribute"]["filterableInStorefront"]
+ == swatch_attribute.filterable_in_storefront
+ )
+ assert (
+ content["data"]["attribute"]["filterableInDashboard"]
+ == swatch_attribute.filterable_in_dashboard
+ )
+ assert (
+ content["data"]["attribute"]["availableInGrid"]
+ == swatch_attribute.available_in_grid
+ )
+ assert (
+ content["data"]["attribute"]["storefrontSearchPosition"]
+ == swatch_attribute.storefront_search_position
+ )
+ assert (
- len(content["data"]["attribute"]["values"])
++ len(content["data"]["attribute"]["choices"]["edges"])
+ == swatch_attribute.values.all().count()
+ )
+ attribute_value_data = []
+ for value in swatch_attribute.values.all():
+ data = {
- "slug": value.slug,
- "value": value.value,
- "inputType": value.input_type.upper(),
- "file": {"url": value.file_url, "contentType": value.content_type}
- if value.file_url
- else None,
++ "node": {
++ "slug": value.slug,
++ "value": value.value,
++ "inputType": value.input_type.upper(),
++ "file": {"url": value.file_url, "contentType": value.content_type}
++ if value.file_url
++ else None,
++ }
+ }
+ attribute_value_data.append(data)
+
+ for data in attribute_value_data:
- assert data in content["data"]["attribute"]["values"]
++ assert data in content["data"]["attribute"]["choices"]["edges"]
+
+
QUERY_ATTRIBUTES = """
query {
attributes(first: 20) {
diff --cc saleor/graphql/schema.graphql
index 46462f2a5a,3b53598114..cbf39f042a
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@@ -514,7 -528,7 +528,8 @@@ enum AttributeInputTypeEnum
REFERENCE
NUMERIC
RICH_TEXT
+ SWATCH
+ BOOLEAN
}
type AttributeReorderValues {
@@@ -666,17 -695,9 +698,17 @@@ type AttributeValueUpdate
attributeValue: AttributeValue
}
+input AttributeValueUpdateInput {
+ value: String
+ richText: JSONString
+ fileUrl: String
+ contentType: String
+ name: String
+}
+
input BulkAttributeValueInput {
id: ID
- values: [String]!
+ values: [String!]!
}
type BulkProductError {