Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Abstract ProductFilter class out

akjanikcf664174af56
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index b82efed4c6..ae90741b8c 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -25,7 +25,6 @@ class ProductFilter(SortedFilterSet):
filter_overrides = {PriceField: {'filter_class': RangeFilter}}
def __init__(self, *args, **kwargs):
- self.category = kwargs.pop('category', None)
super().__init__(*args, **kwargs)
self.product_attributes, self.variant_attributes = (
self._get_attributes())
@@ -35,17 +34,7 @@ class ProductFilter(SortedFilterSet):
self.form.fields['sort_by'].validators.append(self.validate_sort_by)
def _get_attributes(self):
- product_attributes = (
- ProductAttribute.objects.all()
- .prefetch_related('values')
- .filter(product_types__products__category=self.category)
- .distinct())
- variant_attributes = (
- ProductAttribute.objects.all()
- .prefetch_related('values')
- .filter(product_variant_types__products__category=self.category)
- .distinct())
- return product_attributes, variant_attributes
+ raise NotImplementedError()
def _get_product_attributes_filters(self):
filters = {}
@@ -79,10 +68,29 @@ class ProductFilter(SortedFilterSet):
params={'value': value})
+class ProductCategoryFilter(ProductFilter):
+ def __init__(self, *args, **kwargs):
+ self.category = kwargs.pop('category')
+ super().__init__(*args, **kwargs)
+
+ def _get_attributes(self):
+ product_attributes = (
+ ProductAttribute.objects.all()
+ .prefetch_related('values')
+ .filter(product_types__products__category=self.category)
+ .distinct())
+ variant_attributes = (
+ ProductAttribute.objects.all()
+ .prefetch_related('values')
+ .filter(product_variant_types__products__category=self.category)
+ .distinct())
+ return product_attributes, variant_attributes
+
+
class ProductCollectionFilter(ProductFilter):
def __init__(self, *args, **kwargs):
- self.collection = kwargs.pop('collection', None)
+ self.collection = kwargs.pop('collection')
super().__init__(*args, **kwargs)
def _get_attributes(self):
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 3f2f99a061..4e129cf146 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -10,7 +10,7 @@ from django.urls import reverse
from ..cart.utils import set_cart_cookie
from ..core.utils import get_paginator_items, serialize_decimal
from ..core.utils.filters import get_now_sorted_by
-from .filters import ProductFilter, ProductCollectionFilter, SORT_BY_FIELDS
+from .filters import ProductCategoryFilter, ProductCollectionFilter, SORT_BY_FIELDS
from .models import Category, Collection
from .utils import (
get_availability, get_product_attributes_data, get_product_images,
@@ -116,7 +116,7 @@ def category_index(request, path, category_id):
category_id=category_id)
products = products_with_details(user=request.user).filter(
category__id=category.id).order_by('name')
- product_filter = ProductFilter(
+ product_filter = ProductCategoryFilter(
request.GET, queryset=products, category=category)
products_paginated = get_paginator_items(
product_filter.qs, settings.PAGINATE_BY, request.GET.get('page'))