Skip to content

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

Prepare function for export products to csv

IKarbowiak0367b5ec545c
diff --git a/.isort.cfg b/.isort.cfg
index 43fbd9a126..e51a90e25b 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,jwt,markdown,measurement,mptt,oauthlib,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,six,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
+known_third_party = PIL,babel,bleach,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,jwt,markdown,measurement,mptt,oauthlib,petl,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,six,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
diff --git a/saleor/csv/utils.py b/saleor/csv/utils.py
new file mode 100644
index 0000000000..30b57aaa30
--- /dev/null
+++ b/saleor/csv/utils.py
@@ -0,0 +1,58 @@
+from typing import TYPE_CHECKING, Union
+
+import petl as etl
+
+if TYPE_CHECKING:
+ # flake8: noqa
+ from ..product.models import Product, ProductVariant
+
+
+def export_products(queryset, delimiter=";"):
+ file_name = "product_data.csv"
+ headers_mapping = {
+ "product_type__name": "product_type",
+ "category__slug": "category",
+ "is_published": "visible",
+ "price_amount": "price",
+ }
+
+ attributes_fields = set()
+ fields = ["id", "name", "description"] + headers_mapping.keys()
+ products_with_variants_data = []
+ products_data = queryset.values(*fields)
+
+ for product, product_data in zip(queryset, products_data):
+ product_data["collections"] = ", ".join(
+ product.collections.values_list("slug", flat=True)
+ )
+ product_data["images"] = get_image_file_names(product)
+ product_attributes_data = prepare_attributes_data(product)
+ attributes_fields.update(product_attributes_data.keys())
+ product_data.update(product_attributes_data)
+ products_with_variants_data.append(product_data)
+
+ fields += list(attributes_fields)
+ table = etl.fromdicts(products_with_variants_data, header=fields, missing="-")
+ table = etl.rename(table, headers_mapping)
+
+ etl.tocsv(table, file_name, delimiter=delimiter)
+
+
+def get_image_file_names(product: "Product"):
+ return ", ".join(
+ [
+ image.split("/")[1]
+ for image in product.images.values_list("image", flat=True)
+ ]
+ )
+
+
+def prepare_attributes_data(instance: Union["Product", "ProductVariant"]):
+ attribute_values = {}
+ for assigned_attribute in instance.attributes.all():
+ attribute_slug = assigned_attribute.attribute.slug
+ attribute_header = "attribute_{}".format(attribute_slug.replace("-", "_"))
+ attribute_values[attribute_header] = ", ".join(
+ assigned_attribute.values.values_list("slug", flat=True)
+ )
+ return attribute_values