Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor filters and view
michal2ad052310ab2
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex fd6a4b0ed3..1d8442894f 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -17,6 +17,26 @@ SORT_BY_FIELDS = (('price', 'price'),class ProductFilter(FilterSet):def __init__(self, *args, **kwargs):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.filters = OrderedDict(sorted(self.filters.items()))++ sort_by = OrderingFilter(+ label='Sort by',+ fields=SORT_BY_FIELDS+ )++ class Meta:+ model = Product+ fields = ['price']+ filter_overrides = {+ PriceField: {+ 'filter_class': RangeFilter+ }+ }++ def get_attributes(self):product_attributes = set()variant_attributes = set()for product in self.queryset:@@ -24,7 +44,9 @@ class ProductFilter(FilterSet):variant_attributes.add(attribute)for attribute in product.product_class.product_attributes.all():product_attributes.add(attribute)+ return product_attributes, variant_attributes+ def add_product_attributes_filters(self, product_attributes):for attribute in product_attributes:self.filters[attribute.slug] = MultipleChoiceFilter(name='attributes__%s' % attribute.pk,@@ -32,27 +54,13 @@ class ProductFilter(FilterSet):widget=CheckboxSelectMultiple,choices=get_attribute_choices(attribute))+ def add_product_variants_attributes_filters(self, variant_attributes):for attribute in variant_attributes:self.filters[attribute.slug] = MultipleChoiceFilter(name='variants__attributes__%s' % attribute.pk,label=attribute.name,widget=CheckboxSelectMultiple,choices=get_attribute_choices(attribute))- self.filters = OrderedDict(sorted(self.filters.items()))-- sort_by = OrderingFilter(- label='Sort by',- fields=SORT_BY_FIELDS- )-- class Meta:- model = Product- fields = ['price']- filter_overrides = {- PriceField: {- 'filter_class': RangeFilter- }- }def get_attribute_choices(attribute):diff --git a/saleor/product/views.py b/saleor/product/views.pyindex eb20a0f586..f5e4cfe66a 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -117,14 +117,14 @@ def category_index(request, path, category_id):products = products_with_details(user=request.user).filter(categories__name=category)product_filter = ProductFilter(request.GET, queryset=products)- products = [+ products_with_availability = [(product, get_availability(product, request.discounts, request.currency))for product in product_filter.qs]if actual_path != path:return redirect('product:category', permanent=True, path=actual_path,category_id=category_id)- products = get_paginator_items(- products, PAGINATE_BY, request.GET.get('page'))- context = {'category': category, 'products': products,+ products_paginated = get_paginator_items(+ products_with_availability, PAGINATE_BY, request.GET.get('page'))+ context = {'category': category, 'products': products_paginated,'filter': product_filter, 'sort_by_choices': SORT_BY_FIELDS}return TemplateResponse(request, 'category/index.html', context)diff --git a/tests/test_product.py b/tests/test_product.pyindex fc515ff28e..e915f832d3 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -371,8 +371,8 @@ def test_variant_availability_status(unavailable_product):assert status == VariantAvailabilityStatus.AVAILABLE-def test_product_filter_empty_get(authorized_client, product_in_stock,- default_category):+def test_product_filter_before_filtering(+ authorized_client, product_in_stock, default_category):products = models.Product.objects.all()url = reverse('product:category', kwargs={'path': default_category.slug,@@ -392,8 +392,8 @@ def test_product_filter_product_exists(authorized_client, product_in_stock,assert list(response.context['filter'].qs) == list(products)-def test_product_filter_product_not_exists(authorized_client, product_in_stock,- default_category):+def test_product_filter_product_does_not_exists(+ authorized_client, product_in_stock, default_category):url = reverse('product:category', kwargs={'path': default_category.slug,'category_id': default_category.pk})@@ -408,23 +408,7 @@ def test_product_filter_form(authorized_client, product_in_stock,url = reverse('product:category', kwargs={'path': default_category.slug,'category_id': default_category.pk})- data = {'price_0': [''], 'price_1': ['20']}- response = authorized_client.get(url, data)+ response = authorized_client.get(url)assert 'price' in response.context['filter'].form.fields.keys()assert 'sort_by' in response.context['filter'].form.fields.keys()assert list(response.context['filter'].qs) == list(products)---def test_product_filter_standard_form(authorized_client, product_in_stock,- default_category):- products = models.Product.objects.all()- url = reverse(- 'product:category', kwargs={'path': default_category.slug,- 'category_id': default_category.pk})- data = {'price_0': [''], 'price_1': ['20']}- response = authorized_client.get(url, data)- assert 'price' not in \- response.context['filter'].product_attributes_filter_form.fields.keys()- assert 'sort_by' not in \- response.context['filter'].product_attributes_filter_form.fields.keys()- assert list(response.context['filter'].qs) == list(products)