Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor code after merge
Damian W62f81449528a
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 0b8a737b7a..cd0d8c5f22 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -2675,15 +2675,17 @@ ORDER_LINES_CREATE_MUTATION = """@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))def test_order_lines_create(- status, order_with_lines, permission_manage_orders, staff_api_client+ status,+ order_with_lines,+ permission_manage_orders,+ staff_api_client,+ variant_with_many_stocks,):query = ORDER_LINES_CREATE_MUTATIONorder = order_with_linesorder.status = statusorder.save(update_fields=["status"])- line = order.lines.first()- variant = line.variant- old_quantity = line.quantity+ variant = variant_with_many_stocksquantity = 1order_id = graphene.Node.to_global_id("Order", order.id)variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)@@ -2702,7 +2704,7 @@ def test_order_lines_create(content = get_graphql_content(response)data = content["data"]["orderLinesCreate"]assert data["orderLines"][0]["productSku"] == variant.sku- assert data["orderLines"][0]["quantity"] == old_quantity + quantity+ assert data["orderLines"][0]["quantity"] == quantity# mutation should fail when quantity is lower than 1variables = {"orderId": order_id, "variantId": variant_id, "quantity": 0}@@ -2714,6 +2716,41 @@ def test_order_lines_create(assert data["orderErrors"][0]["variants"] == [variant_id][email protected]("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))+def test_order_lines_create_with_existing_variant(+ status,+ order_with_lines,+ permission_manage_orders,+ staff_api_client,+):+ query = ORDER_LINES_CREATE_MUTATION+ order = order_with_lines+ order.status = status+ order.save(update_fields=["status"])+ line = order.lines.first()+ variant = line.variant+ old_quantity = line.quantity+ quantity = 1+ order_id = graphene.Node.to_global_id("Order", order.id)+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)+ variables = {"orderId": order_id, "variantId": variant_id, "quantity": quantity}++ # mutation should fail without proper permissions+ response = staff_api_client.post_graphql(query, variables)+ assert_no_permission(response)+ assert not OrderEvent.objects.exists()++ # assign permissions+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(query, variables)+ assert OrderEvent.objects.count() == 1+ assert OrderEvent.objects.last().type == order_events.OrderEvents.ADDED_PRODUCTS+ content = get_graphql_content(response)+ data = content["data"]["orderLinesCreate"]+ assert data["orderLines"][0]["productSku"] == variant.sku+ assert data["orderLines"][0]["quantity"] == old_quantity + quantity++@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))def test_order_lines_create_with_product_and_variant_not_assigned_to_channel(status, order_with_lines, permission_manage_orders, staff_api_client, variantdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex e530c17034..c5b9850fcf 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -307,7 +307,10 @@ def add_variant_to_order(line = order.lines.get(variant=variant)old_quantity = line.quantitynew_quantity = old_quantity + quantity- change_order_line_quantity(user, line, old_quantity, new_quantity)+ line_info = OrderLineData(line=line, quantity=old_quantity)+ change_order_line_quantity(+ user, line_info, old_quantity, new_quantity, send_event=False+ )except OrderLine.DoesNotExist:product = variant.productcollections = product.collections.all()@@ -406,7 +409,9 @@ def _update_allocations_for_line(decrease_allocations([line_info])-def change_order_line_quantity(user, line_info, old_quantity: int, new_quantity: int):+def change_order_line_quantity(+ user, line_info, old_quantity: int, new_quantity: int, send_event=True+):"""Change the quantity of ordered items in a order line."""line = line_info.lineif new_quantity:@@ -431,7 +436,11 @@ def change_order_line_quantity(user, line_info, old_quantity: int, new_quantity:quantity_diff = old_quantity - new_quantity- # Create the removal event+ if send_event:+ create_order_event(line, user, quantity_diff)+++def create_order_event(line, user, quantity_diff):if quantity_diff > 0:events.order_removed_products_event(order=line.order, user=user, order_lines=[(quantity_diff, line)]