Skip to content

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

Final touch

michal0d265088dd5e
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 1d8442894f..8ecb3e2a22 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -1,25 +1,25 @@
from __future__ import unicode_literals
from collections import OrderedDict
-from copy import deepcopy
from django_filters import (FilterSet, MultipleChoiceFilter, RangeFilter,
OrderingFilter)
from django.forms import CheckboxSelectMultiple
+from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
from .models import Product
-SORT_BY_FIELDS = (('price', 'price'),
- ('name', 'name'))
+SORT_BY_FIELDS = (('price', pgettext_lazy('Sort by filter', 'price')),
+ ('name', pgettext_lazy('Sort by filter', 'name')))
class ProductFilter(FilterSet):
def __init__(self, *args, **kwargs):
super(ProductFilter, self).__init__(*args, **kwargs)
- product_attributes, variant_attributes = self.get_attributes()
- self.add_product_attributes_filters(product_attributes)
- self.add_product_variants_attributes_filters(variant_attributes)
+ product_attributes, variant_attributes = self._get_attributes()
+ self._add_product_attributes_filters(product_attributes)
+ self._add_product_variants_attributes_filters(variant_attributes)
self.filters = OrderedDict(sorted(self.filters.items()))
sort_by = OrderingFilter(
@@ -36,7 +36,7 @@ class ProductFilter(FilterSet):
}
}
- def get_attributes(self):
+ def _get_attributes(self):
product_attributes = set()
variant_attributes = set()
for product in self.queryset:
@@ -46,7 +46,7 @@ class ProductFilter(FilterSet):
product_attributes.add(attribute)
return product_attributes, variant_attributes
- def add_product_attributes_filters(self, product_attributes):
+ def _add_product_attributes_filters(self, product_attributes):
for attribute in product_attributes:
self.filters[attribute.slug] = MultipleChoiceFilter(
name='attributes__%s' % attribute.pk,
@@ -54,7 +54,7 @@ class ProductFilter(FilterSet):
widget=CheckboxSelectMultiple,
choices=get_attribute_choices(attribute))
- def add_product_variants_attributes_filters(self, variant_attributes):
+ def _add_product_variants_attributes_filters(self, variant_attributes):
for attribute in variant_attributes:
self.filters[attribute.slug] = MultipleChoiceFilter(
name='variants__attributes__%s' % attribute.pk,
diff --git a/saleor/product/templatetags/sort_by.py b/saleor/product/templatetags/sort_by.py
index d7a426c9d6..490e09fe96 100644
--- a/saleor/product/templatetags/sort_by.py
+++ b/saleor/product/templatetags/sort_by.py
@@ -5,6 +5,6 @@ register = template.Library()
@register.inclusion_tag('category/_sort_by.html', takes_context=True)
def sort_by(context, attribute):
- context['attribute'] = attribute
- context['negativ_attribute'] = '-' + attribute
+ context['ascending_attribute'] = attribute
+ context['descending_attribute'] = '-' + str(attribute)
return context
diff --git a/saleor/product/views.py b/saleor/product/views.py
index f5e4cfe66a..720ebbe374 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -9,14 +9,15 @@ from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from ..cart.utils import set_cart_cookie
-from ..core.utils import serialize_decimal, get_paginator_items
+from ..core.utils import get_paginator_items, serialize_decimal
from ..settings import PAGINATE_BY
-from .filters import ProductFilter, SORT_BY_FIELDS
+from .filters import SORT_BY_FIELDS, ProductFilter
from .models import Category
from .utils import (products_with_details, products_for_cart,
handle_cart_form, get_availability,
get_product_images, get_variant_picker_data,
- get_product_attributes_data, product_json_ld)
+ get_product_attributes_data,
+ product_json_ld, products_with_availability)
def product_details(request, slug, product_id, form=None):
@@ -114,17 +115,17 @@ 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)
- product_filter = ProductFilter(request.GET, queryset=products)
- products_with_availability = [
- (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)
+ products = products_with_details(
+ user=request.user).filter(categories__name=category)
+ product_filter = ProductFilter(request.GET, queryset=products)
+ products = list(products_with_availability(
+ product_filter.qs, request.discounts, request.currency))
products_paginated = get_paginator_items(
- products_with_availability, PAGINATE_BY, request.GET.get('page'))
- context = {'category': category, 'products': products_paginated,
- 'filter': product_filter, 'sort_by_choices': SORT_BY_FIELDS}
+ products, PAGINATE_BY, request.GET.get('page'))
+ context = {'category': category, 'filter': product_filter,
+ 'products': products_paginated,
+ 'sort_by_choices': [choice[1] for choice in SORT_BY_FIELDS]}
return TemplateResponse(request, 'category/index.html', context)
diff --git a/templates/category/_sort_by.html b/templates/category/_sort_by.html
index d804b0ee23..eedaf59aff 100644
--- a/templates/category/_sort_by.html
+++ b/templates/category/_sort_by.html
@@ -2,21 +2,21 @@
{% load shop %}
{% load staticfiles %}
-<li class="{{ attribute }}">
+<li>
<div class="row">
<div class="col-6">
- Sort by: <strong>{% blocktrans %}{{ attribute }}{% endblocktrans %}</strong>
+ Sort by: <strong>{{ ascending_attribute }}</strong>
</div>
<div class="col-6">
- <div class="{{ attribute }}">
- <a href="{% get_sort_by_url attribute %}">
- <span>ascending</span>
+ <div>
+ <a href="{% get_sort_by_url ascending_attribute %}">
+ <span>{% trans 'ascending' context 'sort by option' %}</span>
<div class="sort-order-icon float-right">
<img src="{% static "assets/arrow_up.svg" %}">
</div>
</a>
- <a href="{% get_sort_by_url negativ_attribute %}">
- <span>descending</span>
+ <a href="{% get_sort_by_url descending_attribute %}">
+ <span>{% trans 'descending' context 'sort by option' %}</span>
<div class="sort-order-icon float-right">
<img src="{% static "assets/arrow_down.svg" %}">
</div>
diff --git a/templates/category/index.html b/templates/category/index.html
index a558f6c639..d5b8404817 100644
--- a/templates/category/index.html
+++ b/templates/category/index.html
@@ -70,7 +70,7 @@
</button>
<ul class="sort-list d-none">
{% for choice in sort_by_choices %}
- {% sort_by choice.0 %}
+ {% sort_by choice %}
{% endfor %}
</ul>
</div>