Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor for satchless-based partitioners
Patryk Zawadzkic3318c762526
diff --git a/cart/__init__.py b/cart/__init__.pyindex f1744950ce..02bf4fe1a9 100755--- a/cart/__init__.py+++ b/cart/__init__.py@@ -2,7 +2,7 @@ from django.conf import settingsfrom django.utils.translation import ugettextfrom prices import Pricefrom satchless import cart-from satchless.item import ItemSet, ItemLine, Item, ItemRange+from satchless.item import Itemclass Shipping(Item):@@ -11,7 +11,7 @@ class Shipping(Item):return Price(5, currency=settings.SATCHLESS_DEFAULT_CURRENCY)-class DeliveryGroup(ItemSet):+class DeliveryGroup(cart.CartPartition):items = None@@ -25,13 +25,26 @@ class DeliveryGroup(ItemSet):def get_delivery_methods(self, **kwargs):yield Shipping(**kwargs)- def get_min_delivery_method(self, **kwargs):+ def get_delivery_total(self, **kwargs):return min(self.get_delivery_methods(),key=lambda x: x.get_price_per_item(**kwargs))-def cart_partition(cart):- yield DeliveryGroup(list(cart))+class Partitioner(cart.CartPartitioner):++ def __iter__(self):+ yield DeliveryGroup(list(self.cart))++ def get_delivery_subtotal(self, partion, **kwargs):+ return partion.get_delivery_total(**kwargs)++ def get_delivery_total(self, **kwargs):+ items = [self.get_delivery_subtotal(partion, **kwargs)+ for partion in self]+ if not items:+ raise AttributeError(+ 'Calling get_delivery_total() for an empty cart')+ return sum(items[1:], items[0])class InsufficientStockException(Exception):diff --git a/cart/forms.py b/cart/forms.pyindex 54a6e3e738..b62f7baa70 100644--- a/cart/forms.py+++ b/cart/forms.py@@ -62,10 +62,11 @@ class ReplaceCartLineForm(AddToCartForm):class ReplaceCartLineFormSet(BaseFormSet):- form = ReplaceCartLineForm- extra = 0- can_order = False+ absolute_max = 9999can_delete = False+ can_order = False+ extra = 0+ form = ReplaceCartLineFormmax_num = Nonedef __init__(self, *args, **kwargs):diff --git a/cart/templates/cart/index.html b/cart/templates/cart/index.htmlindex 2ffcabb6e5..3de07b242c 100644--- a/cart/templates/cart/index.html+++ b/cart/templates/cart/index.html@@ -28,7 +28,7 @@</tbody><tfoot><tr>- <td colspan="2">Shipping: {% gross shipping %}</td>+ <td colspan="2">Shipping: {% gross cart.get_delivery_total %}</td><td><button class="btn" type="submit">Update</button></td><td>Total:</td><td>{% gross cart.get_total %}</td>diff --git a/cart/views.py b/cart/views.pyindex ab83494d86..39c78b6f31 100755--- a/cart/views.py+++ b/cart/views.py@@ -1,26 +1,18 @@from . import get_cart_from_requestfrom .forms import ReplaceCartLineFormSet-from cart import cart_partition+from cart import Partitionerfrom django.shortcuts import redirectfrom django.template.response import TemplateResponse-from prices import Price-from django.conf import settings+def index(request):cart = get_cart_from_request(request)- cart_deliveries = cart_partition(cart)- cart_delivery_prices = [delivery.get_min_delivery_method().get_price()- for delivery in cart_deliveries]- zero = Price(0, currency=settings.SATCHLESS_DEFAULT_CURRENCY)-+ partitioner = Partitioner(cart)formset = ReplaceCartLineFormSet(request.POST or None, cart=cart)-if formset.is_valid():formset.save()return redirect('cart:index')-- return TemplateResponse(request, 'cart/index.html', {- 'cart': cart,- 'formset': formset,- 'shipping': sum(cart_delivery_prices, zero)- })+ return TemplateResponse(+ request, 'cart/index.html', {+ 'cart': partitioner,+ 'formset': formset})