Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change sort_by popup
michal14d20d9e8eb7
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex de146ad193..fd6a4b0ed3 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -2,14 +2,17 @@ from __future__ import unicode_literalsfrom collections import OrderedDictfrom copy import deepcopy-from django_filters import FilterSet, MultipleChoiceFilter, RangeFilter, \- OrderingFilter+from django_filters import (FilterSet, MultipleChoiceFilter, RangeFilter,+ OrderingFilter)from django.forms import CheckboxSelectMultiplefrom django_prices.models import PriceFieldfrom .models import Product+SORT_BY_FIELDS = (('price', 'price'),+ ('name', 'name'))+class ProductFilter(FilterSet):def __init__(self, *args, **kwargs):@@ -39,8 +42,7 @@ class ProductFilter(FilterSet):sort_by = OrderingFilter(label='Sort by',- fields=(('price', 'price'),- ('name', 'name'))+ fields=SORT_BY_FIELDS)class Meta:@@ -52,20 +54,6 @@ class ProductFilter(FilterSet):}}- @property- def product_attributes_filter_form(self):- """- This method returns only those filters that ware dynamically generated- in __init__().- In this case 'price' and 'sort_by' are rendered in template differently- than rest of the filters.- :return:- """- form = deepcopy(self.form)- del form.fields['price']- del form.fields['sort_by']- return form-def get_attribute_choices(attribute):return [(choice.pk, choice.name) for choice in attribute.values.all()]diff --git a/saleor/product/templatetags/sort_by.py b/saleor/product/templatetags/sort_by.pynew file mode 100644index 0000000000..d7a426c9d6--- /dev/null+++ b/saleor/product/templatetags/sort_by.py@@ -0,0 +1,10 @@+from django import template++register = template.Library()++[email protected]_tag('category/_sort_by.html', takes_context=True)+def sort_by(context, attribute):+ context['attribute'] = attribute+ context['negativ_attribute'] = '-' + attribute+ return contextdiff --git a/saleor/product/views.py b/saleor/product/views.pyindex b4e5a75e1d..eb20a0f586 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -11,7 +11,7 @@ from django.template.response import TemplateResponsefrom ..cart.utils import set_cart_cookiefrom ..core.utils import serialize_decimal, get_paginator_itemsfrom ..settings import PAGINATE_BY-from .filters import ProductFilter+from .filters import ProductFilter, SORT_BY_FIELDSfrom .models import Categoryfrom .utils import (products_with_details, products_for_cart,handle_cart_form, get_availability,@@ -126,5 +126,5 @@ def category_index(request, path, category_id):products = get_paginator_items(products, PAGINATE_BY, request.GET.get('page'))context = {'category': category, 'products': products,- 'filter': product_filter}+ 'filter': product_filter, 'sort_by_choices': SORT_BY_FIELDS}return TemplateResponse(request, 'category/index.html', context)diff --git a/templates/category/_sort_by.html b/templates/category/_sort_by.htmlnew file mode 100644index 0000000000..d804b0ee23--- /dev/null+++ b/templates/category/_sort_by.html@@ -0,0 +1,27 @@+{% load i18n %}+{% load shop %}+{% load staticfiles %}++<li class="{{ attribute }}">+ <div class="row">+ <div class="col-6">+ Sort by: <strong>{% blocktrans %}{{ attribute }}{% endblocktrans %}</strong>+ </div>+ <div class="col-6">+ <div class="{{ attribute }}">+ <a href="{% get_sort_by_url attribute %}">+ <span>ascending</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>+ <div class="sort-order-icon float-right">+ <img src="{% static "assets/arrow_down.svg" %}">+ </div>+ </a>+ </div>+ </div>+ </div>+</li>diff --git a/templates/category/index.html b/templates/category/index.htmlindex d324525b15..c6ca3ed98c 100644--- a/templates/category/index.html+++ b/templates/category/index.html@@ -1,11 +1,11 @@{% extends "base.html" %}+{% load bootstrap_pagination from bootstrap3 %}{% load i18n %}{% load shop %}{% load staticfiles %}-{% load bootstrap_pagination from bootstrap3 %}+{% load sort_by from sort_by %}{% load render_bundle from webpack_loader %}{% load prices_i18n %}-{% load product_first_image from product_images %}{% block footer_scripts %}{{ block.super }}@@ -69,52 +69,9 @@</div></button><ul class="sort-list d-none">- <li class="name">- <div class="row">- <div class="col-6">- Sort by: <strong>{% trans "name" context "Product name" %}</strong>- </div>- <div class="col-6">- <div class="name">- <a href="{% get_sort_by_url 'name' %}">- <span>ascending</span>- <div class="sort-order-icon float-right">- <img src="{% static "assets/arrow_up.svg" %}">- </div>- </a>- <a href="{% get_sort_by_url '-name' %}">- <span>descending</span>- <div class="sort-order-icon float-right">- <img src="{% static "assets/arrow_down.svg" %}">- </div>- </a>- </div>- </div>- </div>- </li>- <li class="price">- <div class="row">- <div class="col-6">- Sort by: <strong>{% trans "price" context "Product price" %}</strong>- </div>- <div class="col-6">- <div class="name">- <a href="{% get_sort_by_url 'price' %}">- <span>ascending</span>- <div class="sort-order-icon float-right">- <img src="{% static "assets/arrow_up.svg" %}">- </div>- </a>- <a href="{% get_sort_by_url '-price' %}">- <span>descending</span>- <div class="sort-order-icon float-right">- <img src="{% static "assets/arrow_down.svg" %}">- </div>- </a>- </div>- </div>- </div>- </li>+ {% for choice in sort_by_choices %}+ {% sort_by choice.0 %}+ {% endfor %}</ul></div></div>@@ -147,34 +104,40 @@<div class="product-filters__attributes" data-icon-up="{% static "assets/chevron_up.svg" %}"data-icon-down="{% static "assets/chevron_down.svg" %}"><form method="get">- {% for field in filter.product_attributes_filter_form %}- <div class="toggle-filter">- <h3 class="filter-label">- {{ field.label }}- <div class="collapse-filters-icon">- <img src="{% static "assets/chevron_up.svg" %}">+ {% for field in filter.form %}+ {% if field.name == 'price' %}+ <div class="toggle-filter product-filters__price-range">+ <h3 class="filter-label">+ {{ field.label }}+ <div class="collapse-filters-icon">+ <img src="{% static "assets/chevron_up.svg" %}">+ </div>+ </h3>+ <div class="filter-form-field" style="display:block">+ <input id="{{ field.auto_id }}_0" name="{{ field.name }}_0" value="{% if field.value.0 %}{{ field.value.0 }}{% endif %}" type="number" min="0" class="form-control"+ placeholder="from"><span>-</span><input id="{{ field.auto_id }}_1" name="{{ field.name }}_1" value="{% if field.value.1 %}{{ field.value.1 }}{% endif %}" type="number" min="0"+ class="form-control" placeholder="to">+ <button class="btn primary" type="submit">Update</button></div>- </h3>- <div class="filter-form-field" style="display:block">- {{ field }}</div>- </div>- {% endfor %}- <div class="toggle-filter product-filters__price-range">- <h3 class="filter-label">- {{ filter.form.fields.price.label }}- <div class="collapse-filters-icon">- <img src="{% static "assets/chevron_up.svg" %}">+ {% elif field.name == 'sort_by' %}+ <!--Field 'sort_by' is hidden because it is rendered in header.+ This is required if you want to have sorting to be kept during further filtering.-->+ <input type="hidden" name="sort_by" value="{% if request.GET.sort_by %}{{ request.GET.sort_by }}{% endif %}">+ {% else %}+ <div class="toggle-filter">+ <h3 class="filter-label">+ {{ field.label }}+ <div class="collapse-filters-icon">+ <img src="{% static "assets/chevron_up.svg" %}">+ </div>+ </h3>+ <div class="filter-form-field" style="display:block">+ {{ field }}+ </div></div>- </h3>- <div class="filter-form-field" style="display:block">- <input id="price_0" name="price_0" type="number" min="0" class="form-control"- placeholder="from"><span>-</span><input id="price_1" name="price_1" type="number" min="0"- class="form-control" placeholder="to">- <input type="hidden" name="sort_by" value="{{ request.GET.sort_by }}">- <button class="btn primary" type="submit">Update</button>- </div>- </div>+ {% endif %}+ {% endfor %}</form></div></div>@@ -186,6 +149,11 @@<div class="row">{% include "category/_items.html" with products=products %}</div>+ <div class="row">+ <div class="m-auto">+ {% bootstrap_pagination products extra=request.GET.urlencode %}+ </div>+ </div></div></div></div>