Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move DeliveryGroup.add_items_from_partition to a function
Patryk Zawadzki4f36a3995cd6
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex 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_cartfrom ..core import analyticsfrom ..discount.models import Voucher, NotApplicablefrom ..order.models import Order+from ..order.utils import add_items_to_delivery_groupfrom ..shipping.models import ShippingMethodCountry, ANY_COUNTRYfrom ..userprofile.models import Addressfrom ..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.pyindex 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.pyindex ca0106d9b9..b1f54ededf 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -1,12 +1,13 @@import logging+from functools import wrapsfrom payments.signals import status_changedfrom django.dispatch import receiverfrom django.shortcuts import get_object_or_404, redirect-from functools import wrapsfrom .models import Orderfrom ..core import analytics+from ..product.models import Stockfrom ..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.pyindex 736a072202..c6316180a3 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -3,6 +3,7 @@ from prices import Pricefrom saleor.cart.models import Cartfrom saleor.dashboard.order.forms import ChangeQuantityFormfrom saleor.order import models+from saleor.order.utils import add_items_to_delivery_groupdef 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.stockassert 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_itemgroup = 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_itemgroup = 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)