Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Validate stock reservations only for lines provided in checkoutLinesUpdate (#10749)
SzymJ6711f2988fa1
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 9029ca3578..c14cd27e8c 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -237,13 +237,17 @@ def add_variants_to_checkout(if reservation_length and to_reserve:updated_lines_ids = [line.pk for line in to_reserve + to_delete]++ # Validation for stock reservation should be performed on new and updated lines.+ # For already existing lines only reserved_until should be updated.+ lines_to_update_reservation_time = []for line in checkout_lines:if line.pk not in updated_lines_ids:- to_reserve.append(line)- variants.append(line.variant)+ lines_to_update_reservation_time.append(line)reserve_stocks_and_preorders(to_reserve,+ lines_to_update_reservation_time,variants,country_code,channel,diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.pyindex e8b3271914..a000d12447 100644--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py@@ -596,7 +596,6 @@ def test_update_checkout_lines(},],}-response = get_graphql_content(api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables))@@ -658,7 +657,7 @@ def test_update_checkout_lines_with_reservations(reservation_length=5,)- with django_assert_num_queries(53):+ with django_assert_num_queries(54):variant_id = graphene.Node.to_global_id("ProductVariant", variants[0].pk)variables = {"id": to_global_id_or_none(checkout),@@ -672,7 +671,7 @@ def test_update_checkout_lines_with_reservations(assert not data["errors"]# Updating multiple lines in checkout has same query count as updating one- with django_assert_num_queries(53):+ with django_assert_num_queries(54):variables = {"id": to_global_id_or_none(checkout),"lines": [],@@ -915,7 +914,7 @@ def test_add_checkout_lines_with_reservations(new_lines.append({"quantity": 2, "variantId": variant_id})# Adding multiple lines to checkout has same query count as adding one- with django_assert_num_queries(52):+ with django_assert_num_queries(53):variables = {"id": Node.to_global_id("Checkout", checkout.pk),"lines": [new_lines[0]],@@ -928,7 +927,7 @@ def test_add_checkout_lines_with_reservations(checkout.lines.exclude(id=line.id).delete()- with django_assert_num_queries(52):+ with django_assert_num_queries(53):variables = {"id": Node.to_global_id("Checkout", checkout.pk),"lines": new_lines,diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.pyindex 4b84f88e42..694c008d53 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py@@ -361,6 +361,7 @@ def test_checkout_lines_add_new_variant_updates_other_lines_reservations_expiratcheckout = checkout_line_with_one_reservation.checkoutline = checkout_line_with_one_reservationreservation = line.reservations.get()+lines, _ = fetch_checkout_lines(checkout)assert calculate_checkout_quantity(lines) == 2@@ -384,6 +385,8 @@ def test_checkout_lines_add_new_variant_updates_other_lines_reservations_expiratresponse = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)content = get_graphql_content(response)data = content["data"]["checkoutLinesAdd"]+ reservation.refresh_from_db()+assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)@@ -395,20 +398,10 @@ def test_checkout_lines_add_new_variant_updates_other_lines_reservations_expiratassert new_line.quantity == 3assert calculate_checkout_quantity(lines) == 5- updated_reservation = Reservation.objects.get(checkout_line__variant=variant)- assert updated_reservation.checkout_line == line- assert updated_reservation.quantity_reserved == line.quantity- assert updated_reservation.reserved_until > reservation.reserved_until-other_reservation = Reservation.objects.get(checkout_line__variant=variant_other)assert other_reservation.checkout_line == new_lineassert other_reservation.quantity_reserved == new_line.quantity- assert other_reservation.reserved_until > reservation.reserved_until-- assert updated_reservation.reserved_until == other_reservation.reserved_until-- with pytest.raises(Reservation.DoesNotExist):- reservation.refresh_from_db()+ assert reservation.reserved_until == other_reservation.reserved_untildef test_checkout_lines_add_existing_variant(user_api_client, checkout_with_item):diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.pyindex 7ade758f66..fbd749a559 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py@@ -533,6 +533,8 @@ def test_checkout_lines_update_other_lines_reservations_expirations(response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)content = get_graphql_content(response)data = content["data"]["checkoutLinesUpdate"]+ reservation.refresh_from_db()+assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)@@ -544,19 +546,11 @@ def test_checkout_lines_update_other_lines_reservations_expirations(assert new_line.quantity == 3assert calculate_checkout_quantity(lines) == 5- updated_reservation = Reservation.objects.get(checkout_line__variant=variant)- assert updated_reservation.checkout_line == line- assert updated_reservation.quantity_reserved == line.quantity- assert updated_reservation.reserved_until > reservation.reserved_until-other_reservation = Reservation.objects.get(checkout_line__variant=variant_other)assert other_reservation.checkout_line == new_lineassert other_reservation.quantity_reserved == new_line.quantity- assert updated_reservation.reserved_until == other_reservation.reserved_until-- with pytest.raises(Reservation.DoesNotExist):- reservation.refresh_from_db()+ assert reservation.reserved_until == other_reservation.reserved_untildef test_checkout_lines_update_quantity_and_custom_price(diff --git a/saleor/warehouse/reservations.py b/saleor/warehouse/reservations.pyindex 8a35f119d7..af7e5cc8b7 100644--- a/saleor/warehouse/reservations.py+++ b/saleor/warehouse/reservations.py@@ -22,6 +22,7 @@ StockData = namedtuple("StockData", ["pk", "quantity"])@traced_atomic_transaction()def reserve_stocks_and_preorders(checkout_lines: Iterable["CheckoutLine"],+ lines_to_update_reservation_time: Iterable["CheckoutLine"],variants: Iterable["ProductVariant"],country_code: str,channel: "Channel",@@ -44,33 +45,47 @@ def reserve_stocks_and_preorders(else:stock_lines.append(line)+ reserved_until = timezone.now() + timedelta(minutes=length_in_minutes)+if stock_lines:reserve_stocks(stock_lines,stock_variants,country_code,channel,- length_in_minutes,+ reserved_until,replace=replace,)+ # Refresh reserved_until for already existing lines+ if lines_to_update_reservation_time:+ Reservation.objects.filter(+ checkout_line__in=lines_to_update_reservation_time+ ).update(reserved_until=reserved_until)+if preorder_lines:reserve_preorders(preorder_lines,preorder_variants,country_code,channel.slug,- length_in_minutes,+ reserved_until,replace=replace,)+ # Refresh reserved_until for already existing lines+ if lines_to_update_reservation_time:+ PreorderReservation.objects.filter(+ checkout_line__in=lines_to_update_reservation_time+ ).update(reserved_until=reserved_until)+def reserve_stocks(checkout_lines: Iterable["CheckoutLine"],variants: Iterable["ProductVariant"],country_code: str,channel: "Channel",- length_in_minutes: int,+ reserved_until: datetime,*,replace: bool = True,):@@ -85,8 +100,6 @@ def reserve_stocks(if not checkout_lines:return- reserved_until = timezone.now() + timedelta(minutes=length_in_minutes)-stocks = list(Stock.objects.select_for_update(of=("self",)).get_variants_stocks_for_country(country_code, channel.slug, variants)@@ -222,7 +235,7 @@ def reserve_preorders(variants: Iterable["ProductVariant"],country_code: str,channel_slug: str,- length_in_minutes: int,+ reserved_until: datetime,*,replace: bool = True,):@@ -231,8 +244,6 @@ def reserve_preorders(variants = [variant for variant in variants if variant.pk in variants_ids]variants_map = {variant.id: variant for variant in variants}- reserved_until = timezone.now() + timedelta(minutes=length_in_minutes)-all_variants_channel_listings = (ProductVariantChannelListing.objects.filter(variant__in=variants).annotate_preorder_quantity_allocated()diff --git a/saleor/warehouse/tests/test_preorder_reservations_management.py b/saleor/warehouse/tests/test_preorder_reservations_management.pyindex f685cac667..cd6e49abfb 100644--- a/saleor/warehouse/tests/test_preorder_reservations_management.py+++ b/saleor/warehouse/tests/test_preorder_reservations_management.py@@ -24,7 +24,7 @@ def test_reserve_preorders(checkout_line_with_preorder_item, channel_USD):[checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)reservation = PreorderReservation.objects.get(checkout_line=checkout_line)@@ -43,7 +43,7 @@ def test_preorder_reservation_skips_prev_reservation_delete_if_replace_is_disabl[checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),replace=False,)@@ -53,7 +53,7 @@ def test_preorder_reservation_skips_prev_reservation_delete_if_replace_is_disabl[checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -76,7 +76,7 @@ def test_preorder_reservation_removes_previous_reservations_for_checkout([checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)with pytest.raises(PreorderReservation.DoesNotExist):@@ -100,7 +100,7 @@ def test_preorder_reservation_fails_if_there_is_not_enough_channel_threshold_ava[checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -132,7 +132,7 @@ def test_preorder_reservation_fails_if_channel_threshold_was_allocated([checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -169,7 +169,7 @@ def test_preorder_reservation_fails_if_channel_threshold_was_reserved([checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -209,7 +209,7 @@ def test_preorder_reservation_fails_if_global_threshold_was_allocated([checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -255,7 +255,7 @@ def test_preorder_reservation_fails_if_global_threshold_was_reserved([checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -275,5 +275,5 @@ def test_preorder_reservation_fails_if_there_is_not_enough_global_threshold_avai[checkout_line.variant],COUNTRY_CODE,channel_USD.slug,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)diff --git a/saleor/warehouse/tests/test_stock_reservations_management.py b/saleor/warehouse/tests/test_stock_reservations_management.pyindex a4ec00f146..4e62221801 100644--- a/saleor/warehouse/tests/test_stock_reservations_management.py+++ b/saleor/warehouse/tests/test_stock_reservations_management.py@@ -26,7 +26,7 @@ def test_reserve_stocks(checkout_line, channel_USD):[checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)stock.refresh_from_db()@@ -45,7 +45,7 @@ def test_stocks_reservation_skips_prev_reservation_delete_if_replace_is_disabled[checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),replace=False,)@@ -55,7 +55,7 @@ def test_stocks_reservation_skips_prev_reservation_delete_if_replace_is_disabled[checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -88,7 +88,7 @@ def test_multiple_stocks_reserved_if_single_stock_is_not_enough_highest_stock_st[checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)stock.refresh_from_db()@@ -155,7 +155,7 @@ def test_multiple_stocks_reserved_if_single_stock_is_not_enough_sorting_order_st[checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)# then@@ -195,7 +195,7 @@ def test_stocks_reservation_removes_previous_reservations_for_checkout([checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)with pytest.raises(Reservation.DoesNotExist):@@ -218,7 +218,7 @@ def test_stock_reservation_fails_if_there_is_not_enough_stock_available([checkout_line.variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -255,7 +255,7 @@ def test_stock_reservation_accounts_for_order_allocations([variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)@@ -292,5 +292,5 @@ def test_stock_reservation_accounts_for_order_allocations_and_reservations([variant],COUNTRY_CODE,channel_USD,- RESERVATION_LENGTH,+ timezone.now() + timedelta(minutes=RESERVATION_LENGTH),)