Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move export products to tasks file
IKarbowiakfc8acbdea88b
diff --git a/saleor/csv/tasks.py b/saleor/csv/tasks.pynew file mode 100644index 0000000000..5c4b29f95a--- /dev/null+++ b/saleor/csv/tasks.py@@ -0,0 +1,47 @@+from typing import Dict, Union++from ..celeryconf import app+from ..core import JobStatus+from . import events+from .emails import send_export_failed_info+from .models import ExportFile+from .utils.export import export_products+++def on_task_failure(self, exc, task_id, args, kwargs, einfo):+ export_file_id = args[0]+ export_file = ExportFile.objects.get(pk=export_file_id)++ export_file.content_file = None+ export_file.status = JobStatus.FAILED+ export_file.save(update_fields=["status", "updated_at", "content_file"])++ events.export_failed_event(+ export_file=export_file,+ user=export_file.created_by,+ message=str(exc),+ error_type=str(einfo.type),+ )+ send_export_failed_info(export_file, "export_failed")+++def on_task_success(self, retval, task_id, args, kwargs):+ export_file_id = args[0]++ export_file = ExportFile.objects.get(pk=export_file_id)+ export_file.status = JobStatus.SUCCESS+ export_file.save(update_fields=["status", "updated_at"])++ events.export_success_event(export_file=export_file, user=export_file.created_by)++[email protected](on_success=on_task_success, on_failure=on_task_failure)+def export_products_task(+ export_file_id: int,+ scope: Dict[str, Union[str, dict]],+ export_info: Dict[str, list],+ file_type: str,+ delimiter: str = ";",+):+ export_file = ExportFile.objects.get(pk=export_file_id)+ export_products(export_file, scope, export_info, file_type, delimiter)diff --git a/saleor/csv/utils/export.py b/saleor/csv/utils/export.pyindex a39b1c006e..6b3714346e 100644--- a/saleor/csv/utils/export.py+++ b/saleor/csv/utils/export.py@@ -4,7 +4,6 @@ from typing import IO, TYPE_CHECKING, Dict, List, Set, Unionimport petl as etlfrom django.utils import timezone-from ...celeryconf import appfrom ...core import JobStatusfrom ...product.models import Productfrom .. import FileTypes, events@@ -20,36 +19,8 @@ if TYPE_CHECKING:BATCH_SIZE = 10000-def on_task_failure(self, exc, task_id, args, kwargs, einfo):- export_file_id = args[0]- export_file = ExportFile.objects.get(pk=export_file_id)-- export_file.content_file = None- export_file.status = JobStatus.FAILED- export_file.save(update_fields=["status", "updated_at", "content_file"])-- events.export_failed_event(- export_file=export_file,- user=export_file.created_by,- message=str(exc),- error_type=str(einfo.type),- )- send_export_failed_info(export_file, "export_failed")---def on_task_success(self, retval, task_id, args, kwargs):- export_file_id = args[0]-- export_file = ExportFile.objects.get(pk=export_file_id)- export_file.status = JobStatus.SUCCESS- export_file.save(update_fields=["status", "updated_at"])-- events.export_success_event(export_file=export_file, user=export_file.created_by)--[email protected](on_success=on_task_success, on_failure=on_task_failure)def export_products(- export_file_id: int,+ export_file: ExportFile,scope: Dict[str, Union[str, dict]],export_info: Dict[str, list],file_type: str,@@ -61,7 +32,6 @@ def export_products(export_fields, file_headers, data_headers = get_export_fields_and_headers_info(export_info)- export_file = ExportFile.objects.get(pk=export_file_id)create_file_with_headers(file_headers, delimiter, export_file, file_name, file_type)diff --git a/saleor/graphql/csv/mutations.py b/saleor/graphql/csv/mutations.pyindex e62402de7a..b1ff4bbd82 100644--- a/saleor/graphql/csv/mutations.py+++ b/saleor/graphql/csv/mutations.py@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationErrorfrom ...core.permissions import ProductPermissionsfrom ...csv import models as csv_modelsfrom ...csv.events import export_started_event-from ...csv.utils.export import export_products+from ...csv.tasks import export_products_taskfrom ..core.enums import CsvErrorCodefrom ..core.mutations import BaseMutationfrom ..core.types.common import CsvError@@ -81,7 +81,7 @@ class ExportProducts(BaseMutation):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, file_type)+ export_products_task.delay(export_file.pk, scope, export_info, file_type)export_file.refresh_from_db()return cls(export_file=export_file)diff --git a/tests/api/test_csv.py b/tests/api/test_csv.pyindex 862276478c..5dab703c12 100644--- a/tests/api/test_csv.py+++ b/tests/api/test_csv.py@@ -59,7 +59,7 @@ EXPORT_PRODUCTS_MUTATION = """),],)-@patch("saleor.graphql.csv.mutations.export_products.delay")+@patch("saleor.graphql.csv.mutations.export_products_task.delay")def test_export_products_mutation(export_products_mock,staff_api_client,@@ -92,7 +92,7 @@ def test_export_products_mutation().exists()-@patch("saleor.graphql.csv.mutations.export_products.delay")+@patch("saleor.graphql.csv.mutations.export_products_task.delay")def test_export_products_mutation_ids_scope(export_products_mock, staff_api_client, product_list, permission_manage_products):@@ -143,7 +143,7 @@ def test_export_products_mutation_ids_scope().exists()-@patch("saleor.graphql.csv.mutations.export_products.delay")+@patch("saleor.graphql.csv.mutations.export_products_task.delay")def test_export_products_mutation_with_warehouse_and_attribute_ids(export_products_mock, staff_api_client, product_list, permission_manage_products):@@ -229,7 +229,7 @@ def test_export_products_mutation_with_warehouse_and_attribute_ids(),],)-@patch("saleor.graphql.csv.mutations.export_products.delay")+@patch("saleor.graphql.csv.mutations.export_products_task.delay")def test_export_products_mutation_failed(export_products_mock,staff_api_client,diff --git a/tests/test_csv_export.py b/tests/test_csv_export.pyindex a886d5f6d4..8cddd54014 100644--- a/tests/test_csv_export.py+++ b/tests/test_csv_export.py@@ -13,6 +13,7 @@ from freezegun import freeze_timefrom saleor.core import JobStatusfrom saleor.csv import ExportEvents, FileTypesfrom saleor.csv.models import ExportEvent, ExportFile+from saleor.csv.tasks import export_products_task, on_task_failure, on_task_successfrom saleor.csv.utils.export import (append_to_file,create_file_with_headers,@@ -20,14 +21,27 @@ from saleor.csv.utils.export import (export_products_in_batches,get_filename,get_product_queryset,- on_task_failure,- on_task_success,save_csv_file_in_export_file,)from saleor.graphql.csv.enums import ProductFieldEnumfrom saleor.product.models import Product+@patch("saleor.csv.tasks.export_products")+def test_export_products_task(export_products_mock, export_file):+ # given+ scope = {"all": ""}+ export_info = {"fields": "name"}+ file_type = FileTypes.CSV+ delimiter = ";"++ # when+ export_products_task(export_file.id, scope, export_info, file_type, delimiter)++ # then+ export_products_mock.called_once_with()++@patch("saleor.csv.utils.export.send_export_failed_info")def test_on_task_failure(send_export_failed_info_mock, export_file):exc = Exception("Test")@@ -111,7 +125,7 @@ def test_export_products("warehouses": [],"attributes": [],}- export_products(export_file.id, scope, export_info, file_type)+ export_products(export_file, scope, export_info, file_type)export_products_in_batches_mock.called_once_with(ANY,@@ -141,7 +155,7 @@ def test_export_products_ids(assert export_file.status == JobStatus.PENDINGassert not export_file.content_file- export_products(export_file.id, {"ids": pks}, export_info, file_type)+ export_products(export_file, {"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)