Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Implement digital fulfillment email event
NyanKiyoshi17b6aca7f2cb
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex ac6876c8f1..b3dc17a23b 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -19,7 +19,7 @@ from ...events.models import OrderEventfrom ...events.types import OrderEvents, OrderEventsEmailsfrom ...order import OrderStatusfrom ...order.emails import (- send_fulfillment_confirmation, send_fulfillment_update,+ send_fulfillment_confirmation_to_customer, send_fulfillment_update,send_order_confirmation)from ...order.models import Fulfillment, FulfillmentLine, Orderfrom ...order.utils import update_order_prices, update_order_status@@ -620,10 +620,8 @@ def fulfill_order_lines(request, order_pk):quantities=quantities, order_lines=order_lines)]if form.cleaned_data.get('send_mail'):- send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)- events.append(OrderEvent.email_sent_event(- order=order, source=request.user,- email_type=OrderEventsEmails.SHIPPING))+ events += send_fulfillment_confirmation_to_customer(+ order, fulfillment, request.user)OrderEvent.objects.bulk_create(events)else:diff --git a/saleor/events/models/OrderEvent.py b/saleor/events/models/OrderEvent.pyindex e96006b0f3..229bac3819 100644--- a/saleor/events/models/OrderEvent.py+++ b/saleor/events/models/OrderEvent.py@@ -1,4 +1,4 @@-from typing import Dict, List, Union+from typing import Dict, List, Optional, Unionfrom django.conf import settingsfrom django.contrib.auth.base_user import AbstractBaseUser@@ -51,7 +51,7 @@ class OrderEvent(models.Model):def email_sent_event(cls, *,order: Order, email_type: OrderEventsEmails,- source: Union[User, None]) -> models.Model:+ source: Optional[User]) -> models.Model:if source is not None and source.is_anonymous:source = None@@ -141,7 +141,7 @@ class OrderEvent(models.Model):@staticmethoddef _get_payment_data(- amount: Union[Money, None], payment: Payment) -> Dict:+ amount: Optional[Money], payment: Payment) -> Dict:return {'parameters': {'amount': amount,diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.pyindex de9822b821..aebb8a8476 100644--- a/saleor/graphql/order/mutations/fulfillments.py+++ b/saleor/graphql/order/mutations/fulfillments.py@@ -3,9 +3,8 @@ from django.core.exceptions import ValidationErrorfrom django.utils.translation import npgettext_lazy, pgettext_lazyfrom ....events.models import OrderEvent-from ....events.types import OrderEventsEmailsfrom ....order import models-from ....order.emails import send_fulfillment_confirmation+from ....order.emails import send_fulfillment_confirmation_to_customerfrom ....order.utils import (cancel_fulfillment, fulfill_order_line, update_order_status)from ...core.mutations import BaseMutation@@ -13,13 +12,6 @@ from ...order.types import Fulfillment, Orderfrom ..types import OrderLine-def send_fulfillment_confirmation_to_customer(order, fulfillment, user):- send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)- OrderEvent.email_sent_event(- order=order, email_type=OrderEventsEmails.FULFILLMENT,- source=user).save()--class FulfillmentLineInput(graphene.InputObjectType):order_line_id = graphene.ID(description='The ID of the order line.', name='orderLineId')@@ -113,12 +105,15 @@ class FulfillmentCreate(BaseMutation):fulfillment.lines.bulk_create(fulfillment_lines)update_order_status(order)- OrderEvent.fulfillment_fulfilled_items_event(+ events = [OrderEvent.fulfillment_fulfilled_items_event(order=order, source=user,- quantities=quantities, order_lines=order_lines).save()+ quantities=quantities, order_lines=order_lines)]+if cleaned_input.get('notify_customer', True):- send_fulfillment_confirmation_to_customer(+ events += send_fulfillment_confirmation_to_customer(order, fulfillment, user)++ OrderEvent.objects.bulk_create(events)return fulfillment@classmethoddiff --git a/saleor/order/emails.py b/saleor/order/emails.pyindex 590829b7fb..19213059c2 100644--- a/saleor/order/emails.py+++ b/saleor/order/emails.py@@ -5,6 +5,8 @@ from templated_email import send_templated_mailfrom ..core.emails import get_email_base_contextfrom ..core.utils import build_absolute_uri+from ..events.models import OrderEvent+from ..events.types import OrderEventsEmailsfrom ..seo.schema.email import get_order_confirmation_markupfrom .models import Fulfillment, Order@@ -65,6 +67,23 @@ def send_fulfillment_confirmation(order_pk, fulfillment_pk):send_templated_mail(**email_data)+def send_fulfillment_confirmation_to_customer(order, fulfillment, user):+ send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)++ events = [OrderEvent.email_sent_event(+ order=order, source=user,+ email_type=OrderEventsEmails.FULFILLMENT)]++ # If digital lines were sent in the fulfillment email,+ # trigger the event+ if any((line for line in order if line.variant.is_digital())):+ events.append(OrderEvent.email_sent_event(+ order=order, source=user,+ email_type=OrderEventsEmails.DIGITAL_LINKS))++ return events++@shared_taskdef send_fulfillment_update(order_pk, fulfillment_pk):email_data = collect_data_for_fullfillment_email(diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 81f1e32c5f..7b70ff7e01 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -13,7 +13,7 @@ from ..account.models import Address, Userfrom ..checkout.models import Checkoutfrom ..core import analyticsfrom ..events.models import OrderEvent-from ..events.types import OrderEvents, OrderEventsEmails+from ..events.types import OrderEventsEmailsfrom ..order import utils as order_utilsfrom ..order.emails import send_payment_confirmationfrom ..order.models import Orderdiff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.pyindex d85c6cb27d..a6f46b05e7 100644--- a/tests/api/test_fulfillment.py+++ b/tests/api/test_fulfillment.py@@ -3,7 +3,7 @@ from unittest.mock import patchimport grapheneimport pytest-from saleor.events.types import OrderEvents, OrderEventsEmails+from saleor.events.types import OrderEventsfrom saleor.order.models import FulfillmentStatusfrom tests.api.utils import get_graphql_content@@ -35,11 +35,10 @@ CREATE_FULFILLMENT_QUERY = """"""-@patch(- 'saleor.graphql.order.mutations.fulfillments.'- 'send_fulfillment_confirmation')+@patch('saleor.graphql.order.mutations.fulfillments.'+ 'send_fulfillment_confirmation_to_customer')def test_create_fulfillment(- mock_send_fulfillment_confirmation, staff_api_client, order_with_lines,+ mock_email_fulfillment, staff_api_client, order_with_lines,staff_user, permission_manage_orders):order = order_with_linesquery = CREATE_FULFILLMENT_QUERY@@ -47,7 +46,6 @@ def test_create_fulfillment(order_line = order.lines.first()order_line_id = graphene.Node.to_global_id('OrderLine', order_line.id)tracking = 'Flames tracking'- assert not order.events.all()variables = {'order': order_id,'lines': [{'orderLineId': order_line_id, 'quantity': 1}],@@ -61,26 +59,13 @@ def test_create_fulfillment(assert data['trackingNumber'] == trackingassert len(data['lines']) == 1- event_fulfillment, event_email_sent = order.events.all()- assert event_fulfillment.type == (- OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value)- assert event_fulfillment.parameters == {'fulfilled_items': [- {'item': str(order_line), 'quantity': 1}]}- assert event_fulfillment.user == staff_user-- assert event_email_sent.type == OrderEvents.EMAIL_SENT.value- assert event_email_sent.user == staff_user- assert event_email_sent.parameters == {- 'email': order.user_email,- 'email_type': OrderEventsEmails.FULFILLMENT.value}-- assert mock_send_fulfillment_confirmation.delay.called+ assert mock_email_fulfillment.call_count == 1@patch('saleor.graphql.order.mutations.fulfillments.'- 'send_fulfillment_confirmation')-def test_create_fulfillment_with_emtpy_quantity(+ 'send_fulfillment_confirmation_to_customer')+def test_create_fulfillment_with_empty_quantity(mock_send_fulfillment_confirmation, staff_api_client, order_with_lines,staff_user, permission_manage_orders):order = order_with_lines@@ -106,7 +91,7 @@ def test_create_fulfillment_with_emtpy_quantity(assert data['fulfillmentOrder'] == 1assert data['status'] == FulfillmentStatus.FULFILLED.upper()- assert mock_send_fulfillment_confirmation.delay.called+ assert mock_send_fulfillment_confirmation.called@pytest.mark.parametrize(@@ -229,11 +214,10 @@ def test_cancel_fulfillment(assert event_cancel_fulfillment.user == staff_user-@patch(- 'saleor.graphql.order.mutations.fulfillments.'- 'send_fulfillment_confirmation')+@patch('saleor.graphql.order.mutations.fulfillments.'+ 'send_fulfillment_confirmation_to_customer')def test_create_digital_fulfillment(- mock_send_fulfillment_confirmation, digital_content, staff_api_client,+ mock_email_fulfillment, digital_content, staff_api_client,order_with_lines, staff_user, permission_manage_orders):order = order_with_linesquery = CREATE_FULFILLMENT_QUERY@@ -247,7 +231,6 @@ def test_create_digital_fulfillment(second_line_id = graphene.Node.to_global_id('OrderLine', second_line.id)tracking = 'Flames tracking'- assert not order.events.all()variables = {'order': order_id,'lines': [@@ -258,21 +241,4 @@ def test_create_digital_fulfillment(query, variables, permissions=[permission_manage_orders])get_graphql_content(response)- event_fulfillment, event_email_sent = order.events.all()- assert event_fulfillment.type == (- OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value)- assert event_fulfillment.parameters == {'fulfilled_items': [- {'item': str(order_line), 'quantity': 1},- {'item': str(second_line), 'quantity': 1}]}- assert event_fulfillment.user == staff_user-- assert event_email_sent.type == OrderEvents.EMAIL_SENT.value- assert event_email_sent.user == staff_user- assert event_email_sent.parameters == {- 'email': order.user_email,- 'email_type': OrderEventsEmails.FULFILLMENT.value}-- digital_content.refresh_from_db()- assert digital_content.urls.count() == 1- assert digital_content.urls.all()[0].line == order_line- assert mock_send_fulfillment_confirmation.delay.called+ assert mock_email_fulfillment.call_count == 1diff --git a/tests/conftest.py b/tests/conftest.pyindex bfe29ab3d4..2b7edef5b4 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -978,6 +978,8 @@ def digital_content(category, media_root):product=product, sku='SKU_554', cost_price=Money(1, 'USD'), quantity=5,quantity_allocated=3)+ assert product_variant.is_digital()+image_file, image_name = create_image()d_content = DigitalContent.objects.create(content_file=image_file, product_variant=product_variant,diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 5ffac20384..b95e52a513 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -1,5 +1,6 @@import jsonfrom decimal import Decimal+from unittest.mock import patchimport pytestfrom django.urls import reverse@@ -16,6 +17,7 @@ from saleor.discount.utils import increase_voucher_usagefrom saleor.events.models import OrderEventfrom saleor.events.types import OrderEvents, OrderEventsEmailsfrom saleor.order import FulfillmentStatus, OrderStatus+from saleor.order.emails import send_fulfillment_confirmation_to_customerfrom saleor.order.models import Order, OrderLinefrom saleor.order.utils import add_variant_to_order, change_order_line_quantityfrom saleor.payment import ChargeStatus, TransactionKind@@ -978,7 +980,58 @@ def test_view_mark_order_as_paid(admin_client, order_with_lines):type=OrderEvents.ORDER_MARKED_AS_PAID.value).exists()-def test_view_fulfill_order_lines(admin_client, order_with_lines):+@patch('saleor.order.utils.emails.send_fulfillment_confirmation')[email protected]('has_standard,has_digital', (+ (True, True),+ (True, False),+ (False, True)))+def test_send_fulfillment_order_lines_mails(+ mocked_send_fulfillment_confirmation,+ staff_user,+ fulfilled_order, fulfillment, digital_content,+ has_standard, has_digital):++ order = fulfilled_order+ assert order.lines.count() == 2++ if not has_standard:+ line = order.lines.all()[0]+ line.variant = digital_content.product_variant+ assert line.is_digital+ line.save()++ if has_digital:+ line = order.lines.all()[1]+ line.variant = digital_content.product_variant+ assert line.is_digital+ line.save()++ events = send_fulfillment_confirmation_to_customer(+ order=order, fulfillment=fulfillment, user=staff_user)++ mocked_send_fulfillment_confirmation.delay.assert_called_once_with(+ order.pk, fulfillment.pk)++ # Ensure the standard fulfillment event was triggered+ assert events[0].user == staff_user+ assert events[0].parameters == {+ 'email': order.user_email,+ 'email_type': OrderEventsEmails.FULFILLMENT.value}++ if has_digital:+ assert len(events) == 2+ assert events[1].user == staff_user+ assert events[1].parameters == {+ 'email': order.user_email,+ 'email_type': OrderEventsEmails.DIGITAL_LINKS.value}+ else:+ assert len(events) == 1+++@patch('saleor.dashboard.order.views.'+ 'send_fulfillment_confirmation_to_customer')+def test_view_fulfill_order_lines(+ mock_email_fulfillment, admin_client, order_with_lines):url = reverse('dashboard:fulfill-order-lines',kwargs={'order_pk': order_with_lines.pk})@@ -1001,6 +1054,7 @@ def test_view_fulfill_order_lines(admin_client, order_with_lines):order_with_lines.refresh_from_db()for line in order_with_lines.lines.all():assert line.quantity_unfulfilled == 0+ assert mock_email_fulfillment.call_count == 1def test_view_fulfill_order_lines_with_empty_quantity(admin_client, order_with_lines):