Skip to content

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

Refactor filters and category_index

michal979897d2896a
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 6e5f0e3363..07db74352f 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -3,7 +3,7 @@ from collections import OrderedDict
from django_filters import (FilterSet, MultipleChoiceFilter, RangeFilter,
OrderingFilter)
-from django.forms import CheckboxSelectMultiple
+from django.forms import CheckboxSelectMultiple, ValidationError
from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
@@ -11,6 +11,10 @@ from django_prices.models import PriceField
from .models import Product, ProductAttribute
+SORT_BY_FIELDS = {'name': pgettext_lazy('Product field', 'name'),
+ 'price': pgettext_lazy('Product field', 'price')}
+
+
class ProductFilter(FilterSet):
def __init__(self, *args, **kwargs):
self.category = kwargs.pop('category')
@@ -20,12 +24,12 @@ class ProductFilter(FilterSet):
self.filters.update(self._get_product_attributes_filters())
self.filters.update(self._get_product_variants_attributes_filters())
self.filters = OrderedDict(sorted(self.filters.items()))
+ self.form.fields['sort_by'].validators.append(self.validate_sort_by)
sort_by = OrderingFilter(
- label=pgettext_lazy('Sort by label', 'Sort by'),
- fields=(
- ('price', 'price'),
- ('name', 'name'))
+ label=pgettext_lazy('Product list sorting form', 'Sort by'),
+ fields=SORT_BY_FIELDS.keys(),
+ field_labels=SORT_BY_FIELDS
)
class Meta:
@@ -73,6 +77,15 @@ class ProductFilter(FilterSet):
def _get_attribute_choices(self, attribute):
return [(choice.pk, choice.name) for choice in attribute.values.all()]
+ def validate_sort_by(self, value):
+ choices = []
+ for field in SORT_BY_FIELDS.keys():
+ choices.append(field)
+ choices.append('-' + field)
+ if value not in choices:
+ raise ValidationError(
+ ('%s is not an even number' % value)
+ )
def get_sort_by_choices(filter):
return [(choice[0], choice[1].lower()) for choice in
diff --git a/saleor/product/views.py b/saleor/product/views.py
index d7862a164a..34751f4b4f 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -9,7 +9,7 @@ from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.translation import pgettext_lazy
-from .filters import ProductFilter, get_sort_by_choices
+from .filters import get_sort_by_choices, ProductFilter, SORT_BY_FIELDS
from .models import Category
from .utils import (products_with_details, products_for_cart,
handle_cart_form, get_availability,
@@ -128,13 +128,15 @@ def category_index(request, path, category_id):
product_filter.qs, PAGINATE_BY, request.GET.get('page'))
products_and_availability = list(products_with_availability(
products_paginated, request.discounts, request.currency))
- sort_by = request.GET.get(
- 'sort_by', pgettext_lazy('Product field', 'name'))
+ sort_by = product_filter.form.cleaned_data.get('sort_by')
+ if sort_by:
+ sort_by = SORT_BY_FIELDS[sort_by[0].strip('-')]
+ else:
+ sort_by = SORT_BY_FIELDS['name']
ctx = {'category': category, 'filter': product_filter,
'products': products_and_availability,
'products_paginated': products_paginated,
'sort_by_choices': get_sort_by_choices(product_filter),
- 'now_sorted_by': pgettext_lazy(
- 'Product field', sort_by.strip('-')),
+ 'now_sorted_by': sort_by,
'is_descending': sort_by.startswith('-')}
return TemplateResponse(request, 'category/index.html', ctx)
diff --git a/templates/category/index.html b/templates/category/index.html
index 6f1be5f9b7..66499d5949 100644
--- a/templates/category/index.html
+++ b/templates/category/index.html
@@ -68,7 +68,7 @@
<li>
<div class="row">
<div class="col-6">
- {% trans 'Sort by' context 'Category page filters' %}: <strong>{{ choice.label }}</strong>
+ {% trans 'Sort by' context 'Category page filters' %}: <strong>{{ label }}</strong>
</div>
<div class="col-6">
<div>
@@ -172,16 +172,28 @@
<div class="col-md-8 col-lg-9 category-list">
<div>
<div>
- <div class="row">
- {% include "category/_items.html" with products=products %}
- </div>
- <div class="row">
- <div class="m-auto">
- {% if products_paginated.has_other_pages %}
- {% bootstrap_pagination products_paginated extra=request.GET.urlencode %}
- {% endif %}
+ {% if products %}
+ <div class="row">
+
+ {% include "category/_items.html" with products=products %}
+
</div>
- </div>
+ <div class="row">
+ <div class="m-auto">
+ {% if products_paginated.has_other_pages %}
+ {% bootstrap_pagination products_paginated extra=request.GET.urlencode %}
+ {% endif %}
+ </div>
+ </div>
+ {% else %}
+ <div class="row no-results">
+ <div class="col-12">
+ <svg data-src="{% static "images/pirate.svg" %}" width="360" height="360"/>
+ <p>{% trans "Sorry, no matches found for your request." context "Search without results text" %}</p>
+ <p>{% trans "Try again or shop new arrivals." context "Search without results text" %}</p>
+ </div>
+ </div>
+ {% endif %}
</div>
</div>
</div>