Skip to content

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

Move user_email from checkout object to cart

Anna Siwik163ee758f1ba
diff --git a/saleor/cart/checkout/forms.py b/saleor/cart/checkout/forms.py
index eabc12494b..60786fb376 100644
--- a/saleor/cart/checkout/forms.py
+++ b/saleor/cart/checkout/forms.py
@@ -10,13 +10,28 @@ from ...shipping.models import ShippingMethodCountry
from ...shipping.utils import get_taxed_shipping_price
-class AnonymousUserShippingForm(forms.Form):
+class AnonymousUserShippingForm(forms.ModelForm):
"""Additional shipping information form for users who are not logged in."""
- email = forms.EmailField(
- required=True, widget=forms.EmailInput(
- attrs={'autocomplete': 'shipping email'}),
- label=pgettext_lazy('Shipping form field label', 'Email'))
+ user_email = forms.EmailField(
+ widget=forms.EmailInput(attrs={'autocomplete': 'shipping email'}),
+ label=pgettext_lazy('Address form field label', 'Email'))
+
+ class Meta:
+ model = Cart
+ fields = ['user_email']
+
+
+class AnonymousUserBillingForm(forms.ModelForm):
+ """Additional billing information form for users who are not logged in."""
+
+ user_email = forms.EmailField(
+ widget=forms.EmailInput(attrs={'autocomplete': 'billing email'}),
+ label=pgettext_lazy('Address form field label', 'Email'))
+
+ class Meta:
+ model = Cart
+ fields = ['user_email']
class AddressChoiceForm(forms.Form):
@@ -109,15 +124,6 @@ class CartShippingMethodForm(forms.ModelForm):
method_field.empty_label = None
-class AnonymousUserBillingForm(forms.Form):
- """Additional billing information form for users who are not logged in."""
-
- email = forms.EmailField(
- required=True, widget=forms.EmailInput(
- attrs={'autocomplete': 'billing email'}),
- label=pgettext_lazy('Billing form field label', 'Email'))
-
-
class NoteForm(forms.Form):
"""Form to add a note to an order."""
diff --git a/saleor/cart/checkout/views/__init__.py b/saleor/cart/checkout/views/__init__.py
index 3fd497ae2e..6cc98e1b78 100644
--- a/saleor/cart/checkout/views/__init__.py
+++ b/saleor/cart/checkout/views/__init__.py
@@ -2,9 +2,10 @@ from django.shortcuts import redirect
from django.template.response import TemplateResponse
from ....checkout.core import load_checkout
-from ....checkout.views import (
- add_voucher_form, validate_cart, validate_is_shipping_required,
- validate_shipping_address, validate_shipping_method, validate_voucher)
+from ....checkout.views import add_voucher_form, validate_voucher
+from ....checkout.views.validators import (
+ validate_cart, validate_is_shipping_required, validate_shipping_address,
+ validate_shipping_method)
from ..forms import CartShippingMethodForm
from ..utils import get_checkout_data
from .shipping import (
diff --git a/saleor/cart/checkout/views/shipping.py b/saleor/cart/checkout/views/shipping.py
index 9d24c1733d..36d8826c3a 100644
--- a/saleor/cart/checkout/views/shipping.py
+++ b/saleor/cart/checkout/views/shipping.py
@@ -16,10 +16,10 @@ def anonymous_user_shipping_address_view(request, cart, checkout):
instance=cart.shipping_address)
user_form = AnonymousUserShippingForm(
- not preview and request.POST or None, initial={'email': checkout.email}
- if not preview else request.POST.dict())
+ not preview and request.POST or None,
+ request.POST.dict() if preview else None, instance=cart)
if all([user_form.is_valid(), address_form.is_valid()]):
- checkout.email = user_form.cleaned_data['email']
+ user_form.save()
address = address_form.save()
save_shipping_address_in_cart(cart, address)
return redirect('checkout:shipping-method')
@@ -40,10 +40,11 @@ def user_shipping_address_view(request, cart, checkout):
"""
data = request.POST or None
user_addresses = request.user.addresses.all()
- checkout.email = request.user.email
shipping_address = cart.shipping_address
+ cart.user_email = request.user.email
+ cart.save()
- if shipping_address and shipping_address in cart.user.addresses.all():
+ if shipping_address and shipping_address in user_addresses:
address_form, preview = get_address_form(
data, country_code=request.country.code,
initial={'country': request.country})
diff --git a/saleor/cart/checkout/views/summary.py b/saleor/cart/checkout/views/summary.py
index 17ec8a4b85..30ec079c20 100644
--- a/saleor/cart/checkout/views/summary.py
+++ b/saleor/cart/checkout/views/summary.py
@@ -129,8 +129,7 @@ def anonymous_summary_without_shipping(request, cart, checkout):
note_form = NoteForm(request.POST or None, checkout=checkout)
if note_form.is_valid():
note_form.set_checkout_note()
- user_form = AnonymousUserBillingForm(
- request.POST or None, initial={'email': checkout.email})
+ user_form = AnonymousUserBillingForm(request.POST or None, instance=cart)
billing_address = cart.billing_address
if billing_address:
address_form, preview = get_address_form(
@@ -142,7 +141,7 @@ def anonymous_summary_without_shipping(request, cart, checkout):
autocomplete_type='billing', initial={'country': request.country})
if all([user_form.is_valid(), address_form.is_valid()]) and not preview:
- checkout.email = user_form.cleaned_data['email']
+ user_form.save()
address = address_form.save()
save_billing_address_in_cart(cart, address)
return handle_order_placement(request, checkout)
diff --git a/saleor/cart/migrations/0007_merge_checkout_into_cart.py b/saleor/cart/migrations/0007_merge_checkout_into_cart.py
index c5ca5a5530..f1d923eb0e 100644
--- a/saleor/cart/migrations/0007_merge_checkout_into_cart.py
+++ b/saleor/cart/migrations/0007_merge_checkout_into_cart.py
@@ -28,4 +28,9 @@ class Migration(migrations.Migration):
name='shipping_method',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='carts', to='shipping.ShippingMethodCountry'),
),
+ migrations.AddField(
+ model_name='cart',
+ name='user_email',
+ field=models.EmailField(blank=True, default='', max_length=254),
+ ),
]
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index e97014860e..ef87be8aa9 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -80,6 +80,7 @@ class Cart(models.Model):
quantity = models.PositiveIntegerField(default=0)
# data used for handling checkout process
+ user_email = models.EmailField(blank=True, default='')
billing_address = models.ForeignKey(
Address, related_name='+', editable=False, null=True,
on_delete=models.SET_NULL)
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index ef7c6094b6..a68734cfa7 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -2,9 +2,7 @@ from django.shortcuts import redirect
from django.template.response import TemplateResponse
from .discount import add_voucher_form, validate_voucher
-from .validators import (
- validate_cart, validate_shipping_address,
- validate_shipping_method, validate_is_shipping_required)
+from .validators import validate_cart, validate_is_shipping_required
from ..core import load_checkout
from ...account.forms import LoginForm
diff --git a/saleor/checkout/views/validators.py b/saleor/checkout/views/validators.py
index 139afc29e0..97ebc171ae 100644
--- a/saleor/checkout/views/validators.py
+++ b/saleor/checkout/views/validators.py
@@ -32,7 +32,7 @@ def validate_shipping_address(view):
"""
@wraps(view)
def func(request, cart, checkout):
- if checkout.email is None or cart.shipping_address is None:
+ if cart.user_email is None or cart.shipping_address is None:
return redirect('checkout:shipping-address')
try:
cart.shipping_address.full_clean()
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index a55db1c67e..197ae7ed16 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -179,10 +179,6 @@ def test_summary_without_address(request_cart_with_item, client):
def test_summary_without_shipping_method(
request_cart_with_item, client, monkeypatch):
- # address test return true
- monkeypatch.setattr(
- 'saleor.checkout.core.Checkout.email', True)
-
response = client.get(reverse('checkout:summary'))
assert response.status_code == 302
assert (