Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix performance
michalde3ec9bda384
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex 8ecb3e2a22..98568c2361 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -8,7 +8,7 @@ from django.utils.translation import pgettext_lazyfrom django_prices.models import PriceField-from .models import Product+from .models import Product, ProductAttribute, AttributeChoiceValueSORT_BY_FIELDS = (('price', pgettext_lazy('Sort by filter', 'price')),('name', pgettext_lazy('Sort by filter', 'name')))@@ -16,10 +16,12 @@ SORT_BY_FIELDS = (('price', pgettext_lazy('Sort by filter', 'price')),class ProductFilter(FilterSet):def __init__(self, *args, **kwargs):+ self.category = kwargs.pop('category')super(ProductFilter, self).__init__(*args, **kwargs)- product_attributes, variant_attributes = self._get_attributes()- self._add_product_attributes_filters(product_attributes)- self._add_product_variants_attributes_filters(variant_attributes)+ self.product_attributes, self.variant_attributes = \+ self._get_attributes(self.category)+ self._add_product_attributes_filters()+ self._add_product_variants_attributes_filters()self.filters = OrderedDict(sorted(self.filters.items()))sort_by = OrderingFilter(@@ -36,32 +38,34 @@ class ProductFilter(FilterSet):}}- def _get_attributes(self):- product_attributes = set()- variant_attributes = set()- for product in self.queryset:- for attribute in product.product_class.variant_attributes.all():- variant_attributes.add(attribute)- for attribute in product.product_class.product_attributes.all():- product_attributes.add(attribute)- return product_attributes, variant_attributes+ def _get_attributes(self, category):+ product_attributes = \+ (ProductAttribute.objects+ .prefetch_related('values')+ .filter(products_class__products__categories=category)+ .distinct())+ variant_attributes = \+ (ProductAttribute.objects+ .prefetch_related('values')+ .filter(product_variants_class__products__categories=category)+ .distinct())+ return list(product_attributes), list(variant_attributes)- def _add_product_attributes_filters(self, product_attributes):- for attribute in product_attributes:+ def _add_product_attributes_filters(self):+ for attribute in self.product_attributes:self.filters[attribute.slug] = MultipleChoiceFilter(name='attributes__%s' % attribute.pk,label=attribute.name,widget=CheckboxSelectMultiple,- choices=get_attribute_choices(attribute))+ choices=self._get_attribute_choices(attribute))- def _add_product_variants_attributes_filters(self, variant_attributes):- for attribute in variant_attributes:+ def _add_product_variants_attributes_filters(self):+ for attribute in self.variant_attributes:self.filters[attribute.slug] = MultipleChoiceFilter(name='variants__attributes__%s' % attribute.pk,label=attribute.name,widget=CheckboxSelectMultiple,- choices=get_attribute_choices(attribute))+ choices=self._get_attribute_choices(attribute))--def get_attribute_choices(attribute):- return [(choice.pk, choice.name) for choice in attribute.values.all()]+ def _get_attribute_choices(self, attribute):+ return [(choice.pk, choice.name) for choice in attribute.values.all()]diff --git a/saleor/product/views.py b/saleor/product/views.pyindex 1333a23f8b..4013ffcf69 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -12,7 +12,7 @@ from ..cart.utils import set_cart_cookiefrom ..core.utils import get_paginator_items, serialize_decimalfrom ..settings import PAGINATE_BYfrom .filters import SORT_BY_FIELDS, ProductFilter-from .models import Category+from .models import Category, ProductAttribute, AttributeChoiceValuefrom .utils import (products_with_details, products_for_cart,handle_cart_form, get_availability,get_product_images, get_variant_picker_data,@@ -120,7 +120,8 @@ def category_index(request, path, category_id):category_id=category_id)products = products_with_details(user=request.user).filter(categories__name=category)- product_filter = ProductFilter(request.GET, queryset=products)+ product_filter = ProductFilter(+ request.GET, queryset=products, category=category)products_paginated = get_paginator_items(product_filter.qs, PAGINATE_BY, request.GET.get('page'))products_and_availability = list(products_with_availability(