Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Export attribute references
IKarbowiakab26a1f16eb1
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.pyindex 5c1bcea68e..5dbc1bae1a 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@@ -1,6 +1,6 @@from measurement.measures import Weight-from .....attribute.models import Attribute+from .....attribute.models import Attribute, AttributeValuefrom .....attribute.utils import associate_attribute_values_to_instancefrom .....channel.models import Channelfrom .....product.models import Product, ProductVariant, VariantImage@@ -227,17 +227,39 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(product_with_image,product_with_variant_with_two_attributes,file_attribute,+ page_reference_attribute,+ page_list,):# givenproduct.variants.add(variant_with_many_stocks)- product.product_type.variant_attributes.add(file_attribute)- product.product_type.product_attributes.add(file_attribute)+ product.product_type.variant_attributes.add(+ file_attribute, page_reference_attribute+ )+ product.product_type.product_attributes.add(+ file_attribute, page_reference_attribute+ )associate_attribute_values_to_instance(variant_with_many_stocks, file_attribute, file_attribute.values.first())associate_attribute_values_to_instance(product, file_attribute, file_attribute.values.first())+ product_ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{product.pk}_{page_list[0].pk}",+ name=page_list[0].title,+ )+ variant_ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{variant_with_many_stocks.pk}_{page_list[1].pk}",+ name=page_list[1].title,+ )+ associate_attribute_values_to_instance(+ variant_with_many_stocks, page_reference_attribute, variant_ref_value+ )+ associate_attribute_values_to_instance(+ product, page_reference_attribute, product_ref_value+ )products = Product.objects.all()export_fields = {"id", "variants__sku"}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.pyindex 1606f6c4b1..dbf5401696 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@@ -1,6 +1,6 @@from unittest.mock import patch-from .....attribute.models import Attribute+from .....attribute.models import Attribute, AttributeValuefrom .....attribute.utils import associate_attribute_values_to_instancefrom .....product.models import Product, ProductImage, VariantImagefrom .....warehouse.models import Warehouse@@ -74,14 +74,26 @@ def test_get_products_relations_data_no_relations_fields(@patch("saleor.csv.utils.products_data.prepare_products_relations_data")def test_get_products_relations_data_attribute_ids(- prepare_products_data_mocked, product_list, file_attribute+ prepare_products_data_mocked,+ product_list,+ file_attribute,+ page_reference_attribute,+ page,):# givenproduct = product_list[0]- product.product_type.product_attributes.add(file_attribute)+ product.product_type.product_attributes.add(+ file_attribute, page_reference_attribute+ )associate_attribute_values_to_instance(product, file_attribute, file_attribute.values.first())+ ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{product.pk}_{page.pk}",+ name=page.title,+ )+ associate_attribute_values_to_instance(product, page_reference_attribute, ref_value)qs = Product.objects.all()export_fields = {"name", "description"}@@ -123,15 +135,31 @@ def test_get_products_relations_data_channel_ids(def test_prepare_products_relations_data(- product_with_image, collection_list, channel_USD, channel_PLN, file_attribute+ product_with_image,+ collection_list,+ channel_USD,+ channel_PLN,+ file_attribute,+ page_reference_attribute,+ page,):# givenpk = product_with_image.pk- product_with_image.product_type.product_attributes.add(file_attribute)+ product_with_image.product_type.product_attributes.add(+ file_attribute, page_reference_attribute+ )associate_attribute_values_to_instance(product_with_image, file_attribute, file_attribute.values.first())+ ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{product_with_image.pk}_{page.pk}",+ name=page.title,+ )+ associate_attribute_values_to_instance(+ product_with_image, page_reference_attribute, ref_value+ )collection_list[0].products.add(product_with_image)collection_list[1].products.add(product_with_image)@@ -302,15 +330,27 @@ def test_get_variants_relations_data_no_relations_fields(@patch("saleor.csv.utils.products_data.prepare_variants_relations_data")def test_get_variants_relations_data_attribute_ids(- prepare_variants_data_mocked, product_list, file_attribute+ prepare_variants_data_mocked,+ product_list,+ file_attribute,+ page_reference_attribute,+ page,):# givenproduct = product_list[0]- product.product_type.variant_attributes.add(file_attribute)+ product.product_type.variant_attributes.add(+ file_attribute, page_reference_attribute+ )variant = product.variants.first()associate_attribute_values_to_instance(variant, file_attribute, file_attribute.values.first())+ ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{variant.pk}_{page.pk}",+ name=page.title,+ )+ associate_attribute_values_to_instance(variant, page_reference_attribute, ref_value)qs = Product.objects.all()export_fields = {"name", "variants__sku"}@@ -414,14 +454,24 @@ def test_prepare_variants_relations_data(channel_PLN,channel_USD,file_attribute,+ page_reference_attribute,+ page,):# givenproduct = product_with_variant_with_two_attributes- product.product_type.variant_attributes.add(file_attribute)+ product.product_type.variant_attributes.add(+ file_attribute, page_reference_attribute+ )variant = product.variants.first()associate_attribute_values_to_instance(variant, file_attribute, file_attribute.values.first())+ ref_value = AttributeValue.objects.create(+ attribute=page_reference_attribute,+ slug=f"{variant.pk}_{page.pk}",+ name=page.title,+ )+ associate_attribute_values_to_instance(variant, page_reference_attribute, ref_value)qs = Product.objects.all()variant = product_with_variant_with_two_attributes.variants.first()@@ -675,7 +725,7 @@ def test_add_attribute_info_to_data(product):slug = "test_attribute_slug"value = "test value"attribute_data = AttributeData(- slug=slug, value=value, file_url=None, input_type="dropdown"+ slug=slug, value=value, file_url=None, input_type="dropdown", entity_type=None)input_data = {pk: {}}@@ -697,7 +747,7 @@ def test_add_attribute_info_to_data_update_attribute_data(product):expected_header = f"{slug} (variant attribute)"attribute_data = AttributeData(- slug=slug, value=value, file_url=None, input_type="dropdown"+ slug=slug, value=value, file_url=None, input_type="dropdown", entity_type=None)input_data = {pk: {expected_header: {"value1"}}}@@ -714,7 +764,7 @@ def test_add_attribute_info_to_data_no_slug(product):# givenpk = product.pkattribute_data = AttributeData(- slug=None, value=None, file_url=None, input_type="dropdown"+ slug=None, value=None, file_url=None, input_type="dropdown", entity_type=None)input_data = {pk: {}}@@ -733,7 +783,7 @@ def test_add_file_attribute_info_to_data(product):slug = "testtxt"test_url = "test.txt"attribute_data = AttributeData(- slug=slug, value=None, file_url=test_url, input_type="file"+ slug=slug, value=None, file_url=test_url, input_type="file", entity_type=None)input_data = {pk: {}}@@ -747,6 +797,57 @@ def test_add_file_attribute_info_to_data(product):assert result[pk][expected_header] == {"http://mirumee.com/media/" + test_url}+def test_add_reference_attribute_info_to_data(product, page):+ # given+ pk = product.pk+ slug = "test_attribute_slug"+ value = f"{product.id}_{page.id}"+ attribute_data = AttributeData(+ slug=slug,+ value=value,+ file_url=None,+ input_type="reference",+ entity_type="Page",+ )+ 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] == {f"Page_{page.id}"}+++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}"+ expected_header = f"{slug} (variant attribute)"+ values = {"Page_989"}++ attribute_data = AttributeData(+ slug=slug,+ value=value,+ file_url=None,+ input_type="reference",+ entity_type="Page",+ )+ input_data = {pk: {expected_header: values}}++ # when+ result = add_attribute_info_to_data(+ product.pk, attribute_data, "variant attribute", input_data+ )++ # then+ values.add(f"Page_{page.id}")+ assert result[pk][expected_header] == values++def test_add_warehouse_info_to_data(product):# givenpk = product.pkdiff --git a/saleor/csv/tests/export/products_data/utils.py b/saleor/csv/tests/export/products_data/utils.pyindex 0de4552ecf..2dcb1bfbf9 100644--- a/saleor/csv/tests/export/products_data/utils.py+++ b/saleor/csv/tests/export/products_data/utils.py@@ -6,13 +6,7 @@ def add_product_attribute_data_to_expected_data(data, product, attribute_ids, pkif assigned_attribute:header = f"{assigned_attribute.attribute.slug} (product attribute)"if str(assigned_attribute.attribute.pk) in attribute_ids:- value_instance = assigned_attribute.values.first()- value = (- value_instance.slug- if assigned_attribute.attribute.input_type- != AttributeInputType.FILE- else value_instance.file_url- )+ value = get_attribute_value(assigned_attribute)if pk:data[pk][header] = valueelse:@@ -24,12 +18,7 @@ def add_variant_attribute_data_to_expected_data(data, variant, attribute_ids, pkfor assigned_attribute in variant.attributes.all():header = f"{assigned_attribute.attribute.slug} (variant attribute)"if str(assigned_attribute.attribute.pk) in attribute_ids:- value_instance = assigned_attribute.values.first()- value = (- value_instance.slug- if assigned_attribute.attribute.input_type != AttributeInputType.FILE- else value_instance.file_url- )+ value = get_attribute_value(assigned_attribute)if pk:data[pk][header] = valueelse:@@ -38,6 +27,19 @@ def add_variant_attribute_data_to_expected_data(data, variant, attribute_ids, pkreturn data+def get_attribute_value(assigned_attribute):+ value_instance = assigned_attribute.values.first()+ attribute = assigned_attribute.attribute+ if attribute.input_type == AttributeInputType.FILE:+ value = value_instance.file_url+ elif attribute.input_type == AttributeInputType.REFERENCE:+ ref_id = value_instance.slug.split("_")[1]+ value = f"{attribute.entity_type}_{ref_id}"+ else:+ value = value_instance.slug+ return value++def add_stocks_to_expected_data(data, variant, warehouse_ids, pk=None):for stock in variant.stocks.all():if str(stock.warehouse.pk) in warehouse_ids:diff --git a/saleor/csv/utils/__init__.py b/saleor/csv/utils/__init__.pyindex 4f749b2fe6..f99f0ab1ad 100644--- a/saleor/csv/utils/__init__.py+++ b/saleor/csv/utils/__init__.py@@ -25,6 +25,7 @@ class ProductExportFields:"file_url": "attributes__values__file_url","slug": "attributes__assignment__attribute__slug","input_type": "attributes__assignment__attribute__input_type",+ "entity_type": "attributes__assignment__attribute__entity_type","attribute_pk": "attributes__assignment__attribute__pk",}@@ -49,6 +50,7 @@ class ProductExportFields:"file_url": "variants__attributes__values__file_url","slug": "variants__attributes__assignment__attribute__slug","input_type": "variants__attributes__assignment__attribute__input_type",+ "entity_type": "variants__attributes__assignment__attribute__entity_type","attribute_pk": "variants__attributes__assignment__attribute__pk",}diff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.pyindex d4ed69e9ee..8f345e5df6 100644--- a/saleor/csv/utils/products_data.py+++ b/saleor/csv/utils/products_data.py@@ -11,7 +11,6 @@ from ...core.utils import build_absolute_urifrom . import ProductExportFieldsif TYPE_CHECKING:- # flake8: noqafrom django.db.models import QuerySet@@ -287,7 +286,9 @@ def add_image_uris_to_data(return result_data-AttributeData = namedtuple("AttributeData", ["slug", "file_url", "value", "input_type"])+AttributeData = namedtuple(+ "AttributeData", ["slug", "file_url", "value", "input_type", "entity_type"]+)def handle_attribute_data(@@ -304,6 +305,7 @@ def handle_attribute_data(input_type=data.pop(attribute_fields["input_type"], None),file_url=data.pop(attribute_fields["file_url"], None),value=data.pop(attribute_fields["value"], None),+ entity_type=data.pop(attribute_fields["entity_type"], None),)if attribute_ids and attribute_pk in attribute_ids:@@ -378,13 +380,16 @@ def add_attribute_info_to_data(header = Noneif slug:header = f"{slug} ({attribute_owner})"- value = (- attribute_data.value- if attribute_data.input_type != AttributeInputType.FILE- else build_absolute_uri(+ input_type = attribute_data.input_type+ if input_type == AttributeInputType.FILE:+ value = build_absolute_uri(urljoin(settings.MEDIA_URL, attribute_data.file_url))- )+ elif input_type == AttributeInputType.REFERENCE:+ reference_id = attribute_data.value.split("_")[1]+ value = f"{attribute_data.entity_type}_{reference_id}"+ else:+ value = attribute_data.valueif header in result_data[pk]:result_data[pk][header].add(value) # type: ignoreelse: