Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move partitioning cart functionality to Cart model
Anna Siwik33dd544f40ab
diff --git a/saleor/cart/models.py b/saleor/cart/models.pyindex 53925e7735..90a92b9a8b 100644--- a/saleor/cart/models.py+++ b/saleor/cart/models.py@@ -1,6 +1,7 @@"""Cart-related ORM models."""from collections import namedtuplefrom decimal import Decimal+from itertools import groupbyfrom uuid import uuid4from django.conf import settings@@ -11,7 +12,6 @@ from django.utils.timezone import nowfrom django_prices.models import PriceFieldfrom jsonfield import JSONFieldfrom prices import Price-from satchless.item import ItemList, partitionfrom . import CartStatus, logger@@ -29,7 +29,7 @@ def find_open_cart_for_user(user):return carts.first()-class ProductGroup(ItemList):+class ProductGroup(list):"""A group of products."""def is_shipping_required(self):@@ -218,10 +218,12 @@ class Cart(models.Model):self.update_quantity()def partition(self):- """Split the card into a list of groups for shipping."""+ """Split the cart into a list of groups for shipping."""grouper = (lambda p: 'physical' if p.is_shipping_required() else 'digital')- return partition(self.lines.all(), grouper, ProductGroup)+ subject = sorted(self.lines.all(), key=grouper)+ for _, lines in groupby(subject, key=grouper):+ yield ProductGroup(lines)class CartLine(models.Model):diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex a9361a0524..c55e009194 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -330,14 +330,14 @@ class Checkout:order = Order.objects.create(**order_data)- for line in self.cart.partition():- shipping_required = line.is_shipping_required()+ for partition in self.cart.partition():+ shipping_required = partition.is_shipping_required()shipping_method_name = (smart_text(self.shipping_method) if shipping_requiredelse None)group = order.groups.create(shipping_method_name=shipping_method_name)- group.process(line, self.cart.discounts)+ group.process(partition, self.cart.discounts)group.save()if voucher is not None: