Skip to content

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

Change format of export csv file

IKarbowiak58856ec2d743
diff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.py
index 707db68b90..70e9077551 100644
--- a/saleor/csv/utils/products_data.py
+++ b/saleor/csv/utils/products_data.py
@@ -45,7 +45,10 @@ class ProductExportFields:
"cost_price_amount": "cost price",
"variant_currency": "variant_currency",
},
- "common": {"images": "images"},
+ "images": {
+ "product_images": "product_images",
+ "variant_images": "variant_images",
+ },
}
@@ -86,9 +89,7 @@ def prepare_products_data(
relations_data = product_relations_data.get(pk, {})
data = {**product_data, **relations_data}
- products_with_variants_data.append(data)
-
- variants_data, *headers = prepare_variants_data(pk)
+ variants_data, *headers = prepare_variants_data(pk, data)
products_with_variants_data.extend(variants_data)
variants_attributes_fields.update(headers[0])
warehouse_fields.update(headers[1])
@@ -135,7 +136,7 @@ def prepare_product_relations_data(
if attribute_header:
attributes_headers.add(attribute_header)
- result_data = add_image_uris_to_data(pk, image, result_data)
+ result_data = add_image_uris_to_data(pk, image, "product_images", result_data)
result_data = add_collection_info_to_data(pk, collection, result_data)
result: Dict[int, Dict[str, str]] = {
@@ -165,7 +166,7 @@ def add_collection_info_to_data(
return result_data
-def prepare_variants_data(pk: int) -> Tuple[List[dict], set, set]:
+def prepare_variants_data(pk: int, product_data: dict) -> Tuple[List[dict], set, set]:
"""Prepare variants data for product with given pk.
This function gets product pk and prepared data about product's variants.
@@ -193,7 +194,7 @@ def prepare_variants_data(pk: int) -> Tuple[List[dict], set, set]:
)
result_data, variant_attributes_headers, warehouse_headers = update_variant_data(
- variants_data
+ variants_data, product_data
)
result = [
@@ -207,7 +208,7 @@ def prepare_variants_data(pk: int) -> Tuple[List[dict], set, set]:
def update_variant_data(
- variants_data: List[Dict[str, Union[str, int]]]
+ variants_data: List[Dict[str, Union[str, int]]], product_data: dict
) -> Tuple[Dict[int, dict], set, set]:
"""Update variant data with info about relations fields.
@@ -231,6 +232,8 @@ def update_variant_data(
image: str = data.pop("image_path") # type: ignore
if pk not in result_data:
+ # add product data to variant row
+ data.update(product_data)
result_data[pk] = data
result_data, attribute_header = add_attribute_info_to_data(
@@ -245,13 +248,13 @@ def update_variant_data(
if headers:
warehouse_headers.update(headers)
- result_data = add_image_uris_to_data(pk, image, result_data)
+ result_data = add_image_uris_to_data(pk, image, "variant_images", result_data)
return result_data, variant_attributes_headers, warehouse_headers
def add_image_uris_to_data(
- pk: int, image: str, result_data: Dict[int, dict]
+ pk: int, image: str, header: str, result_data: Dict[int, dict]
) -> Dict[int, dict]:
"""Add absolute uri of given image path to product or variant data.
@@ -260,7 +263,6 @@ def add_image_uris_to_data(
absolute uri of given image is added to set with other uris.
"""
if image:
- header = "images"
uri = build_absolute_uri(os.path.join(settings.MEDIA_URL, image))
if header in result_data[pk]:
result_data[pk][header].add(uri)
diff --git a/saleor/graphql/csv/types.py b/saleor/graphql/csv/types.py
index dde90704dc..1fb3cfd293 100644
--- a/saleor/graphql/csv/types.py
+++ b/saleor/graphql/csv/types.py
@@ -1,5 +1,4 @@
import graphene
-from graphene import relay
from graphql_jwt.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions
@@ -14,7 +13,7 @@ class ExportFile(CountableDjangoObjectType):
class Meta:
description = "Represents a job data of exported file."
- interfaces = [relay.Node, Job]
+ interfaces = [graphene.relay.Node, Job]
model = models.ExportFile
only_fields = ["id", "created_by", "url"]
diff --git a/tests/test_csv_products_data.py b/tests/test_csv_products_data.py
index ef3d00842f..9e4ac46e37 100644
--- a/tests/test_csv_products_data.py
+++ b/tests/test_csv_products_data.py
@@ -27,7 +27,7 @@ def test_get_products_data(mock_prepare_products_data, product_list):
**product_headers["product"],
**product_headers["product_many_to_many"],
**product_headers["variant"],
- **product_headers["common"],
+ **product_headers["images"],
}
expected_headers = list(expected_csv_headers.keys()) + attr_and_warehouse_headers
@@ -60,7 +60,7 @@ def test_prepare_products_data(product, product_with_image, collection, image):
product_data["product_currency"] = product.currency
product_data["product_type__name"] = product.product_type.name
product_data["id"] = product.pk
- product_data["images"] = (
+ product_data["product_images"] = (
""
if not product.images.all()
else "http://mirumee.com{}".format(product.images.first().image.url)
@@ -72,35 +72,32 @@ def test_prepare_products_data(product, product_with_image, collection, image):
product_data[header] = assigned_attribute.values.first().slug
expected_headers.add(header)
- expected_data.append(product_data)
-
for variant in product.variants.all():
- variant_data = {}
- variant_data["images"] = (
+ product_data["variant_images"] = (
""
if not variant.images.all()
else "http://mirumee.com{}".format(variant.images.first().image.url)
)
- variant_data["sku"] = variant.sku
- variant_data["variant_currency"] = variant.currency
- variant_data["price_override_amount"] = variant.price_override_amount
- variant_data["cost_price_amount"] = variant.cost_price_amount
+ product_data["sku"] = variant.sku
+ product_data["variant_currency"] = variant.currency
+ product_data["price_override_amount"] = variant.price_override_amount
+ product_data["cost_price_amount"] = variant.cost_price_amount
for stock in variant.stocks.all():
slug = stock.warehouse.slug
warehouse_headers = [
f"{slug} (warehouse quantity)",
]
- variant_data[warehouse_headers[0]] = stock.quantity
+ product_data[warehouse_headers[0]] = stock.quantity
expected_headers.update(warehouse_headers)
assigned_attribute = variant.attributes.first()
if assigned_attribute:
header = f"{assigned_attribute.attribute.slug} (attribute)"
- variant_data[header] = assigned_attribute.values.first().slug
+ product_data[header] = assigned_attribute.values.first().slug
expected_headers.add(header)
- expected_data.append(variant_data)
+ expected_data.append(product_data)
assert data == expected_data
assert set(headers) == expected_headers
@@ -122,7 +119,7 @@ def test_prepare_product_relations_data(product_with_image, collection_list):
for image in product_with_image.images.all()
]
)
- expected_result = {pk: {"collections": collections, "images": images}}
+ expected_result = {pk: {"collections": collections, "product_images": images}}
assigned_attribute = product_with_image.attributes.first()
if assigned_attribute:
@@ -138,7 +135,9 @@ def test_prepare_variants_data(product):
warehouse_headers = set()
attribute_headers = set()
- expected_result = {
+ data = {"id": 123, "name": "test_product"}
+
+ variant_data = {
"sku": variant.sku,
"cost_price_amount": variant.cost_price_amount,
"price_override_amount": variant.price_override_amount,
@@ -147,7 +146,7 @@ def test_prepare_variants_data(product):
assigned_attribute = variant.attributes.first()
if assigned_attribute:
header = f"{assigned_attribute.attribute.slug} (attribute)"
- expected_result[header] = assigned_attribute.values.first().slug
+ variant_data[header] = assigned_attribute.values.first().slug
attribute_headers.add(header)
for stock in variant.stocks.all():
@@ -155,13 +154,15 @@ def test_prepare_variants_data(product):
headers = [
f"{slug} (warehouse quantity)",
]
- expected_result[headers[0]] = stock.quantity
+ variant_data[headers[0]] = stock.quantity
warehouse_headers.update(headers)
result_data, res_attribute_headers, res_warehouse_headers = prepare_variants_data(
- product.pk
+ product.pk, data
)
+ expected_result = {**data, **variant_data}
+
assert result_data == [expected_result]
assert res_attribute_headers == attribute_headers
assert res_warehouse_headers == warehouse_headers
@@ -198,27 +199,31 @@ def test_add_collection_info_to_data_no_collection(product):
def test_add_image_uris_to_data(product):
pk = product.pk
image_path = "test/path/image.jpg"
+ field = "variant_images"
input_data = {pk: {}}
- result = add_image_uris_to_data(product.pk, image_path, input_data)
+ result = add_image_uris_to_data(product.pk, image_path, field, input_data)
- assert result[pk]["images"] == {"http://mirumee.com/media/" + image_path}
+ assert result[pk][field] == {"http://mirumee.com/media/" + image_path}
def test_add_image_uris_to_data_update_images(product):
pk = product.pk
old_path = "http://mirumee.com/media/test/image0.jpg"
image_path = "test/path/image.jpg"
- input_data = {pk: {"images": {old_path}}}
- result = add_image_uris_to_data(product.pk, image_path, input_data)
+ input_data = {pk: {"product_images": {old_path}}}
+ field = "product_images"
+ result = add_image_uris_to_data(product.pk, image_path, field, input_data)
- assert result[pk]["images"] == {"http://mirumee.com/media/" + image_path, old_path}
+ assert result[pk][field] == {"http://mirumee.com/media/" + image_path, old_path}
def test_add_image_uris_to_data_no_image_path(product):
pk = product.pk
image_path = None
input_data = {pk: {"name": "test"}}
- result = add_image_uris_to_data(product.pk, image_path, input_data)
+ result = add_image_uris_to_data(
+ product.pk, image_path, "product_images", input_data
+ )
assert result == input_data