Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Remove code duplication and avoid circular imports
Anna Siwik42c0d667597f
diff --git a/saleor/order/transitions.py b/saleor/order/transitions.pyindex ed8dbe80d2..dea1d3eac8 100644--- a/saleor/order/transitions.py+++ b/saleor/order/transitions.py@@ -1,37 +1,14 @@-from satchless.item import InsufficientStock-from saleor.order import GroupStatus+from saleor.order.utils import add_variant_to_delivery_groupfrom saleor.product.models import Stockdef 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+ add_variant_to_delivery_group(+ group, item.variant, item.get_quantity(), discounts,+ add_to_existing=False)def cancel_delivery_group(group):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 58182da563..06fdceb373 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -9,13 +9,14 @@ from satchless.item import InsufficientStockfrom ..product.models import Stockfrom ..userprofile.utils import store_user_address-from .models import Order, OrderLinefrom . import GroupStatusdef check_order_status(func):"""Preserves execution of function if order is fully paid by redirectingto order's details page."""+ from .models import Order+@wraps(func)def decorator(*args, **kwargs):token = kwargs.pop('token')@@ -60,7 +61,7 @@ def attach_order_to_user(order, user):def add_variant_to_delivery_group(- group, variant, total_quantity, discounts=None):+ 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.@@ -70,8 +71,9 @@ 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)+ 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()@@ -175,6 +177,7 @@ def remove_empty_groups(line, force=False):def move_order_line_to_group(line, target_group, quantity):+ from .models import OrderLine"""Moves given quantity of order line to another shipment group."""try:target_line = target_group.lines.get(