Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle processing newly created delivery group
Anna Siwik4930989c9d7a
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex 38f2fb8d0c..f3175fd885 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -14,13 +14,10 @@ from ..cart.utils import get_or_empty_db_cartfrom ..core import analyticsfrom ..discount.models import NotApplicable, Voucherfrom ..order.models import Order-from ..order.utils import fill_group_with_partitionfrom ..shipping.models import ANY_COUNTRY, ShippingMethodCountryfrom ..userprofile.models import Addressfrom ..userprofile.utils import store_user_address-from phonenumber_field.phonenumber import PhoneNumber-STORAGE_SESSION_KEY = 'checkout_storage'@@ -329,8 +326,8 @@ class Checkout:else None)group = order.groups.create(shipping_method_name=shipping_method_name)- fill_group_with_partition(- group, partition, discounts=self.cart.discounts)+ group.process(partition, self.cart.discounts)+ group.save()if voucher is not None:Voucher.objects.increase_usage(voucher)diff --git a/saleor/order/models.py b/saleor/order/models.pyindex ac06385d13..cb83c96dbe 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -15,7 +15,8 @@ from payments.models import BasePaymentfrom prices import FixedDiscount, Pricefrom satchless.item import ItemLine, ItemSet-from saleor.order.transitions import ship_delivery_group, cancel_delivery_group+from saleor.order.transitions import (+ cancel_delivery_group, process_delivery_group, ship_delivery_group)from . import emails, GroupStatus, OrderStatusfrom ..core.utils import build_absolute_urifrom ..discount.models import Voucher@@ -214,6 +215,11 @@ class DeliveryGroup(models.Model, ItemSet):return iter(self.lines.all())return super().__iter__()+ @transition(+ field=status, source=GroupStatus.NEW, target=GroupStatus.NEW)+ def process(self, partition, discounts=None):+ process_delivery_group(self, partition, discounts)+@transition(field=status, source=GroupStatus.NEW, target=GroupStatus.SHIPPED)def ship(self):diff --git a/saleor/order/transitions.py b/saleor/order/transitions.pyindex 21695978d5..ed8dbe80d2 100644--- a/saleor/order/transitions.py+++ b/saleor/order/transitions.py@@ -1,7 +1,39 @@+from satchless.item import InsufficientStock+from saleor.order import GroupStatusfrom saleor.product.models import Stock+def process_delivery_group(group, partition, discounts=None):+ """Fills shipment group with order lines created from partition items."""+ for item in partition:+ variant = item.variant+ quantity_left = item.get_quantity()+ price = variant.get_price_per_item(discounts)+ while quantity_left > 0:+ stock = variant.select_stockrecord()+ if not stock:+ raise InsufficientStock(variant)+ quantity = (+ stock.quantity_available+ if quantity_left > stock.quantity_available+ else quantity_left+ )+ group.lines.create(+ product=variant.product,+ product_name=variant.display_product(),+ product_sku=variant.sku,+ quantity=quantity,+ unit_price_net=price.net,+ unit_price_gross=price.gross,+ stock=stock,+ stock_location=stock.location.name)+ Stock.objects.allocate_stock(stock, quantity)+ # refresh stock for accessing quantity_allocated+ stock.refresh_from_db()+ quantity_left -= quantity++def cancel_delivery_group(group):"""Cancels delivery group by returning products to stocks."""if group.status == GroupStatus.NEW:diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 5defb98b71..58182da563 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -59,16 +59,8 @@ def attach_order_to_user(order, user):order.save(update_fields=['user'])-def fill_group_with_partition(group, partition, discounts=None):- """Fills shipment group with order lines created from partition items."""- for item in partition:- add_variant_to_delivery_group(- group, item.variant, item.get_quantity(), discounts,- add_to_existing=False)--def add_variant_to_delivery_group(- group, variant, total_quantity, discounts=None, add_to_existing=True):+ group, variant, total_quantity, discounts=None):"""Adds total_quantity of variant to group.Raises InsufficientStock exception if quantity could not be fulfilled.@@ -78,10 +70,8 @@ def add_variant_to_delivery_group(Order lines are created by increasing quantity of lines,as long as total_quantity of variant will be added."""- quantity_left = (- add_variant_to_existing_lines(- group, variant, total_quantity) if add_to_existing- else total_quantity)+ quantity_left = add_variant_to_existing_lines(+ group, variant, total_quantity)price = variant.get_price_per_item(discounts)while quantity_left > 0:stock = variant.select_stockrecord()diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 85cbea6334..d49e451681 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -2,15 +2,17 @@ from decimal import Decimalfrom django.urls import reverseimport pytest+from tests.utils import get_redirect_location, get_url_path-from saleor.dashboard.order.forms import ChangeQuantityForm, MoveLinesForm+from saleor.dashboard.order.forms import ChangeQuantityFormfrom saleor.order import OrderStatusfrom saleor.order.models import (DeliveryGroup, Order, OrderHistoryEntry, OrderLine)+from saleor.order.transitions import process_delivery_groupfrom saleor.order.utils import (add_variant_to_existing_lines, change_order_line_quantity,- fill_group_with_partition, remove_empty_groups)+ remove_empty_groups)from saleor.product.models import ProductVariant, Stock, StockLocation@@ -127,7 +129,7 @@ def test_view_change_order_line_quantity_with_invalid_data(def test_dashboard_change_quantity_form(request_cart_with_item, order):cart = request_cart_with_itemgroup = DeliveryGroup.objects.create(order=order)- fill_group_with_partition(group, cart.lines.all())+ process_delivery_group(group, cart.lines.all())order_line = group.lines.get()# Check max quantity validationdiff --git a/tests/test_order.py b/tests/test_order.pyindex 8b5120a07f..038d8737db 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -2,8 +2,8 @@ from prices import Pricefrom saleor.cart.models import Cartfrom saleor.order import models, OrderStatus-from saleor.order.utils import (- add_variant_to_delivery_group, fill_group_with_partition)+from saleor.order.transitions import process_delivery_group+from saleor.order.utils import add_variant_to_delivery_groupdef test_total_property():@@ -33,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)- fill_group_with_partition(delivery_group, cart.lines.all())+ process_delivery_group(delivery_group, cart.lines.all())order_line = delivery_group.lines.get()stock = order_line.stockassert stock.quantity_allocated == 2@@ -42,7 +42,7 @@ def test_stock_allocation(billing_address, product_in_stock):def test_order_discount(sale, order, request_cart_with_item):cart = request_cart_with_itemgroup = models.DeliveryGroup.objects.create(order=order)- fill_group_with_partition(+ process_delivery_group(group, cart.lines.all(), discounts=cart.discounts)line = group.lines.first()assert line.get_price_per_item() == Price(currency="USD", net=5)