Skip to content

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

Handle processing newly created delivery group

Anna Siwik4930989c9d7a
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 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_cart
from ..core import analytics
from ..discount.models import NotApplicable, Voucher
from ..order.models import Order
-from ..order.utils import fill_group_with_partition
from ..shipping.models import ANY_COUNTRY, ShippingMethodCountry
from ..userprofile.models import Address
from ..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.py
index ac06385d13..cb83c96dbe 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -15,7 +15,8 @@ from payments.models import BasePayment
from prices import FixedDiscount, Price
from 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, OrderStatus
from ..core.utils import build_absolute_uri
from ..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.py
index 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 GroupStatus
from 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.py
index 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.py
index 85cbea6334..d49e451681 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -2,15 +2,17 @@ from decimal import Decimal
from django.urls import reverse
import 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 ChangeQuantityForm
from saleor.order import OrderStatus
from saleor.order.models import (
DeliveryGroup, Order, OrderHistoryEntry, OrderLine)
+from saleor.order.transitions import process_delivery_group
from 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_item
group = 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 validation
diff --git a/tests/test_order.py b/tests/test_order.py
index 8b5120a07f..038d8737db 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -2,8 +2,8 @@ from prices import Price
from saleor.cart.models import Cart
from 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_group
def 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.stock
assert 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_item
group = 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)