Skip to content

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

Allow exporting swatch attributes (#6818)

Iga Karbowiak62a03f0bfb02
diff --git a/saleor/csv/tests/export/products_data/test_get_products_data.py b/saleor/csv/tests/export/products_data/test_get_products_data.py
index 44b366199a..ce4c51202b 100644
--- a/saleor/csv/tests/export/products_data/test_get_products_data.py
+++ b/saleor/csv/tests/export/products_data/test_get_products_data.py
@@ -241,6 +241,7 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
rich_text_attribute,
color_attribute,
variant_with_many_stocks,
+ swatch_attribute,
):
# given
product.variants.add(variant_with_many_stocks)
@@ -250,6 +251,7 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
product_type_product_reference_attribute,
numeric_attribute,
rich_text_attribute,
+ swatch_attribute,
)
product.product_type.product_attributes.add(
file_attribute,
@@ -257,6 +259,7 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
product_type_product_reference_attribute,
numeric_attribute,
rich_text_attribute,
+ swatch_attribute,
)
# add rich text attribute
@@ -328,6 +331,15 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
assigned_product = product.attributes.get(assignment__attribute=color_attribute)
assigned_product.values.clear()
+ # add swatch attribute
+ swatch_value_1 = swatch_attribute.values.first()
+ swatch_value_2 = swatch_attribute.values.last()
+
+ associate_attribute_values_to_instance(
+ variant_with_many_stocks, swatch_attribute, swatch_value_1
+ )
+ associate_attribute_values_to_instance(product, swatch_attribute, swatch_value_2)
+
products = Product.objects.all()
export_fields = {"id", "variants__sku"}
warehouse_ids = [str(warehouse.pk) for warehouse in Warehouse.objects.all()]
diff --git a/saleor/csv/tests/export/products_data/test_handle_relations_data.py b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
index ce80bbc3ac..dc9f2bcaec 100644
--- a/saleor/csv/tests/export/products_data/test_handle_relations_data.py
+++ b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
@@ -790,10 +790,13 @@ def test_add_attribute_info_to_data(product):
# given
pk = product.pk
slug = "test_attribute_slug"
- value = "test value"
+ value_name = "test value"
+ value_slug = "test-value"
attribute_data = AttributeData(
slug=slug,
- value=value,
+ value_name=value_name,
+ value_slug=value_slug,
+ value=None,
file_url=None,
input_type="dropdown",
entity_type=None,
@@ -809,19 +812,21 @@ def test_add_attribute_info_to_data(product):
# then
expected_header = f"{slug} (product attribute)"
- assert result[pk][expected_header] == {value}
+ assert result[pk][expected_header] == {value_name}
def test_add_attribute_info_to_data_update_attribute_data(product):
# given
pk = product.pk
slug = "test_attribute_slug"
- value = "test value"
+ value_slug = "test-value"
expected_header = f"{slug} (variant attribute)"
attribute_data = AttributeData(
slug=slug,
- value=value,
+ value_slug=value_slug,
+ value_name=None,
+ value=None,
file_url=None,
input_type="dropdown",
entity_type=None,
@@ -836,7 +841,7 @@ def test_add_attribute_info_to_data_update_attribute_data(product):
)
# then
- assert result[pk][expected_header] == {value, "value1"}
+ assert result[pk][expected_header] == {value_slug, "value1"}
def test_add_attribute_info_to_data_no_slug(product):
@@ -844,6 +849,8 @@ def test_add_attribute_info_to_data_no_slug(product):
pk = product.pk
attribute_data = AttributeData(
slug=None,
+ value_slug=None,
+ value_name=None,
value=None,
file_url=None,
input_type="dropdown",
@@ -868,6 +875,8 @@ def test_add_attribute_info_when_no_value(product):
slug = "test_attribute_slug"
attribute_data = AttributeData(
slug=slug,
+ value_slug=None,
+ value_name=None,
value=None,
file_url=None,
input_type="dropdown",
@@ -894,6 +903,8 @@ def test_add_file_attribute_info_to_data(product):
test_url = "test.txt"
attribute_data = AttributeData(
slug=slug,
+ value_slug=None,
+ value_name=None,
value=None,
file_url=test_url,
input_type="file",
@@ -919,6 +930,8 @@ def test_add_rich_text_attribute_info_to_data(product):
slug = "testtxt"
attribute_data = AttributeData(
slug=slug,
+ value_slug=None,
+ value_name=None,
value=None,
file_url=None,
input_type="rich-text",
@@ -942,10 +955,12 @@ def test_add_reference_attribute_info_to_data(product, page):
# given
pk = product.pk
slug = "test_attribute_slug"
- value = f"{product.id}_{page.id}"
+ value_slug = f"{product.id}_{page.id}"
attribute_data = AttributeData(
slug=slug,
- value=value,
+ value_slug=value_slug,
+ value_name=None,
+ value=None,
file_url=None,
input_type="reference",
entity_type="Page",
@@ -968,13 +983,15 @@ def test_add_reference_info_to_data_update_attribute_data(product, page):
# given
pk = product.pk
slug = "test_attribute_slug"
- value = f"{product.id}_{page.id}"
+ value_slug = f"{product.id}_{page.id}"
expected_header = f"{slug} (variant attribute)"
values = {"Page_989"}
attribute_data = AttributeData(
slug=slug,
- value=value,
+ value_slug=value_slug,
+ value_name=None,
+ value=None,
file_url=None,
input_type="reference",
entity_type="Page",
@@ -996,10 +1013,13 @@ def test_add_reference_info_to_data_update_attribute_data(product, page):
def test_add_numeric_attribute_info_to_data(product, numeric_attribute):
# given
pk = product.pk
- value = "12.3"
+ name = "12.3"
+ slug = "12_3"
attribute_data = AttributeData(
slug=numeric_attribute.slug,
- value=value,
+ value_slug=slug,
+ value_name=name,
+ value=None,
file_url=None,
input_type="numeric",
entity_type=None,
@@ -1015,16 +1035,19 @@ def test_add_numeric_attribute_info_to_data(product, numeric_attribute):
# then
expected_header = f"{numeric_attribute.slug} (product attribute)"
- assert result[pk][expected_header] == {f"{value} {numeric_attribute.unit}"}
+ assert result[pk][expected_header] == {f"{name} {numeric_attribute.unit}"}
def test_add_numeric_attribute_info_to_data_no_unit(product, numeric_attribute):
# given
pk = product.pk
- value = "12.3"
+ name = "12.3"
+ slug = "12_3"
attribute_data = AttributeData(
slug=numeric_attribute.slug,
- value=value,
+ value_slug=slug,
+ value_name=name,
+ value=None,
file_url=None,
input_type="numeric",
entity_type=None,
@@ -1040,6 +1063,34 @@ def test_add_numeric_attribute_info_to_data_no_unit(product, numeric_attribute):
# 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,
+ )
+ 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}
@@ -1047,10 +1098,12 @@ def test_add_attribute_info_to_data_no_file_url_for_file_attribute(product):
# given
pk = product.pk
slug = "test_attribute_slug"
- value = "test value"
+ value_slug = "test-value"
attribute_data = AttributeData(
+ value_slug=value_slug,
slug=slug,
- value=value,
+ value=None,
+ value_name=None,
file_url=None,
input_type="file",
entity_type=None,
@@ -1073,10 +1126,12 @@ def test_add_attribute_info_to_data_no_rich_text_for_rich_text_attribute(product
# given
pk = product.pk
slug = "test_attribute_slug"
- value = "test value"
+ value_slug = "test-value"
attribute_data = AttributeData(
slug=slug,
- value=value,
+ value_slug=value_slug,
+ value=None,
+ value_name=None,
file_url=None,
input_type="rich-text",
entity_type=None,
@@ -1101,6 +1156,8 @@ def test_add_attribute_info_to_data_no_value_for_reference_attribute(product):
slug = "test_attribute_slug"
attribute_data = AttributeData(
slug=slug,
+ value_slug=None,
+ value_name=None,
value=None,
file_url=None,
input_type="reference",
@@ -1120,6 +1177,34 @@ def test_add_attribute_info_to_data_no_value_for_reference_attribute(product):
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,
+ )
+ 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 --git a/saleor/csv/tests/export/products_data/utils.py b/saleor/csv/tests/export/products_data/utils.py
index 67e66095a0..4ef1cfa293 100644
--- a/saleor/csv/tests/export/products_data/utils.py
+++ b/saleor/csv/tests/export/products_data/utils.py
@@ -44,8 +44,12 @@ def get_attribute_value(assigned_attribute):
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
+ )
else:
- value = value_instance.slug
+ value = value_instance.name or value_instance.slug
return value
diff --git a/saleor/csv/utils/__init__.py b/saleor/csv/utils/__init__.py
index 0f157a750b..d666e5e7b8 100644
--- a/saleor/csv/utils/__init__.py
+++ b/saleor/csv/utils/__init__.py
@@ -21,9 +21,11 @@ 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",
"slug": "attributes__assignment__attribute__slug",
"input_type": "attributes__assignment__attribute__input_type",
"entity_type": "attributes__assignment__attribute__entity_type",
@@ -48,9 +50,11 @@ class ProductExportFields:
}
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",
"slug": "variants__attributes__assignment__attribute__slug",
"input_type": "variants__attributes__assignment__attribute__input_type",
"entity_type": "variants__attributes__assignment__attribute__entity_type",
diff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.py
index 138a5e3551..5896e03c11 100644
--- a/saleor/csv/utils/products_data.py
+++ b/saleor/csv/utils/products_data.py
@@ -293,7 +293,17 @@ def add_image_uris_to_data(
AttributeData = namedtuple(
"AttributeData",
- ["slug", "file_url", "value", "input_type", "entity_type", "unit", "rich_text"],
+ [
+ "slug",
+ "input_type",
+ "entity_type",
+ "unit",
+ "value_slug",
+ "value_name",
+ "value",
+ "file_url",
+ "rich_text",
+ ],
)
@@ -310,6 +320,8 @@ def handle_attribute_data(
slug=data.pop(attribute_fields["slug"], None),
input_type=data.pop(attribute_fields["input_type"], None),
file_url=data.pop(attribute_fields["file_url"], None),
+ value_slug=data.pop(attribute_fields["value_slug"], None),
+ value_name=data.pop(attribute_fields["value_name"], None),
value=data.pop(attribute_fields["value"], None),
entity_type=data.pop(attribute_fields["entity_type"], None),
unit=data.pop(attribute_fields["unit"], None),
@@ -386,32 +398,51 @@ 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.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)
- 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 --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 976d8a0988..4e54bad945 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -877,8 +877,8 @@ def numeric_attribute(db):
filterable_in_dashboard=True,
available_in_grid=True,
)
- AttributeValue.objects.create(attribute=attribute, name="10", slug="10")
- AttributeValue.objects.create(attribute=attribute, name="15", slug="15")
+ AttributeValue.objects.create(attribute=attribute, name="9.5", slug="10_5")
+ AttributeValue.objects.create(attribute=attribute, name="15.2", slug="15_2")
return attribute