Skip to content

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

Refactor getting email from order and checkout

fowczarekf8c2a3847f9d
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 3f4698ad49..8d2da91ae4 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -90,6 +90,9 @@ class Checkout(models.Model):
def __len__(self):
return self.lines.count()
+ def get_customer_email(self):
+ return self.user.email if self.user else self.email
+
def is_shipping_required(self):
"""Return `True` if any of the lines requires shipping."""
return any(line.is_shipping_required() for line in self)
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index ecf9cf4701..ca70f1f02d 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -58,7 +58,7 @@ class CreateOrderFromDraftForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
- if not self.instance.get_user_current_email():
+ if not self.instance.get_customer_email():
self.fields.pop("notify_customer")
def clean(self):
@@ -548,7 +548,7 @@ class FulfillmentTrackingNumberForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
- if not self.instance.order.get_user_current_email():
+ if not self.instance.order.get_customer_email():
self.fields.pop("send_mail")
@@ -675,7 +675,7 @@ class FulfillmentForm(forms.ModelForm):
order = kwargs.pop("order")
super().__init__(*args, **kwargs)
self.instance.order = order
- if not order.get_user_current_email():
+ if not order.get_customer_email():
self.fields.pop("send_mail")
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index f85d9d0e4b..7dd16c6263 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -115,6 +115,10 @@ class Checkout(CountableDjangoObjectType):
interfaces = [graphene.relay.Node]
filter_fields = ["token"]
+ @staticmethod
+ def resolve_email(root: models.Checkout, info):
+ return root.get_customer_email()
+
@staticmethod
def resolve_total_price(root: models.Checkout, info):
taxed_total = (
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index f36e97fd8f..ed9e81e588 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -451,11 +451,7 @@ class Order(CountableDjangoObjectType):
@staticmethod
@gql_optimizer.resolver_hints(select_related="user")
def resolve_user_email(root: models.Order, _info):
- if root.user_email:
- return root.user_email
- if root.user_id:
- return root.user.email
- return None
+ return root.get_customer_email()
@staticmethod
def resolve_available_shipping_methods(root: models.Order, _info):
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index dd5c9d8f48..6030d9eb56 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -23,7 +23,7 @@ def collect_data_for_email(order_pk, template):
template (str): email template path
"""
order = Order.objects.get(pk=order_pk)
- recipient_email = order.get_user_current_email()
+ recipient_email = order.get_customer_email()
email_context = get_email_base_context()
email_context["order_details_url"] = build_absolute_uri(
reverse("order:details", kwargs={"token": order.token})
diff --git a/saleor/order/events.py b/saleor/order/events.py
index 7d7943f6e3..4e6b5b52fb 100644
--- a/saleor/order/events.py
+++ b/saleor/order/events.py
@@ -46,7 +46,7 @@ def email_sent_event(
return OrderEvent.objects.create(
order=order,
type=OrderEvents.EMAIL_SENT,
- parameters={"email": order.get_user_current_email(), "email_type": email_type},
+ parameters={"email": order.get_customer_email(), "email_type": email_type},
**kwargs,
)
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 6c6df861b9..5dd01c6ad6 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -165,7 +165,7 @@ class Order(models.Model):
total_paid = self._total_paid()
return total_paid.gross.amount > 0
- def get_user_current_email(self):
+ def get_customer_email(self):
return self.user.email if self.user else self.user_email
def _total_paid(self):
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index d90588a8af..c0586c63d4 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -103,7 +103,7 @@ def create_payment_information(
def handle_fully_paid_order(order):
events.order_fully_paid_event(order=order)
- if order.get_user_current_email():
+ if order.get_customer_email():
events.email_sent_event(
order=order, user=None, email_type=events.OrderEventsEmails.PAYMENT
)
diff --git a/templates/dashboard/order/modal/edit_customer.html b/templates/dashboard/order/modal/edit_customer.html
index 7bcf926522..bfd57dcd8d 100644
--- a/templates/dashboard/order/modal/edit_customer.html
+++ b/templates/dashboard/order/modal/edit_customer.html
@@ -29,7 +29,7 @@
{% endblock %}
{% block actions_left %}
- {% if order.get_user_current_email %}
+ {% if order.get_customer_email %}
<button type="submit" class="modal-action btn-flat waves-effect" formaction="{% url 'dashboard:order-customer-remove' order_pk=order.pk %}">
{% trans "Remove customer from order" context "Dashboard edit draft order remove action" %}
</button>
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 68ce55796a..4ec9950944 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -550,12 +550,13 @@ def test_draft_order_create(
def test_draft_order_update(
- staff_api_client, permission_manage_orders, order_with_lines
+ staff_api_client, permission_manage_orders, order_with_lines, voucher
):
order = order_with_lines
+ assert not order.voucher
query = """
- mutation draftUpdate($id: ID!, $email: String) {
- draftOrderUpdate(id: $id, input: {userEmail: $email}) {
+ mutation draftUpdate($id: ID!, $voucher: ID!) {
+ draftOrderUpdate(id: $id, input: {voucher: $voucher}) {
errors {
field
message
@@ -566,15 +567,17 @@ def test_draft_order_update(
}
}
"""
- email = "[email protected]"
order_id = graphene.Node.to_global_id("Order", order.id)
- variables = {"id": order_id, "email": email}
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.id)
+ variables = {"id": order_id, "voucher": voucher_id}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
- data = content["data"]["draftOrderUpdate"]["order"]
- assert data["userEmail"] == email
+ data = content["data"]["draftOrderUpdate"]
+ assert not data["errors"]
+ order.refresh_from_db()
+ assert order.voucher
def test_draft_order_update_doing_nothing_generates_no_events(
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 84b5ab3fe1..549f76b9d5 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -644,7 +644,7 @@ def test_create_order_creates_expected_events(
assert email_sent_event.order is order # ensure the mail event is related to order
assert email_sent_event.date # ensure a date was set
assert email_sent_event.parameters == { # ensure the correct parameters were set
- "email": order.get_user_current_email(),
+ "email": order.get_customer_email(),
"email_type": OrderEventsEmails.ORDER,
}
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 24da45032c..8cce55db47 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -66,7 +66,7 @@ def test_send_emails(mocked_templated_email, order, template, send_email, settin
send_email(order.pk)
email_data = emails.collect_data_for_email(order.pk, template)
- recipients = [order.get_user_current_email()]
+ recipients = [order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
@@ -106,7 +106,7 @@ def test_send_confirmation_emails_without_addresses(
send_email(order.pk)
email_data = emails.collect_data_for_email(order.pk, template)
- recipients = [order.get_user_current_email()]
+ recipients = [order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
@@ -143,7 +143,7 @@ def test_send_fulfillment_emails(
fulfilled_order.pk, template, fulfillment.pk
)
- recipients = [fulfilled_order.get_user_current_email()]
+ recipients = [fulfilled_order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 6d2779b74c..197eb5075d 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -138,7 +138,7 @@ def test_handle_fully_paid_order(mock_send_payment_confirmation, order):
assert event_email_sent.type == OrderEvents.EMAIL_SENT
assert event_email_sent.parameters == {
- "email": order.get_user_current_email(),
+ "email": order.get_customer_email(),
"email_type": OrderEventsEmails.PAYMENT,
}