Skip to content

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

Email sending, mutation and tests improvements

Tomasz Szymanskia247cef07f65
diff --git a/.isort.cfg b/.isort.cfg
index c1e4bed09a..6374636deb 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,opentracing,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
+known_third_party = PIL,babel,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,opentracing,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 5b47b3eb83..a25baaa34b 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -686,26 +686,28 @@ class SendInvoiceEmail(ModelMutation):
error_type_field = "invoice_errors"
@classmethod
- def clean_input(cls, info, instance, data):
+ def clean_instance(cls, info, instance):
validation_errors = {}
if instance.status != InvoiceStatus.READY:
- validation_errors['invoice'] = ValidationError(
+ validation_errors["invoice"] = ValidationError(
"Invoice needs to be ready in order to send it.",
code=InvoiceErrorCode.INVALID,
)
- for field in ('url', 'number'):
+ for field in ("url", "number"):
if not getattr(instance, field):
validation_errors[field] = ValidationError(
- f"Provided invoice needs to have {field} assigned in order to be sent.",
+ (
+ f"Provided invoice needs to have {field} "
+ "assigned in order to be sent."
+ ),
code=InvoiceErrorCode.REQUIRED,
)
if validation_errors:
raise ValidationError(validation_errors)
- return data
@classmethod
def perform_mutation(cls, _root, info, **data):
instance = cls.get_instance(info, **data)
- cleaned_input = cls.clean_input(info, instance, data)
+ cls.clean_instance(info, instance)
send_invoice.delay(instance.pk)
return SendInvoiceEmail(invoice=instance)
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index c16a43c4ed..4cc8c704e3 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -7,7 +7,7 @@ from ..celeryconf import app
from ..core.emails import get_email_context, prepare_url
from ..seo.schema.email import get_order_confirmation_markup
from . import events
-from .models import Fulfillment, Order
+from .models import Fulfillment, Invoice, Order
CONFIRM_ORDER_TEMPLATE = "order/confirm_order"
STAFF_CONFIRM_ORDER_TEMPLATE = "order/staff_confirm_order"
@@ -101,10 +101,28 @@ def send_staff_order_confirmation(order_pk, redirect_url):
send_templated_mail(**staff_email_data)
+def collect_invoice_data_for_email(invoice_pk, template):
+ """Collect the required data for sending emails."""
+ invoice = Invoice.objects.get(pk=invoice_pk)
+ recipient_email = invoice.order.user.email
+ send_kwargs, email_context = get_email_context()
+
+ email_context["number"] = invoice.number
+ email_context["download_url"] = invoice.url
+
+ return {
+ "recipient_list": [recipient_email],
+ "template_name": template,
+ "context": email_context,
+ **send_kwargs,
+ }
+
+
@app.task
def send_invoice(invoice_pk):
- """Sends an invoice to user of related order with URL to download it."""
- pass
+ """Send an invoice to user of related order with URL to download it."""
+ email_data = collect_invoice_data_for_email(invoice_pk, "order/send_invoice")
+ send_templated_mail(**email_data)
@app.task
diff --git a/templates/templated_email/compiled/send_invoice.html b/templates/templated_email/compiled/send_invoice.html
new file mode 100644
index 0000000000..28c6db7449
--- /dev/null
+++ b/templates/templated_email/compiled/send_invoice.html
@@ -0,0 +1,415 @@
+
+ <!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">
+ .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">
+
+
+
+ @media only screen and (max-width:480px) {
+ table.full-width-mobile { width: 100% !important; }
+ td.full-width-mobile { width: auto !important; }
+ }
+
+ </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 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;">
+
+ <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;">
+ <tbody>
+ <tr>
+ <td style="padding: 5px; width: 176px;" width="176">
+
+ <img height="38" src="{{ logo_url }}" style="border:0;display:block;outline:none;text-decoration:none;height:38px;width:100%;font-size:13px;" width="176">
+
+ </td>
+ </tr>
+ </tbody>
+ </table>
+
+ </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 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 trimmed context "Send invoice e-mail text" %}
+ In order to download invoice {{ number }}, click the link below.
+ {% endblocktrans %}</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;"><a href="{{ download_url | safe }}">
+ {{ download_url | safe }}
+ </a></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 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 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 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 file
diff --git a/templates/templated_email/order/send_invoice.email b/templates/templated_email/order/send_invoice.email
new file mode 100644
index 0000000000..72b5058a82
--- /dev/null
+++ b/templates/templated_email/order/send_invoice.email
@@ -0,0 +1,20 @@
+{% load i18n %}
+
+{% block subject %}
+ {% trans "Invoice" context "Invoice email subject" %} {{ number }} {{ site_name }}
+{% endblock %}
+
+{% block plain %}
+{% include 'templated_email/shared/_header.email' %}
+{% blocktrans context "Send invoice e-mail text" %}
+In order to download invoice {{ number }}, click the link below.
+{% endblocktrans %}
+
+{{ download_url | safe }}
+
+{% include 'templated_email/shared/_footer.email' %}
+{% endblock %}
+
+{% block html %}
+{% include 'templated_email/compiled/send_invoice.html' %}
+{% endblock %}
diff --git a/templates/templated_email/source/send_invoice.mjml b/templates/templated_email/source/send_invoice.mjml
new file mode 100644
index 0000000000..cb8ece817d
--- /dev/null
+++ b/templates/templated_email/source/send_invoice.mjml
@@ -0,0 +1,29 @@
+<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 trimmed context "Send invoice e-mail text" %}
+ In order to download invoice {{ number }}, click the link below.
+ {% endblocktrans %}
+ </mj-text>
+ <mj-text>
+ <a href="{{ download_url | safe }}">
+ {{ download_url | safe }}
+ </a>
+ </mj-text>
+ </mj-column>
+ </mj-section>
+ <mj-include path="./shared/footer.mjml" />
+ </mj-body>
+</mjml>
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 2cd1c980fd..f040c628a7 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -2902,6 +2902,20 @@ UPDATE_INVOICE_MUTATION = """
"""
+SEND_INVOICE_MUTATION = """
+ mutation SendInvoice($id: ID!) {
+ sendInvoiceEmail(
+ id: $id
+ ) {
+ invoiceErrors {
+ field
+ code
+ }
+ }
+ }
+"""
+
+
@mock.patch("saleor.extensions.base_plugin.BasePlugin.invoice_request")
def test_request_invoice(
plugin_mock, staff_api_client, permission_manage_orders, orders
@@ -3135,3 +3149,39 @@ def test_create_invoice_empty_params(
assert not Invoice.objects.filter(
order_id=order.pk, status=InvoiceStatus.READY
).exists()
+
+
[email protected]("saleor.order.emails.send_invoice.delay")
+def test_send_invoice(email_mock, staff_api_client, permission_manage_orders, orders):
+ number = "01/12/2020/TEST"
+ url = "http://www.example.com"
+ order = orders[0]
+ invoice = Invoice.objects.create(
+ order=order, number=number, url=url, status=InvoiceStatus.READY
+ )
+ variables = {"id": graphene.Node.to_global_id("Invoice", invoice.pk)}
+ response = staff_api_client.post_graphql(
+ SEND_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ assert not content["data"]["sendInvoiceEmail"]["invoiceErrors"]
+ email_mock.assert_called_with(invoice.pk)
+
+
[email protected]("saleor.order.emails.send_invoice.delay")
+def test_send_pending_invoice(
+ email_mock, staff_api_client, permission_manage_orders, orders
+):
+ invoice = Invoice.objects.create(
+ order=orders[0], number=None, url=None, status=InvoiceStatus.PENDING
+ )
+ variables = {"id": graphene.Node.to_global_id("Invoice", invoice.pk)}
+ response = staff_api_client.post_graphql(
+ SEND_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]
+ assert errors[0] == {"field": "invoice", "code": InvoiceErrorCode.INVALID.name}
+ assert errors[1] == {"field": "url", "code": InvoiceErrorCode.REQUIRED.name}
+ assert errors[2] == {"field": "number", "code": InvoiceErrorCode.REQUIRED.name}
+ email_mock.assert_not_called()
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 0ed80b1683..761517ae61 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -11,6 +11,7 @@ import saleor.account.emails as account_emails
import saleor.order.emails as emails
from saleor.core.emails import get_email_context, prepare_url
from saleor.core.utils import build_absolute_uri
+from saleor.order.models import Invoice, InvoiceStatus
from saleor.order.utils import add_variant_to_order
@@ -41,6 +42,19 @@ def test_collect_data_for_order_confirmation_email(order):
assert "schema_markup" in email_context
+def test_collect_invoice_data_for_email(order):
+ number = "01/12/2020/TEST"
+ url = "http://www.example.com"
+ invoice = Invoice.objects.create(
+ number=number, url=url, status=InvoiceStatus.READY, order=order
+ )
+ email_data = emails.collect_invoice_data_for_email(invoice.pk, "order/send_invoice")
+ email_context = email_data["context"]
+ assert email_context["number"] == number
+ assert email_context["download_url"] == url
+ assert email_data["recipient_list"] == [order.user.email]
+
+
def test_collect_data_for_fullfillment_email(fulfilled_order):
template = emails.CONFIRM_FULFILLMENT_TEMPLATE
fulfillment = fulfilled_order.fulfillments.first()