Skip to content

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

Move handling is_shipping_required to Cart

Anna Siwike98eba392da0
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index b2f8e6531d..bd4c012464 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -92,6 +92,12 @@ class Cart(models.Model):
class Meta:
ordering = ('-last_status_change',)
+ def __repr__(self):
+ return 'Cart(quantity=%s)' % (self.quantity,)
+
+ def __len__(self):
+ return self.lines.count()
+
def update_quantity(self):
"""Recalculate cart quantity based on lines."""
total_lines = self.count()['total_quantity']
@@ -126,12 +132,6 @@ class Cart(models.Model):
"""Return `True` if any of the lines requires shipping."""
return any(line.is_shipping_required() for line in self.lines.all())
- def __repr__(self):
- return 'Cart(quantity=%s)' % (self.quantity,)
-
- def __len__(self):
- return self.lines.count()
-
def get_shipping_price(self, taxes):
return (
self.shipping_method.get_total_price(taxes)
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 72b3a0d1e3..e11d2a04b2 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -89,11 +89,6 @@ class Checkout:
return Address(**address_data)
return None
- @property
- def is_shipping_required(self):
- """Return `True` if this checkout session needs shipping."""
- return self.cart.is_shipping_required()
-
@property
def email(self):
"""Return the customer email if any."""
@@ -220,7 +215,7 @@ class Checkout:
# Voucher expired in meantime, abort order placement
return None
- if self.is_shipping_required:
+ if self.cart.is_shipping_required():
shipping_address = self.cart.shipping_address
if self.cart.user:
if shipping_address not in self.cart.user.addresses.all():
@@ -233,7 +228,7 @@ class Checkout:
self._add_to_user_address_book(
self.billing_address, is_billing=True)
- if self.is_shipping_required:
+ if self.cart.is_shipping_required():
shipping_method = self.cart.shipping_method
shipping_method_name = smart_text(shipping_method)
else:
@@ -315,8 +310,7 @@ class Checkout:
def get_total(self):
"""Calculate order total with shipping and discount amount."""
total = self.get_subtotal()
- if self.cart.shipping_method and self.is_shipping_required:
- total += self.cart.get_shipping_price(self.get_taxes())
+ total += self.cart.get_shipping_price(self.get_taxes())
if self.discount:
total -= self.discount
return total
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index e825f6af52..60fae2076f 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -16,7 +16,7 @@ def _get_value_voucher_discount_for_checkout(voucher, checkout):
def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
"""Calculate discount value for a voucher of shipping type."""
- if not checkout.is_shipping_required:
+ if not checkout.cart.is_shipping_required():
msg = pgettext(
'Voucher not applicable',
'Your order does not require shipping.')
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index 53497076c8..654c50b1cd 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -26,7 +26,7 @@ def index_view(request, cart, checkout):
@add_voucher_form
def summary_view(request, cart, checkout):
"""Display the correct order summary."""
- if checkout.is_shipping_required:
+ if cart.is_shipping_required():
view = validate_shipping_address(summary_with_shipping_view)
view = validate_shipping_method(view)
return view(request, cart, checkout)
diff --git a/saleor/checkout/views/validators.py b/saleor/checkout/views/validators.py
index ed45d519a4..71c92a537b 100644
--- a/saleor/checkout/views/validators.py
+++ b/saleor/checkout/views/validators.py
@@ -66,7 +66,7 @@ def validate_is_shipping_required(view):
"""
@wraps(view)
def func(request, cart, checkout):
- if not checkout.is_shipping_required:
+ if not cart.is_shipping_required():
return redirect('checkout:summary')
return view(request, cart, checkout)
return func
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 979ab75d94..dd4ce93142 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -43,12 +43,6 @@ def test_checkout_clear_storage(checkout):
assert checkout.modified is True
-def test_checkout_is_shipping_required(checkout):
- cart = Mock(is_shipping_required=Mock(return_value=True))
- checkout.cart = cart
- assert checkout.is_shipping_required is True
-
-
@pytest.mark.parametrize('user, address', [
(AnonymousUser(), None),
(
@@ -119,8 +113,8 @@ def test_checkout_taxes(checkout_with_items, shipping_method, vatlayer):
cart = checkout_with_items.cart
cart.shipping_method = shipping_method.price_per_country.get()
cart.save()
- assert checkout_with_items.shipping_price == TaxedMoney(
- net=Money('8.13', 'USD'), gross=Money(10, 'USD'))
+ assert checkout_with_items.cart.get_shipping_price(vatlayer) == (
+ TaxedMoney(net=Money('8.13', 'USD'), gross=Money(10, 'USD')))
subtotal = checkout_with_items.cart.get_total(taxes=vatlayer)
assert checkout_with_items.get_subtotal() == subtotal
@@ -190,8 +184,8 @@ def test_shipping_voucher_checkout_discount(
net=Money(shipping_cost, 'USD'), gross=Money(shipping_cost, 'USD'))
checkout = Mock(
get_subtotal=Mock(return_value=subtotal),
- is_shipping_required=True,
cart=Mock(
+ is_shipping_required=Mock(return_value=True),
shipping_method=Mock(
price=Money(shipping_cost, 'USD'),
country_code=shipping_country_code,
@@ -226,8 +220,9 @@ def test_shipping_voucher_checkout_discount_not_applicable(
discount_type, apply_to, limit, subtotal, error_msg):
subtotal_price = TaxedMoney(net=subtotal, gross=subtotal)
checkout = Mock(
- is_shipping_required=is_shipping_required,
- cart=Mock(shipping_method=shipping_method),
+ cart=Mock(
+ is_shipping_required=Mock(return_value=is_shipping_required),
+ shipping_method=shipping_method),
get_subtotal=Mock(return_value=subtotal_price))
voucher = Voucher(
code='unique', type=VoucherType.SHIPPING,
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index b04fd28cae..1f69e1b9dd 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -139,9 +139,11 @@ def test_checkout_flow_authenticated_user(
assert len(payment.get_purchased_items()) == len(order.lines.all())
-def test_address_without_shipping(request_cart_with_item, client, monkeypatch):
- monkeypatch.setattr(
- 'saleor.checkout.core.Checkout.is_shipping_required', False)
+def test_address_without_shipping(request_cart_with_item, client):
+ line = request_cart_with_item.lines.get()
+ product_type = line.variant.product.product_type
+ product_type.is_shipping_required = False
+ product_type.save()
response = client.get(reverse('checkout:shipping-address'))
assert response.status_code == 302
@@ -149,9 +151,11 @@ def test_address_without_shipping(request_cart_with_item, client, monkeypatch):
def test_shipping_method_without_shipping(
- request_cart_with_item, client, monkeypatch):
- monkeypatch.setattr('saleor.checkout.core.Checkout.is_shipping_required',
- False)
+ request_cart_with_item, client):
+ line = request_cart_with_item.lines.get()
+ product_type = line.variant.product.product_type
+ product_type.is_shipping_required = False
+ product_type.save()
response = client.get(reverse('checkout:shipping-method'))
assert response.status_code == 302