Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change Cart to iterable
Anna Siwik8942bc62d7d5
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 2d06a8aeb1..0629eda0c5 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -62,12 +62,15 @@ class Cart(models.Model):def __repr__(self):return 'Cart(quantity=%s)' % (self.quantity,)+ def __iter__(self):+ return iter(self.lines.all())+def __len__(self):return self.lines.count()def is_shipping_required(self):"""Return `True` if any of the lines requires shipping."""- return any(line.is_shipping_required() for line in self.lines.all())+ return any(line.is_shipping_required() for line in self)def get_shipping_price(self, taxes):return (@@ -77,8 +80,7 @@ class Cart(models.Model):def get_subtotal(self, discounts=None, taxes=None):"""Return the total cost of the cart prior to shipping."""- subtotals = (- line.get_total(discounts, taxes) for line in self.lines.all())+ subtotals = (line.get_total(discounts, taxes) for line in self)return sum(subtotals, ZERO_TAXED_MONEY)def get_total(self, discounts=None, taxes=None):@@ -90,7 +92,7 @@ class Cart(models.Model):def get_line(self, variant):"""Return a line matching the given variant and data if any."""- matching_lines = (l for l in self.lines.all() if l.variant == variant)+ matching_lines = (line for line in self if line.variant == variant)return next(matching_lines, None)diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 9658b1b6f5..ca4f5f0579 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -44,7 +44,7 @@ def set_cart_cookie(simple_cart, response):def contains_unavailable_variants(cart):"""Return `True` if cart contains any unfulfillable lines."""try:- for line in cart.lines.all():+ for line in cart:line.variant.check_quantity(line.quantity)except InsufficientStock:return True@@ -66,7 +66,7 @@ def token_is_valid(token):def remove_unavailable_variants(cart):"""Remove any unavailable items from cart."""- for line in cart.lines.all():+ for line in cart:try:add_variant_to_cart(cart, line.variant, line.quantity, replace=True)@@ -77,9 +77,7 @@ def remove_unavailable_variants(cart):def get_product_variants_and_prices(cart, product):"""Get variants and unit prices from cart lines matching the product."""- lines = (- cart_line for cart_line in cart.lines.all()- if cart_line.variant.product_id == product.id)+ lines = (line for line in cart if line.variant.product_id == product.id)for line in lines:for dummy_i in range(line.quantity):yield line.variant, line.variant.get_price()@@ -91,11 +89,10 @@ def get_category_variants_and_prices(cart, root_category):Product is assumed to be in the category if it belongs to any of itsdescendant subcategories."""- products = {cart_line.variant.product for cart_line in cart.lines.all()}- matching_products = set()- for product in products:- if product.category.is_descendant_of(root_category, include_self=True):- matching_products.add(product)+ matching_products = {+ line.variant.product for line in cart+ if line.variant.product.category.is_descendant_of(+ root_category, include_self=True)}for product in matching_products:for line in get_product_variants_and_prices(cart, product):yield line@@ -592,8 +589,7 @@ def change_shipping_address_in_cart(cart, address):def get_cart_data_for_checkout(cart, discounts, taxes):"""Data shared between views in checkout process."""- lines = [- (line, line.get_total(discounts, taxes)) for line in cart.lines.all()]+ lines = [(line, line.get_total(discounts, taxes)) for line in cart]subtotal = cart.get_subtotal(discounts, taxes)total = cart.get_total(discounts, taxes)shipping_price = cart.get_shipping_price(taxes)@@ -802,7 +798,7 @@ def create_order(cart, tracking_code, discounts, taxes):order = Order.objects.create(**order_data)- for line in cart.lines.all():+ for line in cart:add_variant_to_order(order, line.variant, line.quantity, discounts, taxes)diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.pyindex 73c20336e6..b8054ce6d0 100644--- a/saleor/checkout/views/__init__.py+++ b/saleor/checkout/views/__init__.py@@ -216,6 +216,6 @@ def cart_summary(request, cart):data = {'quantity': cart.quantity,'total': cart.get_subtotal(discounts, taxes),- 'lines': [prepare_line_data(line) for line in cart.lines.all()]}+ 'lines': [prepare_line_data(line) for line in cart]}return render(request, 'cart_dropdown.html', data)diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 4a6dbe46c1..82f72b4885 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -459,8 +459,7 @@ def test_view_change_order_line_quantity_with_invalid_data(def test_dashboard_change_quantity_form(request_cart_with_item, order):- cart = request_cart_with_item- for line in cart.lines.all():+ for line in request_cart_with_item:add_variant_to_order(order, line.variant, line.quantity)order_line = order.lines.get()quantity_before = order_line.variant.quantity_allocateddiff --git a/tests/test_cart.py b/tests/test_cart.pyindex c8115ceb2e..f9c092d666 100644--- a/tests/test_cart.py+++ b/tests/test_cart.py@@ -290,7 +290,8 @@ def test_cart_counter(monkeypatch):def test_get_product_variants_and_prices():variant = Mock(product_id=1, id=1, get_price=Mock(return_value=10))cart = MagicMock(spec=Cart)- cart.lines.all.return_value = [Mock(quantity=1, variant=variant)]+ cart.__iter__ = Mock(+ return_value=iter([Mock(quantity=1, variant=variant)]))variants = list(utils.get_product_variants_and_prices(cart, variant))assert variants == [(variant, 10)]@@ -299,11 +300,11 @@ def test_contains_unavailable_variants():missing_variant = Mock(check_quantity=Mock(side_effect=InsufficientStock('')))cart = MagicMock()- cart.lines.all.return_value = [Mock(variant=missing_variant)]+ cart.__iter__ = Mock(return_value=iter([Mock(variant=missing_variant)]))assert utils.contains_unavailable_variants(cart)variant = Mock(check_quantity=Mock())- cart.lines.all.return_value = [Mock(variant=variant)]+ cart.__iter__ = Mock(return_value=iter([Mock(variant=variant)]))assert not utils.contains_unavailable_variants(cart)