Skip to content

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

Change csv export temporary file open mode

Tomasz Szymanskiab199f7f3a7f
diff --git a/saleor/csv/tests/export/test_export.py b/saleor/csv/tests/export/test_export.py
index 2495d5520a..902dafc445 100644
--- a/saleor/csv/tests/export/test_export.py
+++ b/saleor/csv/tests/export/test_export.py
@@ -66,13 +66,13 @@ def test_export_products(
)
-@patch("saleor.csv.utils.export.create_file_with_headers")
+@patch("saleor.csv.utils.export.save_csv_file_in_export_file")
@patch("saleor.csv.utils.export.export_products_in_batches")
@patch("saleor.csv.utils.export.send_email_with_link_to_download_file")
def test_export_products_ids(
send_email_mock,
export_products_in_batches_mock,
- create_file_with_headers_mock,
+ save_csv_file_mock,
product_list,
user_export_file,
):
@@ -88,19 +88,19 @@ def test_export_products_ids(
export_products(user_export_file, {"ids": pks}, export_info, file_type)
# then
- create_file_with_headers_mock.called_once_with(
- ["id"], ";", user_export_file, ANY, file_type
- )
- export_products_in_batches_mock.called_once_with(
+ assert save_csv_file_mock.call_args[0][0] == user_export_file
+ assert save_csv_file_mock.call_args[0][2].startswith("product_data_")
+ import ipdb; ipdb.set_trace()
+ export_products_in_batches_mock.assert_called_once_with(
Product.objects.filter(pk__in=pks),
export_info,
{"id"},
["id"],
";",
- user_export_file,
+ ANY,
file_type,
)
- send_email_mock.called_once_with(
+ send_email_mock.assert_called_once_with(
user_export_file, user_export_file.user.email, "export_products_success"
)
diff --git a/saleor/csv/utils/export.py b/saleor/csv/utils/export.py
index ee08d6f9bd..dcca058cfc 100644
--- a/saleor/csv/utils/export.py
+++ b/saleor/csv/utils/export.py
@@ -32,7 +32,7 @@ def export_products(
export_info
)
- create_file_with_headers(file_headers, delimiter, export_file, file_name, file_type)
+ temporary_file = create_file_with_headers(file_headers, delimiter, file_type)
export_products_in_batches(
queryset,
@@ -40,10 +40,12 @@ def export_products(
set(export_fields),
data_headers,
delimiter,
- export_file,
+ temporary_file,
file_type,
)
+ save_csv_file_in_export_file(export_file, temporary_file, file_name)
+
if export_file.user:
send_email_with_link_to_download_file(
export_file, export_file.user.email, "export_products_success"
@@ -97,7 +99,7 @@ def export_products_in_batches(
export_fields: Set[str],
headers: List[str],
delimiter: str,
- export_file: "ExportFile",
+ temporary_file: "NamedTemporaryFile",
file_type: str,
):
warehouses = export_info.get("warehouses")
@@ -117,40 +119,38 @@ def export_products_in_batches(
product_batch, export_fields, attributes, warehouses
)
- append_to_file(export_data, headers, export_file, file_type, delimiter)
+ append_to_file(export_data, headers, temporary_file, file_type, delimiter)
def create_file_with_headers(
file_headers: List[str],
delimiter: str,
- export_file: "ExportFile",
- file_name: str,
file_type: str,
):
table = etl.wrap([file_headers])
- with NamedTemporaryFile() as temporary_file:
- if file_type == FileTypes.CSV:
- etl.tocsv(table, temporary_file.name, delimiter=delimiter)
- else:
- etl.io.xlsx.toxlsx(table, temporary_file.name)
+ temp_file = NamedTemporaryFile("ab+")
+ if file_type == FileTypes.CSV:
+ etl.tocsv(table, temp_file.name, delimiter=delimiter)
+ else:
+ etl.io.xlsx.toxlsx(table, temp_file.name)
- save_csv_file_in_export_file(export_file, temporary_file, file_name)
+ return temp_file
def append_to_file(
export_data: List[Dict[str, Union[str, bool]]],
headers: List[str],
- export_file: "ExportFile",
+ temporary_file: "NamedTemporaryFile",
file_type: str,
delimiter: str,
):
table = etl.fromdicts(export_data, header=headers, missing=" ")
if file_type == FileTypes.CSV:
- etl.io.csv.appendcsv(table, export_file.content_file, delimiter=delimiter)
+ etl.io.csv.appendcsv(table, temporary_file.name, delimiter=delimiter)
else:
- etl.io.xlsx.appendxlsx(table, export_file.content_file.path)
+ etl.io.xlsx.appendxlsx(table, temporary_file.name)
def save_csv_file_in_export_file(