Skip to content

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

Add fileType to ExportProducts mutation

IKarbowiak8220a53473bb
diff --git a/saleor/csv/utils/export.py b/saleor/csv/utils/export.py
index 3d9e338f65..6d424f254e 100644
--- a/saleor/csv/utils/export.py
+++ b/saleor/csv/utils/export.py
@@ -46,7 +46,7 @@ def export_products(
export_file_id: int,
scope: Dict[str, Union[str, dict]],
export_info: Dict[str, list],
- file_type: str = FileTypes.CSV,
+ file_type: str,
delimiter: str = ";",
):
file_name = get_filename("product", file_type)
diff --git a/saleor/graphql/csv/enums.py b/saleor/graphql/csv/enums.py
index 6a54156939..9b2155c275 100644
--- a/saleor/graphql/csv/enums.py
+++ b/saleor/graphql/csv/enums.py
@@ -1,9 +1,10 @@
import graphene
-from ...csv import ExportEvents
+from ...csv import ExportEvents, FileTypes
from ...graphql.core.enums import to_enum
ExportEventEnum = to_enum(ExportEvents)
+FileTypeEnum = to_enum(FileTypes)
class ExportScope(graphene.Enum):
diff --git a/saleor/graphql/csv/mutations.py b/saleor/graphql/csv/mutations.py
index 8000df6ff7..e62402de7a 100644
--- a/saleor/graphql/csv/mutations.py
+++ b/saleor/graphql/csv/mutations.py
@@ -14,7 +14,7 @@ from ..product.filters import ProductFilterInput
from ..product.types import Attribute, Product
from ..utils import resolve_global_ids_to_primary_keys
from ..warehouse.types import Warehouse
-from .enums import ExportScope, ProductFieldEnum
+from .enums import ExportScope, FileTypeEnum, ProductFieldEnum
from .types import ExportFile
@@ -49,6 +49,7 @@ class ExportProductsInput(graphene.InputObjectType):
description="Input with info about fields which should be exported.",
required=True,
)
+ file_type = FileTypeEnum(description="Type of exported file.", required=True)
class ExportProducts(BaseMutation):
@@ -76,9 +77,12 @@ class ExportProducts(BaseMutation):
input = data["input"]
scope = cls.get_products_scope(input)
export_info = cls.get_export_info(input["export_info"])
+ file_type = input["file_type"]
+
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, export_info)
+ export_products.delay(export_file.pk, scope, export_info, file_type)
+
export_file.refresh_from_db()
return cls(export_file=export_file)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index fac38ac4ca..9dc43e2789 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1829,6 +1829,7 @@ input ExportProductsInput {
filter: ProductFilterInput
ids: [ID!]
exportInfo: ExportInfoInput!
+ fileType: FileTypesEnum!
}
enum ExportScope {
@@ -1837,6 +1838,11 @@ enum ExportScope {
FILTER
}
+enum FileTypesEnum {
+ CSV
+ XLSX
+}
+
type Fulfillment implements Node & ObjectWithMetadata {
id: ID!
fulfillmentOrder: Int!
diff --git a/tests/api/test_csv.py b/tests/api/test_csv.py
index 07bbdef450..862276478c 100644
--- a/tests/api/test_csv.py
+++ b/tests/api/test_csv.py
@@ -9,7 +9,7 @@ from saleor.account.models import User
from saleor.core import JobStatus
from saleor.csv import ExportEvents
from saleor.csv.models import ExportEvent, ExportFile
-from saleor.graphql.csv.enums import ExportScope, ProductFieldEnum
+from saleor.graphql.csv.enums import ExportScope, FileTypeEnum, ProductFieldEnum
from saleor.product.models import Attribute
from saleor.warehouse.models import Warehouse
from tests.api.utils import get_graphql_content
@@ -40,12 +40,20 @@ EXPORT_PRODUCTS_MUTATION = """
@pytest.mark.parametrize(
"input, called_data",
[
- ({"scope": ExportScope.ALL.name, "exportInfo": {}}, {"all": ""}),
+ (
+ {
+ "scope": ExportScope.ALL.name,
+ "exportInfo": {},
+ "fileType": FileTypeEnum.CSV.name,
+ },
+ {"all": ""},
+ ),
(
{
"scope": ExportScope.FILTER.name,
"filter": {"isPublished": True},
"exportInfo": {},
+ "fileType": FileTypeEnum.CSV.name,
},
{"filter": {"is_published": True}},
),
@@ -71,7 +79,9 @@ def test_export_products_mutation(
data = content["data"]["exportProducts"]
export_file_data = data["exportFile"]
- export_products_mock.assert_called_once_with(ANY, called_data, {})
+ export_products_mock.assert_called_once_with(
+ ANY, called_data, {}, FileTypeEnum.CSV.value
+ )
assert not data["csvErrors"]
assert data["exportFile"]["id"]
@@ -106,6 +116,7 @@ def test_export_products_mutation_ids_scope(
"warehouses": [],
"attributes": [],
},
+ "fileType": FileTypeEnum.XLSX.name,
}
}
@@ -121,6 +132,7 @@ def test_export_products_mutation_ids_scope(
assert set(call_args[1]["ids"]) == pks
assert call_args[2] == {"fields": [ProductFieldEnum.PRICE_OVERRIDE.value]}
+ assert call_args[3] == FileTypeEnum.XLSX.value
assert not data["csvErrors"]
assert data["exportFile"]["id"]
@@ -165,6 +177,7 @@ def test_export_products_mutation_with_warehouse_and_attribute_ids(
"warehouses": warehouse_ids,
"attributes": attribute_ids,
},
+ "fileType": FileTypeEnum.CSV.name,
}
}
@@ -184,6 +197,7 @@ def test_export_products_mutation_with_warehouse_and_attribute_ids(
"warehouses": warehouse_pks,
"attributes": attribute_pks,
}
+ assert call_args[3] == FileTypeEnum.CSV.value
assert not data["csvErrors"]
assert data["exportFile"]["id"]
@@ -197,8 +211,22 @@ def test_export_products_mutation_with_warehouse_and_attribute_ids(
@pytest.mark.parametrize(
"input, error_field",
[
- ({"scope": ExportScope.FILTER.name, "exportInfo": {}}, "filter"),
- ({"scope": ExportScope.IDS.name, "exportInfo": {}}, "ids"),
+ (
+ {
+ "scope": ExportScope.FILTER.name,
+ "exportInfo": {},
+ "fileType": FileTypeEnum.CSV.name,
+ },
+ "filter",
+ ),
+ (
+ {
+ "scope": ExportScope.IDS.name,
+ "exportInfo": {},
+ "fileType": FileTypeEnum.CSV.name,
+ },
+ "ids",
+ ),
],
)
@patch("saleor.graphql.csv.mutations.export_products.delay")
diff --git a/tests/test_csv_export.py b/tests/test_csv_export.py
index 333989fd6a..8b9950f112 100644
--- a/tests/test_csv_export.py
+++ b/tests/test_csv_export.py
@@ -79,14 +79,25 @@ def test_update_export_file_when_task_finished(export_file):
assert export_file.updated_at != previous_updated_at
[email protected]("scope", [{"filter": {"is_published": True}}, {"all": ""}])
+ "scope, filet_type",
+ [
+ ({"filter": {"is_published": True}}, FileTypes.CSV),
+ ({"all": ""}, FileTypes.XLSX),
+ ],
+)
@patch("saleor.csv.utils.export.send_email_with_link_to_download_csv")
@patch("saleor.csv.utils.export.save_csv_file_in_export_file")
def test_export_products(
- save_csv_file_in_export_file_mock, send_email_mock, product_list, export_file, scope
+ save_csv_file_in_export_file_mock,
+ send_email_mock,
+ product_list,
+ export_file,
+ scope,
+ filet_type,
):
export_info = {"fields": [], "warehouses": [], "attributes": []}
- export_products(export_file.id, scope, export_info)
+ export_products(export_file.id, scope, export_info, filet_type)
save_csv_file_in_export_file_mock.called_once_with(export_file, ANY)
send_email_mock.called_once_with(export_file)
@@ -99,23 +110,31 @@ def test_export_products_ids(
):
pks = [product.pk for product in product_list[:2]]
export_info = {"fields": [], "warehouses": [], "attributes": []}
+ file_type = FileTypes.CSV
assert export_file.status == JobStatus.PENDING
assert not export_file.content_file
- export_products(export_file.id, {"ids": pks}, export_info)
+ export_products(export_file.id, {"ids": pks}, export_info, file_type)
save_csv_file_in_export_file_mock.called_once_with(export_file, ANY)
send_email_mock.called_once_with(export_file)
-def test_get_filename():
+def test_get_filename_csv():
with freeze_time("2000-02-09"):
file_name = get_filename("test", FileTypes.CSV)
assert file_name == "test_data_09_02_2000.csv"
+def test_get_filename_xlsx():
+ with freeze_time("2000-02-09"):
+ file_name = get_filename("test", FileTypes.XLSX)
+
+ assert file_name == "test_data_09_02_2000.xlsx"
+
+
def test_get_product_queryset_all(product_list):
queryset = get_product_queryset({"all": ""})
@@ -139,7 +158,7 @@ def get_product_queryset_filter(product_list):
assert queryset.count() == len(product_list) - 1
-def test_create_csv_file_and_save_in_export_file(export_file, tmpdir):
+def test_create_csv_file_and_save_in_export_file_csv(export_file, tmpdir):
from django.conf import settings
settings.MEDIA_ROOT = tmpdir