Skip to content

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

Fix sort_by

michal420aed73f38d
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 3c29bd73e6..93b5a6813d 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -10,8 +10,13 @@ from django_prices.models import PriceField
from .models import Product, ProductAttribute
-SORT_BY_FIELDS = (('name', pgettext_lazy('Sort by filter', 'name')),
- ('price', pgettext_lazy('Sort by filter', 'price')))
+# SORT_BY_FIELDS = (('name', pgettext_lazy('Sort by filter', 'name')),
+# ('price', pgettext_lazy()))
+
+SORT_BY_FIELDS = [{'value': 'name',
+ 'label': pgettext_lazy('Sort by filter', 'name')},
+ {'value': 'price',
+ 'label': pgettext_lazy('Sort by filter', 'price')}]
class ProductFilter(FilterSet):
@@ -26,7 +31,7 @@ class ProductFilter(FilterSet):
sort_by = OrderingFilter(
label='Sort by',
- fields=SORT_BY_FIELDS
+ fields=[(field['value'], field['value']) for field in SORT_BY_FIELDS]
)
class Meta:
diff --git a/saleor/product/templatetags/sort_menu.py b/saleor/product/templatetags/sort_menu.py
index 0d0f1ef780..0d78026939 100644
--- a/saleor/product/templatetags/sort_menu.py
+++ b/saleor/product/templatetags/sort_menu.py
@@ -4,14 +4,15 @@ register = template.Library()
@register.inclusion_tag('category/_sort_menu.html', takes_context=True)
-def sort_menu(context, attribute):
- t = []
- for attr in [a[0] for a in attribute]:
- t.append(attr)
- t.append('-' + str(attr))
- choices = [a[1] for a in attribute]
- context['sort_by_choices'] = [{'label': a[1],
- 'value': a[0]} for a in attribute]
- context['choice'] = choices[int(t.index(context['request'].GET.get('sort_by', 'name')) / 2)]
- context['arrow_up'] = (context['request'].GET.get('sort_by', 'name')[0] != '-')
+def sort_menu(context, attributes):
+ ordering_options = []
+ for attr in [a['value'] for a in attributes]:
+ ordering_options.append(attr)
+ ordering_options.append('-' + str(attr))
+ choices = [a['label'] for a in attributes]
+ context['sort_by_choices'] = attributes
+ get_sort_by = context['request'].GET.get('sort_by', 'name')
+ context['choice'] = choices[int(ordering_options.index(get_sort_by) / 2)]
+ context['arrow_up'] = \
+ (context['request'].GET.get('sort_by', 'name')[0] != '-')
return context