Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add prefetches
Pacu2230bd02fab87
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex b2038494ad..b84f836cde 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -27,6 +27,7 @@ class CartQueryset(models.QuerySet):"""return self.prefetch_related('lines__variant__product__category',+ 'lines__variant__product__collections','lines__variant__product__images','lines__variant__product__product_type__product_attributes__values') # noqadiff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.pyindex ad7135d9fd..6e4b5819f2 100644--- a/saleor/checkout/views/__init__.py+++ b/saleor/checkout/views/__init__.py@@ -111,6 +111,7 @@ def cart_index(request, cart):'variant__product__product_type','variant__product__category')lines = lines.prefetch_related(+ 'variant__product__collections','variant__product__images','variant__product__product_type__variant_attributes')for line in lines:diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex 4be76abdca..0c73df3f27 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -18,6 +18,13 @@ def decrease_voucher_usage(voucher):voucher.save(update_fields=['used'])+def are_product_collections_on_sale(product, sale):+ """Checks if any collection is on sale."""+ discounted_collections = set(sale.collections.all())+ product_collections = set(product.collections.all())+ return set(product_collections).intersection(discounted_collections)++def is_category_on_sale(category, sale):"""Check if category is descendant of one of categories on sale."""discounted_categories = set(sale.categories.all())@@ -31,7 +38,8 @@ def get_product_discount_on_sale(sale, product):discounted_products = {p.pk for p in sale.products.all()}is_product_on_sale = (product.pk in discounted_products or- is_category_on_sale(product.category, sale))+ is_category_on_sale(product.category, sale) or+ are_product_collections_on_sale(product, sale))if is_product_on_sale:return sale.get_discount()raise NotApplicable(diff --git a/saleor/product/models.py b/saleor/product/models.pyindex b366fbc986..e5bc1b6fa8 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -3,7 +3,7 @@ from decimal import Decimalfrom django.conf import settingsfrom django.contrib.postgres.fields import HStoreField-from django.core.validators import MinValueValidator, RegexValidator+from django.core.validators import MinValueValidatorfrom django.db import modelsfrom django.db.models import Qfrom django.urls import reversediff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.pyindex 91c52992fc..199a876fa6 100644--- a/saleor/product/utils/__init__.py+++ b/saleor/product/utils/__init__.py@@ -23,7 +23,7 @@ def products_visible_to_user(user):def products_with_details(user):products = products_visible_to_user(user)products = products.prefetch_related(- 'category', 'images', 'variants__variant_images__image',+ 'category', 'collections', 'images', 'variants__variant_images__image','attributes__values', 'product_type__product_attributes__values')return productsdiff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.pyindex c3b5bce64b..0d83a8e949 100644--- a/tests/dashboard/test_discounts.py+++ b/tests/dashboard/test_discounts.py@@ -4,9 +4,9 @@ from decimal import Decimalfrom unittest.mock import Mockimport pytest+from django.urls import reversefrom prices import Money, TaxedMoney-from saleor.dashboard.order.utils import get_voucher_discount_for_orderfrom saleor.discount import DiscountValueType, VoucherTypefrom saleor.discount.models import Sale, Voucher@@ -48,7 +48,7 @@ def test_voucher_shipping_add(admin_client):assert voucher.limit == Money('59.99', 'USD')-def test_view_sale_add(admin_client, default_category):+def test_view_sale_add(admin_client, default_category, collection):url = reverse('dashboard:sale-add')data = {'name': 'Free products',@@ -63,6 +63,7 @@ def test_view_sale_add(admin_client, default_category):sale = Sale.objects.first()assert sale.name == data['name']assert default_category in sale.categories.all()+ assert collection in sale.categories.all()def test_view_sale_add_requires_product_or_category(