Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix CollectionsByProductIdLoader if collection is missing (#16423)
Artur268793c7477d
diff --git a/saleor/discount/utils/voucher.py b/saleor/discount/utils/voucher.pyindex 80e14706dd..3cce524684 100644--- a/saleor/discount/utils/voucher.py+++ b/saleor/discount/utils/voucher.py@@ -208,7 +208,7 @@ def get_discounted_lines(continueline_category = line_product.categoryline_collections = set(- [collection.pk for collection in line_info.collections]+ [collection.pk for collection in line_info.collections if collection])if line_info.variant and (line_variant.pk in voucher_info.variant_pksdiff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.pyindex 8905b1f3e5..81a55c3926 100644--- a/saleor/graphql/checkout/dataloaders.py+++ b/saleor/graphql/checkout/dataloaders.py@@ -137,7 +137,11 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader[str, list[CheckoutLineInproduct_type=product_types_map[line.variant_id],collections=sorted(collections_map[line.variant_id],- key=lambda collection: collection.slug,+ key=(+ lambda collection: collection.slug+ if collection+ else ""+ ),),discounts=checkout_lines_discounts[line.id],tax_class=tax_class_map[line.variant_id],diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.pyindex 497ea79b48..ec44e7c982 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py@@ -3,6 +3,7 @@ from decimal import Decimalfrom unittest import mockfrom unittest.mock import call, patch+import before_afterimport grapheneimport pytestfrom django.test import override_settings@@ -17,7 +18,7 @@ from .....checkout.utils import add_variant_to_checkout, set_external_shipping_ifrom .....core.models import EventDeliveryfrom .....discount import VoucherTypefrom .....plugins.manager import get_plugins_manager-from .....product.models import ProductVariantChannelListing+from .....product.models import Collection, ProductVariantChannelListingfrom .....warehouse.models import Stockfrom .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncTypefrom ....core.utils import to_global_id_or_none@@ -506,6 +507,42 @@ def test_checkout_add_collection_voucher_code_checkout_with_sale(assert checkout.discount_amount == voucher_discount+def test_checkout_add_collection_voucher_code_checkout_with_sale_collection_deleted(+ api_client, checkout_with_item_on_sale, voucher_percentage, collection+):+ # given+ checkout = checkout_with_item_on_sale++ voucher = voucher_percentage+ product = checkout.lines.first().variant.product+ product.collections.add(collection)+ voucher.type = VoucherType.SPECIFIC_PRODUCT+ voucher.save()+ voucher.collections.add(collection)+ checkout.price_expiration = timezone.now()+ lines, _ = fetch_checkout_lines(checkout)+ variables = {+ "id": to_global_id_or_none(checkout),+ "promoCode": voucher.code,+ }+ assert Collection.objects.count() == 1++ # when+ def delete_collections(*args, **kwargs):+ Collection.objects.all().delete()++ with before_after.after(+ "saleor.graphql.product.dataloaders.products.CollectionsByProductIdLoader"+ ".batch_load",+ delete_collections,+ ):+ data = _mutate_checkout_add_promo_code(api_client, variables)++ # then+ assert not data["errors"]+ assert Collection.objects.count() == 0++def test_checkout_add_category_code_checkout_with_sale(api_client, checkout_with_item_on_sale, voucher_percentage, channel_USD):diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.pyindex fd085b8fee..2aced0e8c4 100644--- a/saleor/graphql/product/dataloaders/products.py+++ b/saleor/graphql/product/dataloaders/products.py@@ -524,10 +524,8 @@ class CollectionByIdLoader(DataLoader):context_key = "collection_by_id"def batch_load(self, keys):- collections = (- Collection.objects.using(self.database_connection_name)- .using(self.database_connection_name)- .in_bulk(keys)+ collections = Collection.objects.using(self.database_connection_name).in_bulk(+ keys)return [collections.get(collection_id) for collection_id in keys]@@ -538,7 +536,6 @@ class CollectionsByProductIdLoader(DataLoader):def batch_load(self, keys):product_collection_pairs = list(CollectionProduct.objects.using(self.database_connection_name)- .using(self.database_connection_name).filter(product_id__in=keys).order_by("id").values_list("product_id", "collection_id")@@ -549,9 +546,10 @@ class CollectionsByProductIdLoader(DataLoader):product_collection_map[pid].append(cid)def map_collections(collections):- collection_map = {c.id: c for c in collections}+ collection_map = {c.id: c for c in collections if c is not None}+return [- [collection_map[cid] for cid in product_collection_map[pid]]+ [collection_map.get(cid) for cid in product_collection_map[pid]]for pid in keys]