Skip to content

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

Refactor filters

michal2c6e45f58153
diff --git a/saleor/core/templatetags/shop.py b/saleor/core/templatetags/shop.py
index f531870389..d73bdf280c 100644
--- a/saleor/core/templatetags/shop.py
+++ b/saleor/core/templatetags/shop.py
@@ -4,6 +4,7 @@ except ImportError:
from itertools import izip_longest as zip_longest
from django.template import Library
+from django.utils.http import urlencode
register = Library()
@@ -13,3 +14,15 @@ def slice(items, group_size=1):
args = [iter(items)] * group_size
return (filter(None, group)
for group in zip_longest(*args, fillvalue=None))
+
+
[email protected]_tag(takes_context=True)
+def get_sort_by_url(context, field):
+ request = context['request']
+ request_get = request.GET.dict()
+ if field == request_get.get('sort_by'):
+ new_sort_by = '-%s' % field # descending sort
+ else:
+ new_sort_by = field # ascending sort
+ request_get['sort_by'] = new_sort_by
+ return '%s?%s' % (request.path, urlencode(request_get))
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index b984e2a3d0..6b85ae748b 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -21,20 +21,18 @@ class ProductFilter(FilterSet):
product_attributes.add(attribute)
for attribute in product_attributes:
- self.filters[attribute.slug] = \
- MultipleChoiceFilter(
- name='attributes__%s' % attribute.pk,
- label=attribute.name,
- widget=CheckboxSelectMultiple,
- choices=get_attribute_choices(attribute))
+ self.filters[attribute.slug] = MultipleChoiceFilter(
+ name='attributes__%s' % attribute.pk,
+ label=attribute.name,
+ widget=CheckboxSelectMultiple,
+ choices=get_attribute_choices(attribute))
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[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(
@@ -46,7 +44,6 @@ class ProductFilter(FilterSet):
class Meta:
model = Product
fields = ['price']
- exclude = []
filter_overrides = {
PriceField: {
'filter_class': RangeFilter
@@ -55,7 +52,4 @@ class ProductFilter(FilterSet):
def get_attribute_choices(attribute):
- result = [(choice.pk, choice.name)
- for choice in
- AttributeChoiceValue.objects.filter(attribute__pk=attribute.pk)]
- return result
+ return [(choice.pk, choice.name) for choice in attribute.values.all()]
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 7fafacd8a8..3ded0cbd15 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -113,18 +113,16 @@ def product_add_to_cart(request, slug, product_id):
def category_index(request, path, category_id):
category = get_object_or_404(Category, id=category_id)
actual_path = category.get_full_path()
- products = products_with_details(user=request.user)\
- .filter(categories__name=category)
+ products = products_with_details(
+ user=request.user).filter(categories__name=category)
product_filter = ProductFilter(request.GET, queryset=products)
- products = [(product,
- get_availability(product, discounts=request.discounts,
- local_currency=request.currency))
- for product in product_filter.qs]
+ products = [
+ (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)
- return TemplateResponse(request, 'category/index.html',
- {'category': category,
- 'products': products,
- 'filter': product_filter})
+ context = {'category': category, 'products': products,
+ 'filter': product_filter}
+ return TemplateResponse(request, 'category/index.html', context)
diff --git a/templates/category/index.html b/templates/category/index.html
index 3d5ae58291..70906c2414 100644
--- a/templates/category/index.html
+++ b/templates/category/index.html
@@ -28,15 +28,14 @@
{% endblock breadcrumb %}
{% block content %}
-
<div class="category-page">
<div class="category-top">
<div class="row">
<div class="col-md-7">
<ul class="breadcrumbs list-unstyled d-none d-md-block">
<li><a href="/">Home</a></li>
- {% if category.paren %}
- <li key=''><a href=''>{{ category.parent.name }}</a></li>
+ {% if category.parent %}
+ <li key=''><a href='(( category.parent.name.get_absule_url }}'>{{ category.parent.name }}</a></li>
{% endif %}
<li><a href='{{ category.get_absolute_url }}'>{{ category.name }}</a></li>
</ul>
@@ -95,8 +94,6 @@
<!--</div>-->
</h3>
{{ field }}
-
-
</div>
{% endif %}
{% endfor %}
diff --git a/tests/test_product.py b/tests/test_product.py
index 7736a8caa6..aece29640e 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -380,8 +380,8 @@ def test_product_filter_empty_get(authorized_client, product_in_stock,
assert list(products) == list(response.context['filter'].qs)
-def test_product_filter_product_exist(authorized_client, product_in_stock,
- default_category):
+def test_product_filter_product_exists(authorized_client, product_in_stock,
+ default_category):
products = models.Product.objects.all()
url = reverse('product:category', args=[default_category.slug,
default_category.pk])
@@ -390,8 +390,8 @@ def test_product_filter_product_exist(authorized_client, product_in_stock,
assert list(products) == list(response.context['filter'].qs)
-def test_product_filter_product_not_exist(authorized_client, product_in_stock,
- default_category):
+def test_product_filter_product_not_exists(authorized_client, product_in_stock,
+ default_category):
products = models.Product.objects.all()
url = reverse('product:category', args=[default_category.slug,
default_category.pk])