Skip to content

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

Refactor sorted_by

michal87d24f77a136
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 11f8abdd8f..c239d04f58 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -92,3 +92,12 @@ class ProductFilter(FilterSet):
def get_sort_by_choices(filter):
return [(choice[0], choice[1].lower()) for choice in
filter.filters['sort_by'].field.choices[1::2]]
+
+
+def get_now_sorted_by(product_filter):
+ 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']
+ return sort_by
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 34751f4b4f..67272c42e5 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -7,9 +7,8 @@ from django.core.urlresolvers import reverse
from django.http import HttpResponsePermanentRedirect, JsonResponse
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 get_sort_by_choices, ProductFilter, SORT_BY_FIELDS
+from .filters import get_now_sorted_by, get_sort_by_choices, ProductFilter
from .models import Category
from .utils import (products_with_details, products_for_cart,
handle_cart_form, get_availability,
@@ -128,15 +127,11 @@ 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 = 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']
+ now_sorted_by = get_now_sorted_by(product_filter)
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': sort_by,
- 'is_descending': sort_by.startswith('-')}
+ 'now_sorted_by': now_sorted_by,
+ 'is_descending': now_sorted_by.startswith('-')}
return TemplateResponse(request, 'category/index.html', ctx)