Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Send email when export failed
IKarbowiakc2e7c8062fff
diff --git a/saleor/csv/__init__.py b/saleor/csv/__init__.pyindex d90318da67..c8db7f6fe7 100644--- a/saleor/csv/__init__.py+++ b/saleor/csv/__init__.py@@ -6,6 +6,7 @@ class ExportEvents:EXPORT_FAILED = "export_failed"EXPORT_DELETED = "export_deleted"EXPORTED_FILE_SENT = "exported_file_sent"+ EXPORT_FAILED_INFO_SENT = "Export_failed_info_sent"CHOICES = [(EXPORT_PENDING, "Data export was started."),@@ -14,7 +15,11 @@ class ExportEvents:(EXPORT_DELETED, "Export file was started."),(EXPORTED_FILE_SENT,- "Email with link to download csv file was sent to the customer.",+ "Email with link to download file was sent to the customer.",+ ),+ (+ EXPORT_FAILED_INFO_SENT,+ "Email with info that export failed was sent to the customer.",),]diff --git a/saleor/csv/emails.py b/saleor/csv/emails.pyindex beb54dc251..9fb1a169e7 100644--- a/saleor/csv/emails.py+++ b/saleor/csv/emails.py@@ -11,7 +11,10 @@ if TYPE_CHECKING:from .models import ExportFile-EXPORT_TEMPLATES = {"export_products": "csv/export_products_csv"}+EXPORT_TEMPLATES = {+ "export_products_success": "csv/export_products_csv",+ "export_failed": "csv/export_failed",+}@app.task@@ -26,3 +29,18 @@ def send_email_with_link_to_download_csv(export_file: "ExportFile", template_nam**send_kwargs,)events.export_file_sent_event(export_file=export_file, user=export_file.created_by)+++def send_export_failed_info(export_file: "ExportFile", template_name: str):+ recipient_email = export_file.created_by.email+ send_kwargs, ctx = get_email_context()+ send_templated_mail(+ template_name=EXPORT_TEMPLATES[template_name],+ recipient_list=[recipient_email],+ context=ctx,+ **send_kwargs,+ )+ events.export_failed_info_sent_event(+ export_file=export_file, user=export_file.created_by+ )diff --git a/saleor/csv/events.py b/saleor/csv/events.pyindex 88dd38c8ca..bbbed1d5b6 100644--- a/saleor/csv/events.py+++ b/saleor/csv/events.py@@ -41,3 +41,9 @@ def export_file_sent_event(*, export_file: "ExportFile", user: User):ExportEvent.objects.create(export_file=export_file, user=user, type=ExportEvents.EXPORTED_FILE_SENT)+++def export_failed_info_sent_event(*, export_file: "ExportFile", user: User):+ ExportEvent.objects.create(+ export_file=export_file, user=user, type=ExportEvents.EXPORT_FAILED_INFO_SENT+ )diff --git a/saleor/csv/utils/export.py b/saleor/csv/utils/export.pyindex 6d424f254e..74fc788811 100644--- a/saleor/csv/utils/export.py+++ b/saleor/csv/utils/export.py@@ -8,7 +8,7 @@ from ...celeryconf import appfrom ...core import JobStatusfrom ...product.models import Productfrom .. import FileTypes, events-from ..emails import send_email_with_link_to_download_csv+from ..emails import send_email_with_link_to_download_csv, send_export_failed_infofrom ..models import ExportFilefrom .products_data import get_products_data@@ -27,6 +27,7 @@ def on_task_failure(self, exc, task_id, args, kwargs, einfo):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):@@ -64,7 +65,7 @@ def export_products(file_name,file_type,)- send_email_with_link_to_download_csv(export_file, "export_products")+ send_email_with_link_to_download_csv(export_file, "export_products_success")def get_filename(model_name: str, file_type: str) -> str:diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex c303d6cff0..59bec3dbef 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1770,6 +1770,7 @@ enum ExportEventsEnum {EXPORT_FAILEDEXPORT_DELETEDEXPORTED_FILE_SENT+ EXPORT_FAILED_INFO_SENT}type ExportFile implements Node & Job {diff --git a/templates/templated_email/compiled/export_failed.html b/templates/templated_email/compiled/export_failed.htmlnew file mode 100644index 0000000000..b364d54c77--- /dev/null+++ b/templates/templated_email/compiled/export_failed.html@@ -0,0 +1,330 @@++ <!doctype html>+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">+ <head>+ <title>++ </title>+ <!--[if !mso]><!-- -->+ <meta http-equiv="X-UA-Compatible" content="IE=edge">+ <!--<![endif]-->+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1">+ <style type="text/css">+ #outlook a { padding:0; }+ body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }+ table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }+ img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; }+ p { display:block;margin:13px 0; }+ </style>+ <!--[if mso]>+ <xml>+ <o:OfficeDocumentSettings>+ <o:AllowPNG/>+ <o:PixelsPerInch>96</o:PixelsPerInch>+ </o:OfficeDocumentSettings>+ </xml>+ <![endif]-->+ <!--[if lte mso 11]>+ <style type="text/css">+ .mj-outlook-group-fix { width:100% !important; }+ </style>+ <![endif]-->++ <!--[if !mso]><!-->+ <link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css">+ <style type="text/css">+ @import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);+ </style>+ <!--<![endif]-->++++ <style type="text/css">+ @media only screen and (min-width:480px) {+ .mj-column-per-100 { width:100% !important; max-width: 100%; }+ }+ </style>+++ <style type="text/css">+++ </style>+ <style type="text/css">address {+ font-style: inherit;+ font-family: Ubuntu, Helvetica, Arial;+ }</style>++ </head>+ <body>+++ <div style>+ {% load i18n %}{% load static %}++ <!--[if mso | IE]>+ <table+ align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"+ >+ <tr>+ <td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">+ <![endif]-->+++ <div style="margin:0px auto;max-width:600px;">++ <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">+ <tbody>+ <tr>+ <td style="direction: ltr; font-size: 0px; padding: 20px 0; text-align: center;" align="center">+ <!--[if mso | IE]>+ <table role="presentation" border="0" cellpadding="0" cellspacing="0">++ <tr>++ <td+ class="" style="vertical-align:top;width:600px;"+ >+ <![endif]-->++ <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">++ <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">++ <tr>+ <td align="left" style="font-size: 0px; padding: 10px 25px; word-break: break-word;">++ <div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#000000;">{% trans "Hi!" context "Standard e-mail greeting" %}</div>++ </td>+ </tr>++ <tr>+ <td align="left" style="font-size: 0px; padding: 10px 25px; word-break: break-word;">++ <div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:left;color:#000000;">{% blocktrans context "Export products e-mail text" %}+ Sorry, we cannot finish product export, some unexpected errors occurred.+ Please try again.+ {% endblocktrans %}</div>++ </td>+ </tr>++ </table>++ </div>++ <!--[if mso | IE]>+ </td>++ </tr>++ </table>+ <![endif]-->+ </td>+ </tr>+ </tbody>+ </table>++ </div>+++ <!--[if mso | IE]>+ </td>+ </tr>+ </table>++ <table+ align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"+ >+ <tr>+ <td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">+ <![endif]-->+++ <div style="margin:0px auto;max-width:600px;">++ <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">+ <tbody>+ <tr>+ <td style="direction: ltr; font-size: 0px; padding: 20px 0; text-align: center;" align="center">+ <!--[if mso | IE]>+ <table role="presentation" border="0" cellpadding="0" cellspacing="0">++ <tr>++ <td+ class="" style="vertical-align:top;width:600px;"+ >+ <![endif]-->++ <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">++ <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">++ <tr>+ <td align="left" style="font-size: 0px; padding: 10px 25px; word-break: break-word;">++ <div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:left;color:#000000;">{% blocktrans trimmed context "Base email text" %}+ This is an automatically generated e-mail, please do not reply.+ {% endblocktrans %}</div>++ </td>+ </tr>++ </table>++ </div>++ <!--[if mso | IE]>+ </td>++ </tr>++ </table>+ <![endif]-->+ </td>+ </tr>+ </tbody>+ </table>++ </div>+++ <!--[if mso | IE]>+ </td>+ </tr>+ </table>++ <table+ align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"+ >+ <tr>+ <td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">+ <![endif]-->+++ <div style="background:#F2F2F2;background-color:#F2F2F2;margin:0px auto;max-width:600px;">++ <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#F2F2F2;background-color:#F2F2F2;width:100%;">+ <tbody>+ <tr>+ <td style="direction: ltr; font-size: 0px; padding: 20px 0; text-align: center;" align="center">+ <!--[if mso | IE]>+ <table role="presentation" border="0" cellpadding="0" cellspacing="0">++ <tr>++ <td+ class="" style="vertical-align:top;width:600px;"+ >+ <![endif]-->++ <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">++ <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">++ <tr>+ <td align="center" style="font-size: 0px; padding: 10px 25px; word-break: break-word;">++ <div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:center;color:#000000;">{% blocktrans trimmed context "Base email footer" %}+ Sincerely, {{ site_name }}+ {% endblocktrans %}</div>++ </td>+ </tr>++ </table>++ </div>++ <!--[if mso | IE]>+ </td>++ </tr>++ </table>+ <![endif]-->+ </td>+ </tr>+ </tbody>+ </table>++ </div>+++ <!--[if mso | IE]>+ </td>+ </tr>+ </table>++ <table+ align="center" border="0" cellpadding="0" cellspacing="0" class="no-display-outlook" style="width:600px;" width="600"+ >+ <tr>+ <td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">+ <![endif]-->+++ <div class="no-display" style="display: none; margin: 0px auto; max-width: 600px;">++ <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">+ <tbody>+ <tr>+ <td style="direction: ltr; font-size: 0px; padding: 20px 0; text-align: center;" align="center">+ <!--[if mso | IE]>+ <table role="presentation" border="0" cellpadding="0" cellspacing="0">++ <tr>++ <td+ class="" style="vertical-align:top;width:600px;"+ >+ <![endif]-->++ <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">++ <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">++ <tr>+ <td align="left" style="font-size: 0px; padding: 10px 25px; word-break: break-word;">++ <div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:left;color:#000000;">{% if schema_markup %}+ <script type="application/ld+json">{{ schema_markup|safe }}</script>+ {% endif %}</div>++ </td>+ </tr>++ </table>++ </div>++ <!--[if mso | IE]>+ </td>++ </tr>++ </table>+ <![endif]-->+ </td>+ </tr>+ </tbody>+ </table>++ </div>+++ <!--[if mso | IE]>+ </td>+ </tr>+ </table>+ <![endif]-->+++ </div>++ </body>+ </html>+\ No newline at end of filediff --git a/templates/templated_email/csv/export_failed.email b/templates/templated_email/csv/export_failed.emailnew file mode 100644index 0000000000..fa4d3aaabb--- /dev/null+++ b/templates/templated_email/csv/export_failed.email@@ -0,0 +1,20 @@+{% load i18n %}++{% block subject %}+ {% trans "Export products data" context "Export products e-mail subject" %}+{% endblock %}++{% block plain %}+{% include 'templated_email/shared/_header.email' %}++{% blocktrans context "Export products e-mail text" %}+Sorry, we cannot finish product export, some unexpected errors occurred.+Please try again.+{% endblocktrans %}++{% include 'templated_email/shared/_footer.email' %}+{% endblock %}++{% block html %}+{% include 'templated_email/compiled/export_failed.html' %}+{% endblock %}diff --git a/templates/templated_email/source/export_failed.mjml b/templates/templated_email/source/export_failed.mjmlnew file mode 100644index 0000000000..3395b0865c--- /dev/null+++ b/templates/templated_email/source/export_failed.mjml@@ -0,0 +1,25 @@+<mjml>+ <mj-head>+ <mj-include path="./shared/styles.mjml" />+ </mj-head>+ <mj-body>+ <mj-raw>+ {% load i18n %}+ </mj-raw>+ <mj-include path="./shared/header.mjml" />+ <mj-section>+ <mj-column>+ <mj-text font-size="16px">+ {% trans "Hi!" context "Standard e-mail greeting" %}+ </mj-text>+ <mj-text>+ {% blocktrans context "Export products e-mail text" %}+ Sorry, we couldn't finish exporting products, some unexpected errors occurred.+ Please try again.+ {% endblocktrans %}+ </mj-text>+ </mj-column>+ </mj-section>+ <mj-include path="./shared/footer.mjml" />+ </mj-body>+</mjml>diff --git a/tests/test_csv_export.py b/tests/test_csv_export.pyindex d0f01380fc..2d122a8e89 100644--- a/tests/test_csv_export.py+++ b/tests/test_csv_export.py@@ -23,7 +23,8 @@ from saleor.csv.utils.export import ()-def test_on_task_failure(export_file):+@patch("saleor.csv.utils.export.send_export_failed_info")+def test_on_task_failure(send_export_failed_info_mock, export_file):exc = Exception("Test")task_id = "task_id"args = [export_file.pk, {"all": ""}]@@ -41,11 +42,17 @@ def test_on_task_failure(export_file):assert export_file.status == JobStatus.FAILEDassert export_file.created_atassert export_file.updated_at != previous_updated_at- export_event = ExportEvent.objects.get(- export_file=export_file, user=export_file.created_by+ export_failed_event = ExportEvent.objects.get(+ export_file=export_file,+ user=export_file.created_by,+ type=ExportEvents.EXPORT_FAILED,)- assert export_event.type == ExportEvents.EXPORT_FAILED- assert export_event.parameters == {"message": str(exc), "error_type": info_type}+ assert export_failed_event.parameters == {+ "message": str(exc),+ "error_type": info_type,+ }++ send_export_failed_info_mock.called_once_with(export_file, "export_failed")def test_on_task_success(export_file):diff --git a/tests/test_emails.py b/tests/test_emails.pyindex 1c9ca5cefc..960007509b 100644--- a/tests/test_emails.py+++ b/tests/test_emails.py@@ -413,8 +413,10 @@ def test_send_email_with_link_to_download_csv(export_file.content_file = file_mockexport_file.save()- csv_emails.send_email_with_link_to_download_csv(export_file, "export_products")- template = csv_emails.EXPORT_TEMPLATES["export_products"]+ csv_emails.send_email_with_link_to_download_csv(+ export_file, "export_products_success"+ )+ template = csv_emails.EXPORT_TEMPLATES["export_products_success"]ctx = {"csv_link": f"http://mirumee.com/media/export_files/{file_mock.name}","domain": "mirumee.com",@@ -440,3 +442,45 @@ def test_send_email_with_link_to_download_csv(user=export_file.created_by,type=ExportEvents.EXPORTED_FILE_SENT,)++[email protected]("saleor.csv.emails.send_templated_mail")+def test_send_export_failed_info(+ mocked_templated_email, site_settings, export_file, tmpdir+):+ from django.conf import settings++ settings.MEDIA_ROOT = tmpdir++ file_mock = mock.MagicMock(spec=File)+ file_mock.name = "temp_file.csv"++ export_file.content_file = file_mock+ export_file.save()++ csv_emails.send_export_failed_info(export_file, "export_failed")+ template = csv_emails.EXPORT_TEMPLATES["export_failed"]+ ctx = {+ "domain": "mirumee.com",+ "site_name": "mirumee.com",+ }+ recipients = [export_file.created_by.email]+ expected_call_kwargs = {+ "context": ctx,+ "from_email": site_settings.default_from_email,+ "template_name": template,+ }++ mocked_templated_email.assert_called_once_with(+ recipient_list=recipients, **expected_call_kwargs+ )++ # Render the email to ensure there is no error+ email_connection = get_connection()+ email_connection.get_email_message(to=recipients, **expected_call_kwargs)++ assert ExportEvent.objects.filter(+ export_file=export_file,+ user=export_file.created_by,+ type=ExportEvents.EXPORT_FAILED_INFO_SENT,+ )