Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
changes after code review
Damian Wca55cc9505ac
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations.pyindex 50c38c57a6..2246aad8de 100644--- a/saleor/graphql/meta/mutations.py+++ b/saleor/graphql/meta/mutations.py@@ -116,9 +116,7 @@ class BaseMetadataMutation(BaseMutation):type_name, _ = graphene.Node.from_global_id(data["id"])if MODEL_EXTRA_METHODS.get(type_name):if type_name == product_models.Product.__name__:- data[- "qs"- ] = product_models.Product.objects.prefetched_product_for_webhook()+ data["qs"] = product_models.Product.objects.prefetched_for_webhook()instance = cls.get_instance(info, **data)MODEL_EXTRA_METHODS[type_name](instance, info, **data)diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex 480538d23f..50b46c652b 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -74,7 +74,7 @@ class CollectionBulkDelete(ModelBulkDeleteMutation):def bulk_action(cls, info, queryset):collections_ids = queryset.values_list("id", flat=True)products = list(- models.Product.objects.prefetched_product_for_webhook()+ models.Product.objects.prefetched_for_webhook(single_object=False).filter(collections__in=collections_ids).distinct())diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.pyindex ac1679a895..e4f6e412aa 100644--- a/saleor/graphql/product/mutations/attributes.py+++ b/saleor/graphql/product/mutations/attributes.py@@ -372,7 +372,7 @@ class ProductReorderAttributeValues(BaseReorderAttributeValuesMutation):)try:- product = models.Product.objects.prefetch_related("attributes").get(pk=pk)+ product = models.Product.objects.prefetched_for_webhook().get(pk=pk)except ObjectDoesNotExist:raise ValidationError({diff --git a/saleor/graphql/product/mutations/channels.py b/saleor/graphql/product/mutations/channels.pyindex 3caaa1033a..ecc130097b 100644--- a/saleor/graphql/product/mutations/channels.py+++ b/saleor/graphql/product/mutations/channels.py@@ -10,7 +10,9 @@ from ....core.permissions import ProductPermissionsfrom ....product.error_codes import CollectionErrorCode, ProductErrorCodefrom ....product.models import CollectionChannelListingfrom ....product.models import Product as ProductModel-from ....product.models import ProductChannelListing, ProductVariantChannelListing+from ....product.models import ProductChannelListing+from ....product.models import ProductVariant as ProductVariantModel+from ....product.models import ProductVariantChannelListingfrom ....product.tasks import update_product_discounted_price_taskfrom ...channel import ChannelContextfrom ...channel.mutations import BaseChannelListingMutation@@ -28,7 +30,6 @@ from ..types.products import Collection, Product, ProductVariantif TYPE_CHECKING:from ....channel.models import Channel as ChannelModelfrom ....product.models import Collection as CollectionModel- from ....product.models import ProductVariant as ProductVariantModelErrorType = DefaultDict[str, List[ValidationError]]@@ -175,7 +176,7 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):@classmethoddef perform_mutation(cls, _root, info, id, input):- qs = ProductModel.objects.prefetched_product_for_webhook()+ qs = ProductModel.objects.prefetched_for_webhook()product = cls.get_node_or_error(info, id, only_type=Product, field="id", qs=qs)errors = defaultdict(list)@@ -337,7 +338,7 @@ class ProductVariantChannelListingUpdate(BaseMutation):@classmethoddef perform_mutation(cls, _root, info, id, input):- qs = ProductVariantModel.objects.prefetch_variant_updated()+ qs = ProductVariantModel.objects.prefetched_for_webhook()variant: "ProductVariantModel" = cls.get_node_or_error( # type: ignoreinfo, id, only_type=ProductVariant, field="id", qs=qs)diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 78705e85ab..51181ca00d 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -230,7 +230,7 @@ class CollectionCreate(ModelMutation):@classmethoddef post_save_action(cls, info, instance, cleaned_input):- products = instance.products.prefetched_product_for_webhook().all()+ products = instance.products.prefetched_for_webhook(single_object=False)for product in products:info.context.plugins.product_updated(product)@@ -258,6 +258,7 @@ class CollectionUpdate(CollectionCreate):@classmethoddef post_save_action(cls, info, instance, cleaned_input):+ """Override this method with `pass` to avoid triggering product webhook."""pass@classmethod@@ -283,7 +284,7 @@ class CollectionDelete(ModelDeleteMutation):node_id = kwargs.get("id")instance = cls.get_node_or_error(info, node_id, only_type=Collection)- products = list(instance.products.prefetched_product_for_webhook().all())+ products = list(instance.products.prefetched_for_webhook(single_object=False))result = super().perform_mutation(_root, info, **kwargs)for product in products:@@ -406,7 +407,7 @@ class CollectionAddProducts(BaseMutation):products,"products",Product,- qs=models.Product.objects.prefetched_product_for_webhook(),+ qs=models.Product.objects.prefetched_for_webhook(single_object=False),)cls.clean_products(products)collection.products.add(*products)@@ -468,7 +469,7 @@ class CollectionRemoveProducts(BaseMutation):products,"products",only_type=Product,- qs=models.Product.objects.prefetched_product_for_webhook(),+ qs=models.Product.objects.prefetched_for_webhook(single_object=False),)collection.products.remove(*products)for product in products:@@ -635,9 +636,8 @@ class ProductCreate(ModelMutation):qs = cls.Meta.model.objects.prefetch_related("product_type__product_attributes__values","product_type__attributeproduct",- ).prefetched_product_for_webhook()+ )return cls.get_node_or_error(info, object_id, only_type="Product", qs=qs)-return super().get_instance(info, **data)@classmethod@@ -696,7 +696,6 @@ class ProductUpdate(ProductCreate):@classmethoddef post_save_action(cls, info, instance, cleaned_input):- # TODO: add prefetch relatedinfo.context.plugins.product_updated(instance)@@ -719,7 +718,6 @@ class ProductDelete(ModelDeleteMutation):@classmethoddef perform_mutation(cls, _root, info, **data):node_id = data.get("id")-instance = cls.get_node_or_error(info, node_id, only_type=Product)variants_id = list(instance.variants.all().values_list("id", flat=True))# get draft order lines for variant@@ -1424,7 +1422,7 @@ class ProductVariantSetDefault(BaseMutation):@classmethoddef perform_mutation(cls, _root, info, product_id, variant_id):- qs = models.Product.objects.prefetched_product_for_webhook()+ qs = models.Product.objects.prefetched_for_webhook()product = cls.get_node_or_error(info, product_id, field="product_id", only_type=Product, qs=qs)@@ -1480,7 +1478,7 @@ class ProductVariantReorder(BaseMutation):pk = from_global_id_strict_type(product_id, only_type=Product, field="id")try:- product = models.Product.objects.prefetched_product_for_webhook().get(pk=pk)+ product = models.Product.objects.prefetched_for_webhook().get(pk=pk)except ObjectDoesNotExist:raise ValidationError({@@ -1566,7 +1564,7 @@ class VariantMediaAssign(BaseMutation):media = cls.get_node_or_error(info, media_id, field="media_id", only_type=ProductMedia)- qs = models.ProductVariant.objects.prefetch_variant_updated()+ qs = models.ProductVariant.objects.prefetched_for_webhook()variant = cls.get_node_or_error(info, variant_id, field="variant_id", only_type=ProductVariant, qs=qs)@@ -1614,7 +1612,7 @@ class VariantMediaUnassign(BaseMutation):media = cls.get_node_or_error(info, media_id, field="image_id", only_type=ProductMedia)- qs = models.ProductVariant.objects.prefetch_variant_updated()+ qs = models.ProductVariant.objects.prefetched_for_webhook()variant = cls.get_node_or_error(info, variant_id, field="variant_id", only_type=ProductVariant, qs=qs)diff --git a/saleor/graphql/translations/mutations.py b/saleor/graphql/translations/mutations.pyindex bdc920dd97..2b7bcecbba 100644--- a/saleor/graphql/translations/mutations.py+++ b/saleor/graphql/translations/mutations.py@@ -146,7 +146,7 @@ class ProductVariantTranslate(BaseTranslateMutation):)variant_pk = from_global_id_strict_type(data["id"], ProductVariant, field="id")- variant = product_models.ProductVariant.objects.prefetch_variant_updated().get(+ variant = product_models.ProductVariant.objects.prefetched_for_webhook().get(pk=variant_pk)variant.translations.update_or_create(diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 0416669b07..176c6058fc 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -299,12 +299,16 @@ class ProductsQueryset(models.QuerySet):f"{ordering}name",)- def prefetched_product_for_webhook(self):+ def prefetched_for_webhook(self, single_object=True):+ if single_object:+ return self.prefetch_related(+ "attributes__values",+ "attributes__assignment__attribute",+ )return self.prefetch_related("attributes__values","attributes__assignment__attribute","collections",- "variants__stocks","variants__stocks__allocations","category",)@@ -417,7 +421,7 @@ class ProductVariantQueryset(models.QuerySet):),)- def prefetch_variant_updated(self):+ def prefetched_for_webhook(self):return self.prefetch_related("channel_listings","attributes__values",diff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.pyindex 9fb287abab..26fd35874c 100644--- a/saleor/product/utils/__init__.py+++ b/saleor/product/utils/__init__.py@@ -44,7 +44,7 @@ def delete_categories(categories_ids: List[str], manager):categories = Category.objects.select_for_update().filter(pk__in=categories_ids)categories.prefetch_related("products")- products = Product.objects.prefetched_product_for_webhook().none()+ products = Product.objects.none()for category in categories:products = products | collect_categories_tree_products(category)@@ -52,6 +52,7 @@ def delete_categories(categories_ids: List[str], manager):is_published=False, publication_date=None)products = list(products)+categories.delete()product_ids = [product.id for product in products]for product in products:@@ -62,7 +63,7 @@ def delete_categories(categories_ids: List[str], manager):def collect_categories_tree_products(category: "Category") -> "QuerySet[Product]":"""Collect products from all levels in category tree."""- products = category.products.all()+ products = category.products.prefetched_for_webhook(single_object=False)descendants = category.get_descendants()for descendant in descendants:products = products | descendant.products.all()