Skip to content

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

Extend ExportProducts with export_info

IKarbowiak3670c150a6a8
diff --git a/saleor/graphql/csv/enums.py b/saleor/graphql/csv/enums.py
index c4b51b9bda..35733e9609 100644
--- a/saleor/graphql/csv/enums.py
+++ b/saleor/graphql/csv/enums.py
@@ -22,3 +22,17 @@ class ExportScope(graphene.Enum):
if self.name in description_mapping:
return description_mapping[self.name]
raise ValueError("Unsupported enum value: %s" % self.value)
+
+
+class ProductFieldEnum(graphene.Enum):
+ NAME = "name"
+ DESCRIPTION = "description"
+ PRODUCT_TYPE = "product_type"
+ CATEGORY = "category"
+ VISIBLE = "visible"
+ COLLECTIONS = "collections"
+ PRICE = "price"
+ PRICE_OVERRIDE = "price_override"
+ IMAGES = "images"
+ WEIGHT = "weight"
+ CHARGE_TAXES = "charge_taxes"
diff --git a/saleor/graphql/csv/mutations.py b/saleor/graphql/csv/mutations.py
index 88c7e984f5..ca4dc2aa5e 100644
--- a/saleor/graphql/csv/mutations.py
+++ b/saleor/graphql/csv/mutations.py
@@ -11,12 +11,28 @@ from ..core.enums import CsvErrorCode
from ..core.mutations import BaseMutation
from ..core.types.common import CsvError
from ..product.filters import ProductFilterInput
-from ..product.types import Product
+from ..product.types import Attribute, Product
from ..utils import resolve_global_ids_to_primary_keys
-from .enums import ExportScope
+from ..warehouse.types import Warehouse
+from .enums import ExportScope, ProductFieldEnum
from .types import ExportFile
+class ExportInfoInput(graphene.InputObjectType):
+ attributes = graphene.List(
+ graphene.NonNull(graphene.ID),
+ description="List of attribute ids witch should be exported.",
+ )
+ warehouses = graphene.List(
+ graphene.NonNull(graphene.ID),
+ description="List of warehouse ids witch should be exported.",
+ )
+ fields = graphene.List(
+ graphene.NonNull(ProductFieldEnum),
+ description="List of product fields witch should be exported.",
+ )
+
+
class ExportProductsInput(graphene.InputObjectType):
scope = ExportScope(
description="Determine which products should be exported.", required=True
@@ -29,6 +45,10 @@ class ExportProductsInput(graphene.InputObjectType):
description="List of products IDS to export.",
required=False,
)
+ export_info = ExportInfoInput(
+ description="Input with info about fields which should be exported.",
+ required=True,
+ )
class ExportProducts(BaseMutation):
@@ -53,7 +73,9 @@ class ExportProducts(BaseMutation):
@classmethod
def perform_mutation(cls, root, info, **data):
user = info.context.user
- scope = cls.get_products_scope(info, data["input"])
+ input = data["input"]
+ scope = cls.get_products_scope(input)
+ cls.get_export_info(input["export_info"])
export_file = csv_models.ExportFile.objects.create(created_by=user)
export_started_event(export_file=export_file, user=user)
export_products.delay(export_file.pk, scope)
@@ -61,7 +83,7 @@ class ExportProducts(BaseMutation):
return cls(export_file=export_file)
@classmethod
- def get_products_scope(cls, info, input) -> Mapping[str, Union[list, dict, str]]:
+ def get_products_scope(cls, input) -> Mapping[str, Union[list, dict, str]]:
scope = input["scope"]
if scope == ExportScope.IDS.value: # type: ignore
return cls.clean_ids(input)
@@ -97,3 +119,23 @@ class ExportProducts(BaseMutation):
}
)
return {"filter": filter}
+
+ @staticmethod
+ def get_export_info(export_info_input):
+ export_info = {}
+ fields = export_info_input.get("fields")
+ attribute_ids = export_info_input.get("attributes")
+ warehouse_ids = export_info_input.get("warehouses")
+ if fields:
+ export_info["fields"] = fields
+ if attribute_ids:
+ _, attribute_pks = resolve_global_ids_to_primary_keys(
+ attribute_ids, graphene_type=Attribute
+ )
+ export_info["attributes"] = attribute_pks
+ if warehouse_ids:
+ _, warehouse_pks = resolve_global_ids_to_primary_keys(
+ warehouse_ids, graphene_type=Warehouse
+ )
+ export_info["warehouses"] = warehouse_pks
+ return export_info
diff --git a/saleor/graphql/csv/types.py b/saleor/graphql/csv/types.py
index 44c76682b9..c39c2dbee7 100644
--- a/saleor/graphql/csv/types.py
+++ b/saleor/graphql/csv/types.py
@@ -23,7 +23,7 @@ class ExportEvent(CountableDjangoObjectType):
class Meta:
description = "History log of export file."
model = models.ExportEvent
- interfaces = [relay.Node]
+ interfaces = [graphene.relay.Node]
only_fields = ["id"]
@staticmethod
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1694bfbb33..b71bfe1ddc 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1812,6 +1812,12 @@ input ExportFileSortingInput {
field: ExportFileSortField!
}
+input ExportInfoInput {
+ attributes: [ID!]
+ warehouses: [ID!]
+ fields: [ProductFieldEnum!]
+}
+
type ExportProducts {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.")
exportFile: ExportFile
@@ -1822,6 +1828,7 @@ input ExportProductsInput {
scope: ExportScope!
filter: ProductFilterInput
ids: [ID!]
+ exportInfo: ExportInfoInput!
}
enum ExportScope {
@@ -3520,6 +3527,20 @@ enum ProductErrorCode {
VARIANT_NO_DIGITAL_CONTENT
}
+enum ProductFieldEnum {
+ NAME
+ DESCRIPTION
+ PRODUCT_TYPE
+ CATEGORY
+ VISIBLE
+ COLLECTIONS
+ PRICE
+ PRICE_OVERRIDE
+ IMAGES
+ WEIGHT
+ CHARGE_TAXES
+}
+
input ProductFilterInput {
isPublished: Boolean
collections: [ID]
diff --git a/tests/api/test_csv.py b/tests/api/test_csv.py
index 2b419e6852..afcaa96d4c 100644
--- a/tests/api/test_csv.py
+++ b/tests/api/test_csv.py
@@ -38,9 +38,13 @@ EXPORT_PRODUCTS_MUTATION = """
@pytest.mark.parametrize(
"input, called_data",
[
- ({"scope": ExportScope.ALL.name}, {"all": ""}),
+ ({"scope": ExportScope.ALL.name, "exportInfo": {}}, {"all": ""}),
(
- {"scope": ExportScope.FILTER.name, "filter": {"isPublished": True}},
+ {
+ "scope": ExportScope.FILTER.name,
+ "filter": {"isPublished": True},
+ "exportInfo": {},
+ },
{"filter": {"is_published": True}},
),
],
@@ -91,7 +95,7 @@ def test_export_products_mutation_ids_scope(
pks.add(str(product.pk))
ids.append(graphene.Node.to_global_id("Product", product.pk))
- variables = {"input": {"scope": ExportScope.IDS.name, "ids": ids}}
+ variables = {"input": {"scope": ExportScope.IDS.name, "ids": ids, "exportInfo": {}}}
response = staff_api_client.post_graphql(
query, variables=variables, permissions=[permission_manage_products]
@@ -117,8 +121,8 @@ def test_export_products_mutation_ids_scope(
@pytest.mark.parametrize(
"input, error_field",
[
- ({"scope": ExportScope.FILTER.name}, "filter"),
- ({"scope": ExportScope.IDS.name}, "ids"),
+ ({"scope": ExportScope.FILTER.name, "exportInfo": {}}, "filter"),
+ ({"scope": ExportScope.IDS.name, "exportInfo": {}}, "ids"),
],
)
@patch("saleor.graphql.csv.mutations.export_products.delay")