Skip to content

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

Refactor email functions

akjanikc270578900ff
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index a236976d85..1ecf445b2f 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -6,7 +6,7 @@ from django.utils.translation import pgettext, pgettext_lazy
from ...account.forms import get_address_form
from ...account.models import Address
from ...core.exceptions import InsufficientStock
-from ...order.emails import collect_data_for_email, send_order_confirmation
+from ...order.emails import send_order_confirmation
from ...order.utils import create_history_entry
from ..forms import (
AnonymousUserBillingForm, BillingAddressesForm,
@@ -29,9 +29,7 @@ def create_order(checkout):
create_history_entry(
order=order, user=user, content=pgettext_lazy(
'Order status history entry', 'Order was placed'))
- email_data = collect_data_for_email(order)
- email_data.update({'order_pk': order.pk})
- send_order_confirmation.delay(**email_data)
+ send_order_confirmation(order)
return order, redirect('order:payment', token=order.token)
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 67d4ce177b..6d2eddbe54 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -12,7 +12,7 @@ from ...cart.forms import QuantityField
from ...core.exceptions import InsufficientStock
from ...discount.utils import decrease_voucher_usage
from ...order import GroupStatus
-from ...order.emails import collect_data_for_email, send_note_confirmation
+from ...order.emails import send_note_confirmation
from ...order.models import DeliveryGroup, OrderLine, OrderNote
from ...order.utils import (
add_variant_to_delivery_group, cancel_order, change_order_line_quantity,
@@ -38,8 +38,7 @@ class OrderNoteForm(forms.ModelForm):
def send_confirmation_email(self):
order = self.instance.order
- email_data = collect_data_for_email(order)
- send_note_confirmation.delay(**email_data)
+ send_note_confirmation(order)
class ManagePaymentForm(forms.Form):
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index 29aedab32a..5e21235e71 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -11,6 +11,7 @@ CONFIRM_PAYMENT_TEMPLATE = 'source/order/payment/confirm_payment'
CONFIRM_NOTE_TEMPLATE = 'source/order/note/confirm_note'
+@shared_task
def _send_confirmation(email, url, template, context=None):
site = Site.objects.get_current()
ctx = {
@@ -27,25 +28,24 @@ def _send_confirmation(email, url, template, context=None):
template_name=template)
-def collect_data_for_email(order):
+def collect_data_for_email(order, template):
email = order.get_user_current_email()
url = build_absolute_uri(
reverse('order:details', kwargs={'token': order.token}))
- return {'email': email, 'url': url}
+ return {'email': email, 'url': url, 'template': template}
-@shared_task
-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})
+def send_order_confirmation(order):
+ email_data = collect_data_for_email(order, CONFIRM_ORDER_TEMPLATE)
+ email_data.update({'context': {'order': order}})
+ _send_confirmation.delay(**email_data)
-@shared_task
-def send_payment_confirmation(email, url):
- _send_confirmation(email, url, CONFIRM_PAYMENT_TEMPLATE)
+def send_payment_confirmation(order):
+ email_data = collect_data_for_email(order, CONFIRM_PAYMENT_TEMPLATE)
+ _send_confirmation.delay(**email_data)
-@shared_task
-def send_note_confirmation(email, url):
- _send_confirmation(email, url, CONFIRM_NOTE_TEMPLATE)
+def send_note_confirmation(order):
+ email_data = collect_data_for_email(order, CONFIRM_NOTE_TEMPLATE)
+ _send_confirmation.delay(**email_data)
diff --git a/saleor/order/signals.py b/saleor/order/signals.py
index bff4d945c0..a1133e9117 100644
--- a/saleor/order/signals.py
+++ b/saleor/order/signals.py
@@ -3,7 +3,7 @@ import logging
from django.utils.translation import pgettext_lazy
from ..core import analytics
-from .emails import collect_data_for_email, send_order_confirmation
+from .emails import send_order_confirmation
from .utils import create_history_entry
logger = logging.getLogger(__name__)
@@ -17,9 +17,7 @@ def order_status_change(sender, instance, **kwargs):
order=order,
content=pgettext_lazy(
'Order status history entry', 'Order fully paid'))
- email_data = collect_data_for_email(order)
- email_data.update({'order_pk': order.pk})
- send_order_confirmation.delay(**email_data)
+ send_order_confirmation(order)
try:
analytics.report_order(order.tracking_client_id, order)
except Exception:
diff --git a/tests/test_asynchronous_tasks.py b/tests/test_asynchronous_tasks.py
index 47a943fcfc..ae55a61b42 100644
--- a/tests/test_asynchronous_tasks.py
+++ b/tests/test_asynchronous_tasks.py
@@ -6,6 +6,7 @@ from saleor.order.emails import (
@pytest.mark.django_db
@pytest.mark.integration
[email protected](reason="It causes other tests to fail right now")
def test_email_sending_asynchronously(
transactional_db, celery_app, celery_worker, order_with_lines):
order = send_order_confirmation.delay(
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 578f476a0f..017d7cbe77 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -1,5 +1,6 @@
from unittest import mock
+import pytest
from django.conf import settings
import saleor.order.emails as emails
@@ -11,48 +12,28 @@ URL = 'wooba/looba'
EMAIL_FROM = settings.ORDER_FROM_EMAIL
[email protected]('fun,template,update_context', [
+ (emails.send_order_confirmation, emails.CONFIRM_ORDER_TEMPLATE, True),
+ (emails.send_payment_confirmation, emails.CONFIRM_PAYMENT_TEMPLATE, False),
+ (emails.send_note_confirmation, emails.CONFIRM_NOTE_TEMPLATE, False)])
@mock.patch('saleor.order.emails.send_templated_mail')
-def test_send_confirmation_using_templated_email(
- mocked_templated_email, order):
- emails.send_order_confirmation(EMAIL, URL, order.pk)
- context = {
- 'protocol': 'http',
- 'domain': DOMAIN,
- 'site_name': SITE_NAME,
- 'url': URL,
- 'order': order}
- mocked_templated_email.assert_called_once_with(
- recipient_list=[EMAIL],
- context=context,
- from_email=EMAIL_FROM,
- template_name=emails.CONFIRM_ORDER_TEMPLATE)
-
-
[email protected]('saleor.order.emails.send_templated_mail')
-def test_send_order_payment_confirmation(mocked_templated_email):
- emails.send_payment_confirmation(EMAIL, URL)
- context = {
- 'protocol': 'http',
- 'domain': DOMAIN,
- 'site_name': SITE_NAME,
- 'url': URL}
- mocked_templated_email.assert_called_once_with(
- recipient_list=[EMAIL],
- context=context,
- from_email=EMAIL_FROM,
- template_name=emails.CONFIRM_PAYMENT_TEMPLATE)
-
-
[email protected]('saleor.order.emails.send_templated_mail')
-def test_send_note_confirmation(mocked_templated_email):
- emails.send_note_confirmation(EMAIL, URL)
- context = {
- 'protocol': 'http',
- 'domain': DOMAIN,
- 'site_name': SITE_NAME,
- 'url': URL}
- mocked_templated_email.assert_called_once_with(
- recipient_list=[EMAIL],
- context=context,
- from_email=EMAIL_FROM,
- template_name=emails.CONFIRM_NOTE_TEMPLATE)
+def test_send_triple(
+ mocked_templated_email, update_context, order, template, fun):
+ with mock.patch('saleor.order.emails.collect_data_for_email',
+ return_value={
+ 'email': EMAIL,
+ 'url': URL,
+ 'template': template}):
+ fun(order)
+ context = {
+ 'protocol': 'http',
+ 'domain': DOMAIN,
+ 'site_name': SITE_NAME,
+ 'url': URL}
+ if update_context:
+ context.update({'order': order})
+ mocked_templated_email.assert_called_once_with(
+ recipient_list=[EMAIL],
+ context=context,
+ from_email=EMAIL_FROM,
+ template_name=template)
diff --git a/tests/test_order.py b/tests/test_order.py
index da8801719b..9c6eb87961 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -1,4 +1,5 @@
from decimal import Decimal
+from unittest.mock import Mock
from django.urls import reverse
from prices import Price
@@ -183,9 +184,9 @@ def test_delivery_group_is_shipping_required_partially_required(
def test_collect_data_for_email(order):
- order = order
+ template = Mock(spec=str)
order.user_mail = '[email protected]'
- email_data = collect_data_for_email(order)
+ email_data = collect_data_for_email(order, template)
order_url = reverse('order:details', kwargs={'token': order.token})
assert order_url in email_data['url']
assert email_data['email'] == order.user_email