Skip to content

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

Simplify passing sorting choices to the template

akjanik6c0e67934130
diff --git a/saleor/core/utils/filters.py b/saleor/core/utils/filters.py
index 08420f839c..5ae98e468a 100644
--- a/saleor/core/utils/filters.py
+++ b/saleor/core/utils/filters.py
@@ -1,8 +1,3 @@
-def get_sort_by_choices(filter_set):
- return [(choice[0], choice[1].lower()) for choice in
- list(filter_set.filters['sort_by'].field.choices)[1::2]]
-
-
def get_now_sorted_by(filter_set, default_sort='name'):
sort_by = filter_set.form.cleaned_data.get('sort_by')
if sort_by:
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index e40ed925f0..18f22f7f87 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -8,9 +8,11 @@ from django_prices.models import PriceField
from ..core.filters import SortedFilterSet
from .models import Product, ProductAttribute
-SORT_BY_FIELDS = {
- 'name': pgettext_lazy('Product list sorting option', 'name'),
- 'price': pgettext_lazy('Product list sorting option', 'price')}
+
+SORT_BY_FIELDS = OrderedDict([
+ ('name', pgettext_lazy('Product list sorting option', 'name')),
+ ('price', pgettext_lazy('Product list sorting option', 'price'))])
+
class ProductFilter(SortedFilterSet):
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 39cfb057d6..9c68230035 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -9,7 +9,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, get_sort_by_choices
+from ..core.utils.filters import get_now_sorted_by
from .filters import ProductFilter, SORT_BY_FIELDS
from .models import Category
from .utils import (
@@ -129,7 +129,7 @@ def category_index(request, path, category_id):
ctx = {'category': category, 'filter_set': product_filter,
'products': products_and_availability,
'products_paginated': products_paginated,
- 'sort_by_choices': get_sort_by_choices(product_filter),
+ 'sort_by_choices': SORT_BY_FIELDS,
'now_sorted_by': now_sorted_by,
'is_descending': is_descending}
return TemplateResponse(request, 'category/index.html', ctx)
diff --git a/templates/category/index.html b/templates/category/index.html
index 140d9ca381..dda7e2b6b8 100644
--- a/templates/category/index.html
+++ b/templates/category/index.html
@@ -87,7 +87,7 @@
</div>
</button>
<ul class="sort-list d-none">
- {% for choice, label in sort_by_choices %}
+ {% for choice, label in sort_by_choices.items %}
<li>
<div class="row">
<div class="col-6">