Skip to content

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

Fix sending order confirmation e-mails asynchronously

Anna Siwik8e4d9577a0dd
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index 5d9a2cd24e..5cda2a9961 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -8,22 +8,23 @@ CONFIRM_PAYMENT_TEMPLATE = 'order/payment/confirm_payment'
CONFIRM_NOTE_TEMPLATE = 'order/note/confirm_note'
-def _send_confirmation(email, url, template, ctx=None):
+def _send_confirmation(email, url, template, context=None):
site = Site.objects.get_current()
- context = {'site_name': site.name, 'url': url}
- if ctx:
- context.update(ctx)
+ ctx = {'site_name': site.name, 'url': url}
+ if context:
+ ctx.update(context)
send_templated_mail(
from_email=settings.ORDER_FROM_EMAIL,
recipient_list=[email],
- context=context,
+ context=ctx,
template_name=template)
@shared_task
-def send_order_confirmation(email, url, order):
- ctx = {'order': order}
- _send_confirmation(email, url, CONFIRM_ORDER_TEMPLATE, ctx)
+def send_order_confirmation(email, url, order_pk):
+ from .models import Order
+ order = Order.objects.get(pk=order_pk)
+ _send_confirmation(email, url, CONFIRM_ORDER_TEMPLATE, {'order': order})
@shared_task
diff --git a/saleor/order/models.py b/saleor/order/models.py
index baec54976c..61f6b11832 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -131,7 +131,7 @@ class Order(models.Model, ItemSet):
email = self.get_user_current_email()
payment_url = build_absolute_uri(
reverse('order:details', kwargs={'token': self.token}))
- emails.send_order_confirmation.delay(email, payment_url, self)
+ emails.send_order_confirmation.delay(email, payment_url, self.pk)
def get_last_payment_status(self):
last_payment = self.payments.last()
diff --git a/tests/conftest.py b/tests/conftest.py
index 6e9750f17f..2b90d2a8fe 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -420,6 +420,7 @@ def order_with_lines(order, product_type, default_category):
unit_price_gross=Decimal('30.00'),
)
+ recalculate_order(order)
return order
@@ -494,6 +495,7 @@ def order_with_variant_from_different_stocks(order_with_lines_and_stock):
Stock.objects.create(
variant=variant, cost_price=1, quantity=5, quantity_allocated=0,
location=warehouse_2)
+ recalculate_order(order_with_lines_and_stock)
return order_with_lines_and_stock
diff --git a/tests/test_asynchronous_tasks.py b/tests/test_asynchronous_tasks.py
index 3de3914564..47a943fcfc 100644
--- a/tests/test_asynchronous_tasks.py
+++ b/tests/test_asynchronous_tasks.py
@@ -7,8 +7,9 @@ from saleor.order.emails import (
@pytest.mark.django_db
@pytest.mark.integration
def test_email_sending_asynchronously(
- transactional_db, celery_app, celery_worker):
- order = send_order_confirmation.delay('[email protected]', '/nowhere/to/go')
+ transactional_db, celery_app, celery_worker, order_with_lines):
+ order = send_order_confirmation.delay(
+ '[email protected]', '/nowhere/to/go', order_with_lines.pk)
payment = send_payment_confirmation.delay('[email protected]', '/nowhere/')
order.get()
payment.get()
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 1e707095f1..93a9cea4c4 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -11,9 +11,10 @@ EMAIL_FROM = settings.ORDER_FROM_EMAIL
@mock.patch('saleor.order.emails.send_templated_mail')
-def test_send_confirmation_using_templated_email(mocked_templated_email):
- emails.send_order_confirmation(EMAIL, URL)
- context = {'site_name': SITE_NAME, 'url': URL}
+def test_send_confirmation_using_templated_email(
+ mocked_templated_email, order):
+ emails.send_order_confirmation(EMAIL, URL, order.pk)
+ context = {'site_name': SITE_NAME, 'url': URL, 'order': order}
mocked_templated_email.assert_called_once_with(
recipient_list=[EMAIL],
context=context,