Skip to content

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

Remove shipping_address from checkout object

Anna Siwik0a4b7160d861
diff --git a/saleor/cart/checkout/views/shipping.py b/saleor/cart/checkout/views/shipping.py
index 4ebc26adaf..9642aebcee 100644
--- a/saleor/cart/checkout/views/shipping.py
+++ b/saleor/cart/checkout/views/shipping.py
@@ -14,14 +14,13 @@ def anonymous_user_shipping_address_view(request, cart, checkout):
request.POST or None, country_code=request.country.code,
autocomplete_type='shipping',
initial={'country': request.country.code},
- instance=checkout.shipping_address)
+ 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())
if all([user_form.is_valid(), address_form.is_valid()]):
checkout.email = user_form.cleaned_data['email']
- checkout.shipping_address = address_form.instance
address = address_form.save()
save_shipping_address_in_cart(cart, address)
return redirect('checkout:shipping-method')
@@ -73,12 +72,10 @@ def user_shipping_address_view(request, cart, checkout):
if use_existing_address:
address_id = addresses_form.cleaned_data['address']
address = Address.objects.get(id=address_id)
- checkout.shipping_address = address
save_shipping_address_in_cart(cart, address)
return redirect('checkout:shipping-method')
elif address_form.is_valid():
- checkout.shipping_address = address_form.instance
address = address_form.save()
save_shipping_address_in_cart(cart, address)
return redirect('checkout:shipping-method')
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index c87bc371c9..2ece77f969 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -49,7 +49,6 @@ class Checkout:
self.tracking_code = tracking_code
self.storage = {'version': self.VERSION}
self._shipping_method = None
- self._shipping_address = None
@classmethod
def from_storage(
@@ -102,33 +101,11 @@ class Checkout:
"""Return `True` if taxes are handled in the delivery country."""
return bool(self.get_taxes())
- @property
- def shipping_address(self):
- """Return a shipping address if any."""
- if self._shipping_address is None:
- address = self._get_address_from_storage('shipping_address')
- if address is None and self.user.is_authenticated:
- address = self.user.default_shipping_address
- self._shipping_address = address
- return self._shipping_address
-
- @shipping_address.setter
- def shipping_address(self, address):
- address_data = model_to_dict(address)
- phone_number = address_data.get('phone')
- if phone_number:
- address_data['phone'] = str(address_data['phone'])
- address_data['country'] = smart_text(address_data['country'])
- self.storage['shipping_address'] = address_data
- self.modified = True
- self._shipping_address = address
-
@property
def shipping_method(self):
"""Return a shipping method if any."""
if self._shipping_method is None:
- shipping_address = self.shipping_address
- if shipping_address is None:
+ if not self.cart.shipping_address:
return None
shipping_method_country_id = self.storage.get(
'shipping_method_country_id')
@@ -139,7 +116,7 @@ class Checkout:
id=shipping_method_country_id)
except ShippingMethodCountry.DoesNotExist:
return None
- shipping_country_code = shipping_address.country.code
+ shipping_country_code = self.cart.shipping_address.country.code
allowed_codes = [ANY_COUNTRY, shipping_country_code]
if shipping_method_country.country_code not in allowed_codes:
return None
@@ -187,9 +164,7 @@ class Checkout:
elif (self.user.is_authenticated and
self.user.default_billing_address):
return self.user.default_billing_address
- elif self.shipping_address:
- return self.shipping_address
- return None
+ return self.cart.shipping_address
@billing_address.setter
def billing_address(self, address):
@@ -254,11 +229,6 @@ class Checkout:
del self.storage['voucher_code']
self.modified = True
- @property
- def is_shipping_same_as_billing(self):
- """Return `True` if shipping and billing addresses are identical."""
- return self.shipping_address == self.billing_address
-
def _add_to_user_address_book(self, address, is_billing=False,
is_shipping=False):
if self.user.is_authenticated:
@@ -269,9 +239,6 @@ class Checkout:
def _save_order_billing_address(self):
return self.billing_address.get_copy()
- def _save_order_shipping_address(self):
- return self.shipping_address.get_copy()
-
@transaction.atomic
def create_order(self):
"""Create an order from the checkout session.
@@ -391,10 +358,8 @@ class Checkout:
def get_taxes(self):
"""Return taxes based on shipping address (if set) or IP country."""
- shipping_address = self.shipping_address
-
- if shipping_address:
- return get_taxes_for_country(shipping_address.country)
+ if self.cart.shipping_address:
+ return get_taxes_for_country(self.cart.shipping_address.country)
return self.taxes
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index 7cb5591c84..c8fd10af27 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -44,7 +44,7 @@ def shipping_address_view(request, cart, checkout):
@add_voucher_form
def shipping_method_view(request, cart, checkout):
"""Display the shipping method selection step."""
- country_code = checkout.shipping_address.country.code
+ country_code = cart.shipping_address.country.code
shipping_method_form = ShippingMethodForm(
country_code, request.taxes, request.POST or None,
initial={'method': checkout.shipping_method})
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index 125cd48856..272bf3bad4 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -104,7 +104,7 @@ def summary_with_shipping_view(request, cart, checkout):
address_form, addresses_form, address = get_billing_forms_with_shipping(
request.POST or None, additional_addresses,
checkout.billing_address or Address(country=request.country),
- checkout.shipping_address)
+ cart.shipping_address)
if address is not None:
checkout.billing_address = address
return handle_order_placement(request, checkout)
diff --git a/saleor/checkout/views/validators.py b/saleor/checkout/views/validators.py
index bf6263a2bd..b5e12b8301 100644
--- a/saleor/checkout/views/validators.py
+++ b/saleor/checkout/views/validators.py
@@ -32,10 +32,10 @@ def validate_shipping_address(view):
"""
@wraps(view)
def func(request, cart, checkout):
- if checkout.email is None or checkout.shipping_address is None:
+ if checkout.email is None or checkout.cart.shipping_address is None:
return redirect('checkout:shipping-address')
try:
- checkout.shipping_address.full_clean()
+ checkout.cart.shipping_address.full_clean()
except ValidationError:
return redirect('checkout:shipping-address')
return view(request, cart, checkout)
diff --git a/templates/checkout/shipping_method.html b/templates/checkout/shipping_method.html
index c53cccfb3d..c2a10ad78c 100644
--- a/templates/checkout/shipping_method.html
+++ b/templates/checkout/shipping_method.html
@@ -5,7 +5,7 @@
{% block forms %}
<h3>{% trans "Shipping address" context "Checkout shipping address title" %}</h3>
- {% format_address checkout.shipping_address %}
+ {% format_address cart.shipping_address %}
<p><a href="{% url 'checkout:shipping-address' %}">{% trans "Select other address" %}</a></p>
<hr>
<form method="post" novalidate>
diff --git a/tests/conftest.py b/tests/conftest.py
index d93164e999..e1515b9262 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -356,10 +356,8 @@ def checkout():
@pytest.fixture
def checkout_with_items(request_cart_with_item, customer_user):
- checkout = Checkout(
+ return Checkout(
request_cart_with_item, customer_user, None, None, 'tracking_code')
- checkout.shipping_address = customer_user.default_shipping_address
- return checkout
@pytest.fixture
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 4e4f580a6d..368da65df6 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -49,48 +49,6 @@ def test_checkout_is_shipping_required(checkout):
assert checkout.is_shipping_required is True
[email protected]('user, shipping', [
- (Mock(default_shipping_address='user_shipping'), 'user_shipping'),
- (AnonymousUser(), None)])
-def test_checkout_shipping_address_with_anonymous_user(
- checkout, user, shipping):
- checkout.user = user
- assert checkout._shipping_address is None
- assert checkout.shipping_address == shipping
- assert checkout._shipping_address == shipping
-
-
[email protected]('address_objects, shipping', [
- (Mock(get=Mock(return_value='shipping')), 'shipping'),
- (Mock(get=Mock(side_effect=Address.DoesNotExist)), None)])
-def test_checkout_shipping_address_with_storage(
- checkout, address_objects, shipping, monkeypatch):
- monkeypatch.setattr(
- 'saleor.checkout.core.Address.objects', address_objects)
- checkout.storage['shipping_address'] = {'id': 1}
- assert checkout.shipping_address == shipping
-
-
-def test_checkout_shipping_address_setter(checkout):
- address = Address(first_name='Jan', last_name='Kowalski')
- assert checkout._shipping_address is None
- checkout.shipping_address = address
- assert checkout._shipping_address == address
- assert checkout.storage['shipping_address'] == {
- 'city': '',
- 'city_area': '',
- 'company_name': '',
- 'country': '',
- 'country_area': '',
- 'first_name': 'Jan',
- 'id': None,
- 'last_name': 'Kowalski',
- 'phone': '',
- 'postal_code': '',
- 'street_address_1': '',
- 'street_address_2': ''}
-
-
@pytest.mark.parametrize('shipping_address, shipping_method, value', [
(
Mock(country=Mock(code='PL')),
@@ -103,7 +61,7 @@ def test_checkout_shipping_address_setter(checkout):
def test_checkout_shipping_method(
checkout, shipping_address, shipping_method, value, monkeypatch):
queryset = Mock(get=Mock(return_value=shipping_method))
- monkeypatch.setattr(Checkout, 'shipping_address', shipping_address)
+ monkeypatch.setattr(checkout.cart, 'shipping_address', shipping_address)
monkeypatch.setattr(
'saleor.checkout.core.ShippingMethodCountry.objects', queryset)
checkout.storage['shipping_method_country_id'] = 1
@@ -138,7 +96,8 @@ def test_checkout_shipping_method_setter(checkout):
addresses=Mock(
is_authenticated=Mock(return_value=True))),
'billing_address')])
-def test_checkout_billing_address(checkout, user, address):
+def test_checkout_billing_address(checkout, user, address, monkeypatch):
+ monkeypatch.setattr(checkout.cart, 'shipping_address', None)
checkout.user = user
assert checkout.billing_address == address