Skip to content

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

Move DeliveryGroup.add_items_from_partition to a function

Patryk Zawadzki4f36a3995cd6
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 191225f0fd..e58ee17dcf 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -11,6 +11,7 @@ from ..cart.utils import get_or_empty_db_cart
from ..core import analytics
from ..discount.models import Voucher, NotApplicable
from ..order.models import Order
+from ..order.utils import add_items_to_delivery_group
from ..shipping.models import ShippingMethodCountry, ANY_COUNTRY
from ..userprofile.models import Address
from ..userprofile.utils import store_user_address
@@ -261,8 +262,8 @@ class Checkout(object):
group = order.groups.create(
shipping_price=shipping_price,
shipping_method_name=shipping_method_name)
- group.add_items_from_partition(
- partition, discounts=self.cart.discounts)
+ add_items_to_delivery_group(
+ group, partition, discounts=self.cart.discounts)
if voucher is not None:
Voucher.objects.increase_usage(voucher)
diff --git a/saleor/order/models.py b/saleor/order/models.py
index e6f965e104..29fdfd6aa7 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -272,25 +272,6 @@ class DeliveryGroup(models.Model, ItemSet):
subtotal = super(DeliveryGroup, self).get_total(**kwargs)
return subtotal + self.shipping_price
- def add_items_from_partition(self, partition, discounts=None):
- for item_line in partition:
- product_variant = item_line.variant
- price = item_line.get_price_per_item(discounts)
- quantity = item_line.get_quantity()
- stock = product_variant.select_stockrecord(quantity)
- self.items.create(
- product=product_variant.product,
- quantity=quantity,
- unit_price_net=price.net,
- product_name=product_variant.display_product(),
- product_sku=product_variant.sku,
- unit_price_gross=price.gross,
- stock=stock,
- stock_location=stock.location.name if stock else None)
- if stock:
- # allocate quantity to avoid overselling
- Stock.objects.allocate_stock(stock, quantity)
-
def get_total_quantity(self):
return sum([item.get_quantity() for item in self])
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index ca0106d9b9..b1f54ededf 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -1,12 +1,13 @@
import logging
+from functools import wraps
from payments.signals import status_changed
from django.dispatch import receiver
from django.shortcuts import get_object_or_404, redirect
-from functools import wraps
from .models import Order
from ..core import analytics
+from ..product.models import Stock
from ..userprofile.utils import store_user_address
@@ -45,3 +46,24 @@ def attach_order_to_user(order, user):
if order.shipping_address:
store_user_address(user, order.shipping_address, shipping=True)
order.save(update_fields=['user'])
+
+
+def add_items_to_delivery_group(delivery_group, partition, discounts=None):
+ for item_line in partition:
+ product_variant = item_line.variant
+ price = item_line.get_price_per_item(discounts)
+ quantity = item_line.get_quantity()
+ stock = product_variant.select_stockrecord(quantity)
+ delivery_group.items.create(
+ product=product_variant.product,
+ quantity=quantity,
+ unit_price_net=price.net,
+ product_name=product_variant.display_product(),
+ product_sku=product_variant.sku,
+ unit_price_gross=price.gross,
+ stock=stock,
+ stock_location=stock.location.name if stock else None)
+ if stock:
+ # allocate quantity to avoid overselling
+ Stock.objects.allocate_stock(stock, quantity)
+
diff --git a/tests/test_order.py b/tests/test_order.py
index 736a072202..c6316180a3 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -3,6 +3,7 @@ from prices import Price
from saleor.cart.models import Cart
from saleor.dashboard.order.forms import ChangeQuantityForm
from saleor.order import models
+from saleor.order.utils import add_items_to_delivery_group
def test_total_property():
@@ -32,7 +33,7 @@ def test_stock_allocation(billing_address, product_in_stock):
cart.add(variant, quantity=2)
order = models.Order.objects.create(billing_address=billing_address)
delivery_group = models.DeliveryGroup.objects.create(order=order)
- delivery_group.add_items_from_partition(cart.lines.all())
+ add_items_to_delivery_group(delivery_group, cart.lines.all())
order_line = delivery_group.items.get()
stock = order_line.stock
assert stock.quantity_allocated == 2
@@ -41,7 +42,7 @@ def test_stock_allocation(billing_address, product_in_stock):
def test_dashboard_change_quantity_form(request_cart_with_item, order):
cart = request_cart_with_item
group = models.DeliveryGroup.objects.create(order=order)
- group.add_items_from_partition(cart.lines.all())
+ add_items_to_delivery_group(group, cart.lines.all())
order_line = group.items.get()
# Check available quantity validation
@@ -72,6 +73,7 @@ def test_dashboard_change_quantity_form(request_cart_with_item, order):
def test_order_discount(sale, order, request_cart_with_item):
cart = request_cart_with_item
group = models.DeliveryGroup.objects.create(order=order)
- group.add_items_from_partition(cart.lines.all(), discounts=cart.discounts)
+ add_items_to_delivery_group(
+ group, cart.lines.all(), discounts=cart.discounts)
item = group.items.first()
assert item.get_price_per_item() == Price(currency="USD", net=5)