Skip to content

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

Order should save current user email

Maciek Jabłońskif262315593d0
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 04533ca2ea..5971fc6b82 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -231,10 +231,10 @@ class Checkout(object):
if self.user.is_authenticated():
order_data['user'] = self.user
+ order_data['user_email'] = self.user.email
+
else:
- # TODO: we should always save email in order not only
- # for anonymous
- order_data['anonymous_user_email'] = self.email
+ order_data['user_email'] = self.email
voucher = self._get_voucher()
if voucher is not None:
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index a2625aa1cf..33ca3cf450 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -205,7 +205,7 @@ def create_fake_order():
user_data = {
'billing_address': address,
'shipping_address': address,
- 'anonymous_user_email': get_email(
+ 'user_email': get_email(
address.first_name, address.last_name)}
order = Order.objects.create(**user_data)
order.change_status('payment-pending')
diff --git a/saleor/order/migrations/0014_auto_20161028_0955.py b/saleor/order/migrations/0014_auto_20161028_0955.py
new file mode 100644
index 0000000000..45fefed22d
--- /dev/null
+++ b/saleor/order/migrations/0014_auto_20161028_0955.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.1 on 2016-10-28 14:55
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('order', '0013_auto_20160906_0741'),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name='order',
+ old_name='anonymous_user_email',
+ new_name='user_email',
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 8c21322ca6..e47d487a2d 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -64,7 +64,7 @@ class Order(models.Model, ItemSet):
Address, related_name='+', editable=False)
shipping_address = models.ForeignKey(
Address, related_name='+', editable=False, null=True)
- anonymous_user_email = models.EmailField(
+ user_email = models.EmailField(
blank=True, default='', editable=False)
token = models.CharField(
pgettext_lazy('Order field', 'token'), max_length=36, unique=True)
@@ -108,11 +108,6 @@ class Order(models.Model, ItemSet):
total = self.get_total()
return total_paid >= total.gross
- def get_user_email(self):
- if self.user:
- return self.user.email
- return self.anonymous_user_email
-
def __iter__(self):
return iter(self.groups.all())
@@ -142,7 +137,7 @@ class Order(models.Model, ItemSet):
Price(0, currency=settings.DEFAULT_CURRENCY))
def send_confirmation_email(self):
- email = self.get_user_email()
+ email = self.user_email
payment_url = build_absolute_uri(
reverse('order:details', kwargs={'token': self.token}))
context = {'payment_url': payment_url}
@@ -374,7 +369,7 @@ class Payment(BasePayment):
reverse('order:details', kwargs={'token': self.order.token}))
def send_confirmation_email(self):
- email = self.order.get_user_email()
+ email = self.order.user_email
order_url = build_absolute_uri(
reverse('order:details', kwargs={'token': self.order.token}))
context = {'order_url': order_url}
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 902c5d9add..daada606b6 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -75,7 +75,7 @@ def start_payment(request, order, variant):
'billing_city': billing.city,
'billing_postcode': billing.postal_code,
'billing_country_code': billing.country,
- 'billing_email': order.get_user_email(),
+ 'billing_email': order.user_email,
'description': _('Order %(order_number)s') % {
'order_number': order},
'billing_country_area': billing.country_area,
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 6f4952065c..bdbf9f6a12 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -132,7 +132,7 @@
{% include 'dashboard/includes/_address.html' with address=address only %}
<div>
- <i class="material-icons">email</i> <a href="mailto:{{ order.get_user_email }}">{{ order.get_user_email }}</a>
+ <i class="material-icons">email</i> <a href="mailto:{{ order.user_email }}">{{ order.user_email }}</a>
</div>
</div>
@@ -152,7 +152,7 @@
{% include 'dashboard/includes/_address.html' with address=address only %}
<div>
- <i class="material-icons">email</i> <a href="mailto:{{ order.get_user_email }}">{{ order.get_user_email }}</a>
+ <i class="material-icons">email</i> <a href="mailto:{{ order.user_email }}">{{ order.user_email }}</a>
</div>
{% endwith %}
</div>
diff --git a/templates/dashboard/payments/detail.html b/templates/dashboard/payments/detail.html
index f5093886d1..dfc2f0306b 100644
--- a/templates/dashboard/payments/detail.html
+++ b/templates/dashboard/payments/detail.html
@@ -67,7 +67,7 @@
{% else %}
<div>{% trans "Account" %}: {% trans "Guest" %}</div>
{% endif %}
- <div><i class="material-icons">email</i> <a href="mailto:{{ payment.order.get_user_email }}">{{ payment.order.get_user_email }}</a></div>
+ <div><i class="material-icons">email</i> <a href="mailto:{{ payment.order.user_email }}">{{ payment.order.user_email }}</a></div>
<div>Customer IP: {{ payment.customer_ip_address }}</div>
</div>
</div>
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index 5a37887b1b..0770e5d6c8 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -1,5 +1,8 @@
from django.core.urlresolvers import reverse
+from saleor.order.models import Order
+from saleor.registration.models import EmailChangeRequest
+from saleor.userprofile.models import User
from tests.utils import get_redirect_location
@@ -186,3 +189,53 @@ def test_client_login(request_cart_with_item, client, admin_user):
assert get_redirect_location(response) == '/'
response = client.get(reverse('checkout:shipping-address'))
assert response.context['checkout'].cart.token == request_cart_with_item.token
+
+
+def test_email_is_saved_in_order(authorized_client, billing_address, normal_user, # pylint: disable=R0914
+ request_cart_with_item, shipping_method):
+ """
+ authorized user change own email after checkout - if is not changed in order
+ """
+ # Prepare some data
+ normal_user.addresses.add(billing_address)
+ request_cart_with_item.user = normal_user
+ request_cart_with_item.save()
+
+ # Enter checkout
+ # Checkout index redirects directly to shipping address step
+ shipping_address = authorized_client.get(reverse('checkout:index'), follow=True)
+
+ # Enter shipping address data
+ shipping_data = {'address': billing_address.pk}
+ shipping_method_page = authorized_client.post(shipping_address.request['PATH_INFO'],
+ data=shipping_data, follow=True)
+
+ # Select shipping method
+ shipping_method_data = {'method': shipping_method.pk}
+ shipping_method_response = authorized_client.post(shipping_method_page.request['PATH_INFO'],
+ data=shipping_method_data, follow=True)
+
+ # Summary page asks for Billing address, default is the same as shipping
+ payment_method_data = {'address': 'shipping_address'}
+ payment_method_page = authorized_client.post(shipping_method_response.request['PATH_INFO'],
+ data=payment_method_data, follow=True)
+
+ # After summary step, order is created and it waits for payment
+ order = payment_method_page.context['order']
+ assert order.user_email == normal_user.email
+
+ # User change email
+
+ authorized_client.post(reverse('registration:request_email_change'),
+ data={'email': '[email protected]'})
+
+ #get token:
+ token = EmailChangeRequest.objects.get(email='[email protected]').token
+ url = reverse('registration:change_email', args=[token])
+ authorized_client.get(url)
+
+ updated_user = User.objects.get(pk=normal_user.pk)
+ order = Order.objects.get(pk=order.pk)
+ assert updated_user.email != normal_user.email
+ assert updated_user.email == '[email protected]'
+ assert order.user_email == normal_user.email