Skip to content

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

Improve adding variant to group functions

Anna Siwike55253e1077e
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index af5b43a1b2..5094da5630 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -16,7 +16,7 @@ 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 create_order_lines_in_delivery_group
+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
@@ -331,7 +331,7 @@ class Checkout(object):
group = order.groups.create(
shipping_price=shipping_price,
shipping_method_name=shipping_method_name)
- create_order_lines_in_delivery_group(
+ fill_group_with_partition(
group, partition, discounts=self.cart.discounts)
if voucher is not None:
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 50619b6d23..0580c7f6e1 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -15,7 +15,7 @@ from ...discount.models import Voucher
from ...order import OrderStatus
from ...order.models import DeliveryGroup, Order, OrderLine, OrderNote
from ...order.utils import (
- add_item_to_delivery_group, cancel_order, cancel_delivery_group,
+ add_variant_to_delivery_group, cancel_order, cancel_delivery_group,
change_order_line_quantity, merge_duplicated_lines)
from ...product.models import Product, ProductVariant, Stock
@@ -387,5 +387,5 @@ class AddDeliveryGroupItemForm(forms.Form):
def save(self):
variant = self.cleaned_data['variant']
quantity = self.cleaned_data['quantity']
- add_item_to_delivery_group(self.group, variant, quantity)
+ add_variant_to_delivery_group(self.group, variant, quantity)
Order.objects.recalculate_order(self.group.order)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 1924b38c2a..6b02847579 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -54,68 +54,40 @@ def attach_order_to_user(order, user):
order.save(update_fields=['user'])
-def create_order_lines_in_delivery_group(
- delivery_group, partition, discounts=None):
- for order_line in partition:
- product_variant = order_line.variant
- price = order_line.get_price_per_item(discounts)
- total_quantity = order_line.get_quantity()
-
- while total_quantity > 0:
- stock = product_variant.select_stockrecord()
- if not stock:
- raise InsufficientStock(product_variant)
- quantity = (
- stock.quantity_available
- if total_quantity > stock.quantity_available
- else total_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)
- total_quantity -= quantity
- # allocate quantity to avoid overselling
- Stock.objects.allocate_stock(stock, quantity)
- # refresh for reading quantity_available in next select_stockrecord
- stock.refresh_from_db()
-
-def add_items_to_delivery_group(delivery_group, partition, discounts=None):
- for item_line in partition:
- variant = item_line.variant
- price = item_line.get_price_per_item(discounts)
- quantity = item_line.get_quantity()
- create_item_in_delivery_group(delivery_group, variant, quantity, price)
-
-
-def add_item_to_delivery_group(group, variant, quantity):
- items = group.items.filter(
- product=variant.product, product_sku=variant.sku, stock__isnull=False,
- stock__quantity__gte=F('stock__quantity_allocated') + quantity)
- if items.exists():
- item = items.first()
- item.quantity += quantity
- item.save()
- Stock.objects.allocate_stock(item.stock, quantity)
- else:
- price = variant.get_price_per_item()
- create_item_in_delivery_group(group, variant, quantity, price)
+def fill_group_with_partition(group, partition, discounts=None):
+ """
+ Fills delivery 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 create_item_in_delivery_group(group, variant, total_quantity, price):
- while total_quantity > 0:
+def add_variant_to_delivery_group(
+ group, variant, total_quantity, discounts=None, add_to_existing=True):
+ """
+ Adds total_quantity of variant to group.
+ Raises InsufficientStock exception if quantity could not be fulfilled.
+
+ By default, first adds variant to existing lines with same variant.
+ It can be disabled with setting add_to_existing to False.
+
+ 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)
+ 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 total_quantity > stock.quantity_available
- else total_quantity
+ if quantity_left > stock.quantity_available
+ else quantity_left
)
item = group.items.create(
product=variant.product,
@@ -126,11 +98,39 @@ def create_item_in_delivery_group(group, variant, total_quantity, price):
unit_price_gross=price.gross,
stock=stock,
stock_location=stock.location.name)
- # allocate quantity to avoid overselling
Stock.objects.allocate_stock(stock, quantity)
- # refresh stock to get quantity_allocated in next loop
- stock.refresh_from_db()
- total_quantity -= quantity
+ stock.refresh_from_db() # refresh stock for accessing quantity_allocated
+ quantity_left -= quantity
+
+
+def add_variant_to_existing_lines(group, variant, total_quantity):
+ """
+ Adds variant to existing lines with same variant.
+
+ Variant is added by increasing quantity of lines with same variant,
+ as long as total_quantity of variant will be added
+ or there is no more lines with same variant.
+
+ Returns quantity that could not be fulfilled with existing lines.
+ """
+ items = group.items.filter(
+ product=variant.product, product_sku=variant.sku,
+ stock__isnull=False).order_by(
+ F('stock__quantity_allocated') - F('stock__quantity'))
+
+ quantity_left = total_quantity
+ for item in items:
+ quantity_added = (
+ item.stock.quantity_available
+ if quantity_left > item.stock.quantity_available
+ else quantity_left)
+ item.quantity += quantity_added
+ item.save()
+ Stock.objects.allocate_stock(item.stock, quantity_added)
+ quantity_left -= quantity_added
+ if quantity_left == 0:
+ break
+ return quantity_left
def cancel_delivery_group(group, cancel_order=True):
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index ee030a5704..7e1f777839 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -9,7 +9,7 @@ from saleor.dashboard.order.forms import ChangeQuantityForm, MoveLinesForm
from saleor.order.models import (
DeliveryGroup, Order, OrderLine, OrderHistoryEntry)
from saleor.order.utils import (
- create_order_lines_in_delivery_group, change_order_line_quantity)
+ fill_group_with_partition, change_order_line_quantity)
from saleor.product.models import ProductVariant, Stock, StockLocation
from tests.utils import get_redirect_location, get_url_path
@@ -127,7 +127,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)
- create_order_lines_in_delivery_group(group, cart.lines.all())
+ fill_group_with_partition(group, cart.lines.all())
order_line = group.items.get()
# Check max quantity validation
diff --git a/tests/test_order.py b/tests/test_order.py
index d33e8572de..909099bb9c 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -3,7 +3,7 @@ from prices import Price
from saleor.cart.models import Cart
from saleor.order import models
from saleor.order.utils import (
- add_item_to_delivery_group, create_order_lines_in_delivery_group)
+ add_variant_to_delivery_group, fill_group_with_partition)
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)
- create_order_lines_in_delivery_group(delivery_group, cart.lines.all())
+ fill_group_with_partition(delivery_group, cart.lines.all())
order_line = delivery_group.items.get()
stock = order_line.stock
assert stock.quantity_allocated == 2
@@ -42,19 +42,19 @@ 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)
- create_order_lines_in_delivery_group(
+ fill_group_with_partition(
group, cart.lines.all(), discounts=cart.discounts)
item = group.items.first()
assert item.get_price_per_item() == Price(currency="USD", net=5)
-def test_add_item_to_delivery_group_adds_item_for_new_variant(
+def test_add_variant_to_delivery_group_adds_item_for_new_variant(
order_with_items, product_in_stock):
group = order_with_items.groups.get()
variant = product_in_stock.variants.get()
items_before = group.items.count()
- add_item_to_delivery_group(group, variant, 1)
+ add_variant_to_delivery_group(group, variant, 1)
item = group.items.last()
assert group.items.count() == items_before + 1
@@ -62,20 +62,20 @@ def test_add_item_to_delivery_group_adds_item_for_new_variant(
assert item.quantity == 1
-def test_add_item_to_delivery_group_allocates_stock_for_new_variant(
+def test_add_variant_to_delivery_group_allocates_stock_for_new_variant(
order_with_items, product_in_stock):
group = order_with_items.groups.get()
variant = product_in_stock.variants.get()
stock = variant.select_stockrecord(quantity=1)
stock_before = stock.quantity_allocated
- add_item_to_delivery_group(group, variant, 1)
+ add_variant_to_delivery_group(group, variant, 1)
stock.refresh_from_db()
assert stock.quantity_allocated == stock_before + 1
-def test_add_item_to_delivery_group_edits_item_for_existing_variant(
+def test_add_variant_to_delivery_group_edits_item_for_existing_variant(
order_with_items_and_stock):
order = order_with_items_and_stock
group = order.groups.get()
@@ -84,7 +84,7 @@ def test_add_item_to_delivery_group_edits_item_for_existing_variant(
items_before = group.items.count()
item_quantity_before = existing_item.quantity
- add_item_to_delivery_group(group, variant, 1)
+ add_variant_to_delivery_group(group, variant, 1)
existing_item.refresh_from_db()
assert group.items.count() == items_before
@@ -92,7 +92,7 @@ def test_add_item_to_delivery_group_edits_item_for_existing_variant(
assert existing_item.quantity == item_quantity_before + 1
-def test_add_item_to_delivery_group_allocates_stock_for_existing_variant(
+def test_add_variant_to_delivery_group_allocates_stock_for_existing_variant(
order_with_items_and_stock):
order = order_with_items_and_stock
group = order.groups.get()
@@ -101,7 +101,7 @@ def test_add_item_to_delivery_group_allocates_stock_for_existing_variant(
stock = existing_item.stock
stock_before = stock.quantity_allocated
- add_item_to_delivery_group(group, variant, 1)
+ add_variant_to_delivery_group(group, variant, 1)
stock.refresh_from_db()
assert stock.quantity_allocated == stock_before + 1