Skip to content

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

Delivery refactoring

Michal Ociepkabbc3d7b34e9c
diff --git a/cart/__init__.py b/cart/__init__.py
index 8e81e77de2..8951eb3258 100755
--- a/cart/__init__.py
+++ b/cart/__init__.py
@@ -1,10 +1,11 @@
+from delivery import get_delivery_methods
from django.conf import settings
from django.utils.translation import ugettext
+from itertools import groupby
from prices import Price
from product.models import StockedProduct
from satchless import cart
from satchless.item import Item, ItemLine, ItemSet, Partitioner
-from delivery import get_delivery_methods
from userprofile.forms import AddressForm
import datetime
@@ -43,11 +44,8 @@ class BaseDeliveryGroup(ItemSet):
class CartPartitioner(Partitioner):
def __iter__(self):
- groups = {}
- for cart_item in self.subject:
- class_name = cart_item.product.__class__.__name__
- groups.setdefault(class_name, []).append(cart_item)
- for items in groups.values():
+ for _key, items in groupby(self.subject,
+ lambda cart_item: type(cart_item.product)):
yield BaseDeliveryGroup(items)
def get_delivery_subtotal(self, partion, **kwargs):
diff --git a/delivery/__init__.py b/delivery/__init__.py
index ce53613ff3..a6c8052cd5 100644
--- a/delivery/__init__.py
+++ b/delivery/__init__.py
@@ -12,9 +12,13 @@ class BaseDelivery(Handler, Item):
self.group = delivery_group
@classmethod
- def can_handle(cls, delivery_group, **kwargs):
+ def can_handle_item(cls, item):
return True
+ @classmethod
+ def can_handle(cls, delivery_group, **kwargs):
+ return all(cls.can_handle_item(item) for item in delivery_group)
+
def get_price_per_item(self, **kwargs):
return Price(0, currency=settings.SATCHLESS_DEFAULT_CURRENCY)
@@ -26,11 +30,8 @@ class DummyShipping(BaseDelivery):
super(DummyShipping, self).__init__(delivery_group, **kwargs)
@classmethod
- def can_handle(cls, delivery_group, **kwargs):
- for item in delivery_group:
- if not isinstance(item.product, PhisicalProduct):
- return False
- return True
+ def can_handle_item(cls, item):
+ return isinstance(item.product, PhisicalProduct)
def get_price_per_item(self, **kwargs):
weight = sum(grup.product.weight for grup in self.group)
@@ -41,12 +42,10 @@ class DummyShipping(BaseDelivery):
class DigitalDelivery(BaseDelivery):
@classmethod
- def can_handle(cls, delivery_group, **kwargs):
- for item in delivery_group:
- if not isinstance(item.product, DigitalShip):
- return False
- return True
+ def can_handle_item(cls, item):
+ return isinstance(item.product, DigitalShip)
def get_delivery_methods(delivery, **kwargs):
- return filter_handlers([DummyShipping, DigitalDelivery],delivery, **kwargs)
+ return filter_handlers([DummyShipping, DigitalDelivery],
+ delivery, **kwargs)