Skip to content

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

Add sorting arrows

michalbbb6b96b3fcd
diff --git a/saleor/core/templatetags/shop.py b/saleor/core/templatetags/shop.py
index e9db74ebb9..081d8cabbe 100644
--- a/saleor/core/templatetags/shop.py
+++ b/saleor/core/templatetags/shop.py
@@ -4,6 +4,7 @@ try:
except ImportError:
from itertools import izip_longest as zip_longest
+from django.contrib.staticfiles.templatetags.staticfiles import static
from django.template import Library
from django.utils.http import urlencode
@@ -29,23 +30,48 @@ def get_sort_by_url(context, field, descending=False):
return '%s?%s' % (request.path, urlencode(request_get))
[email protected]_tag(takes_context=True)
-def get_sort_by_url_toggle(context, field):
[email protected]_tag(takes_context=True)
+def get_sort_by_toggle(context, field):
request = context['request']
request_get = request.GET.copy()
- if request_get.get('sort_by'):
+ sort_by = request_get.get('sort_by')
+ arrow_src = ''
+ active = ''
+ if sort_by:
# toggle existing sort_by
- new_sort_by = toggle(field, request_get)
+ new_sort_by, arrow_src, active = toggle(field, sort_by)
else:
# first click on link
new_sort_by = u'-%s' % field # descending sort
request_get['sort_by'] = new_sort_by
- return '%s?%s' % (request.path, request_get.urlencode())
+ return {
+ 'url': '%s?%s' % (request.path, request_get.urlencode()),
+ 'is_active': active,
+ 'arrow_src': arrow_src}
-def toggle(field, request_get):
- if field == request_get.get('sort_by'):
- new_sort_by = u'-%s' % field # descending sort
+
+def toggle(field, sort_by):
+ if field == sort_by:
+ active, arrow_src, new_sort_by = set_ascending(field)
+ else:
+ active, arrow_src, new_sort_by = set_descending(field, sort_by)
+ return new_sort_by, arrow_src, active
+
+
+def set_ascending(field):
+ new_sort_by = u'-%s' % field # descending sort
+ arrow_src = static('/images/arrow_up_icon.svg')
+ active = 'active'
+ return active, arrow_src, new_sort_by
+
+
+def set_descending(field, sort_by):
+ new_sort_by = field # ascending sort
+ if field == sort_by[1:]:
+ arrow_src = static('/images/arrow_down_icon.svg')
+ active = 'active'
else:
- new_sort_by = field # ascending sort
- return new_sort_by
+ arrow_src = ''
+ active = ''
+ return active, arrow_src, new_sort_by
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 3ff1daee10..c4efc3aa72 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -123,10 +123,8 @@ def product_list(request):
products = get_paginator_items(
product_filter.qs, settings.DASHBOARD_PAGINATE_BY,
request.GET.get('page'))
- sort_by = request.GET.get('sort_by')
ctx = {
- 'is_descending': sort_by.startswith('-') if sort_by else False,
'bulk_action_form': forms.ProductBulkUpdate(), 'form': form,
'products': products, 'product_classes': product_classes,
'filter': product_filter,
diff --git a/templates/dashboard/category/list.html b/templates/dashboard/category/list.html
index a8bbe33142..276e9db3a0 100644
--- a/templates/dashboard/category/list.html
+++ b/templates/dashboard/category/list.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load mptt_tags %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -116,13 +116,17 @@
<table class="bordered highlight responsive data-table data-table-description no-align">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Category list table header name" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'description' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Description" context "Category list table header description" %}
</a>
</th>
diff --git a/templates/dashboard/customer/list.html b/templates/dashboard/customer/list.html
index fccce98369..b599337ad3 100644
--- a/templates/dashboard/customer/list.html
+++ b/templates/dashboard/customer/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load prices_i18n %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load materializecss %}
{% load utils %}
@@ -30,18 +30,24 @@
<table class="bordered highlight responsive data-table">
<thead>
<tr>
- <th class="wide">
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Customers table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'email' %}">
+ {% get_sort_by_toggle 'email' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Email" context "Customers table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'location' %}">
+ {% get_sort_by_toggle 'location' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Location" context "Customers table header" %}
</a>
</th>
diff --git a/templates/dashboard/discount/sale/list.html b/templates/dashboard/discount/sale/list.html
index c35c7782c5..3df04fc755 100644
--- a/templates/dashboard/discount/sale/list.html
+++ b/templates/dashboard/discount/sale/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load prices_i18n %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load materializecss %}
{% load utils %}
@@ -46,13 +46,17 @@
<table class="bordered highlight responsive data-table">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Sale (discount) table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'value' %}">
+ {% get_sort_by_toggle 'value' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Discount" context "Sale (discount) table header" %}
</th>
</tr>
diff --git a/templates/dashboard/discount/voucher/list.html b/templates/dashboard/discount/voucher/list.html
index f3e8c149a6..75dd4fb05e 100644
--- a/templates/dashboard/discount/voucher/list.html
+++ b/templates/dashboard/discount/voucher/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load prices_i18n %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load materializecss %}
{% load utils %}
{% load static %}
@@ -48,36 +48,48 @@
{% if vouchers %}
<thead>
<tr>
- <th class="wide">
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Voucher table header" %}
</a>
</th>
- <th class="right-align">
- <a href="{% get_sort_by_url_toggle 'discount_value' %}">
+ {% get_sort_by_toggle 'discount_value' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Discount" context "Voucher table header" %}
</a>
</th>
<th>
{% trans "Apply to" context "Voucher table header" %}
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'start_date' %}">
+ {% get_sort_by_toggle 'start_date' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Start date" context "Voucher table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'end_date' %}">
+ {% get_sort_by_toggle 'end_date' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "End date" context "Voucher table header" %}
</a>
</th>
- <th class="right-align">
- <a href="{% get_sort_by_url_toggle 'used' %}">
+ {% get_sort_by_toggle 'used' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Used" context "Voucher table header" %}
</a>
</th>
- <th class="right-align">
- <a href="{% get_sort_by_url_toggle 'limit' %}">
+ {% get_sort_by_toggle 'limit' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Limit" context "Voucher table header" %}
</a>
</th>
diff --git a/templates/dashboard/group/list.html b/templates/dashboard/group/list.html
index 0a1265efb9..be4bc4e482 100644
--- a/templates/dashboard/group/list.html
+++ b/templates/dashboard/group/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -43,8 +43,10 @@
<table class="bordered highlight data-table data-table-description no-align">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Dashboard groups list table header name" %}
</a>
</th>
diff --git a/templates/dashboard/includes/_orders_table.html b/templates/dashboard/includes/_orders_table.html
index 4f58ee5918..93058f8981 100644
--- a/templates/dashboard/includes/_orders_table.html
+++ b/templates/dashboard/includes/_orders_table.html
@@ -1,39 +1,51 @@
{% load i18n %}
{% load prices_i18n %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load status %}
<div class="data-table-container">
<table class="bordered highlight responsive data-table">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'pk' %}">
+ {% get_sort_by_toggle 'pk' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
#
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'email' %}">
+ {% get_sort_by_toggle 'email' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Customer" context "Orders table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'created' %}">
+ {% get_sort_by_toggle 'created' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Placed on" context "Orders table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'status' %}">
+ {% get_sort_by_toggle 'status' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Order status" context "Orders table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'payment_status' %}">
+ {% get_sort_by_toggle 'payment_status' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Payment status" context "Orders table header" %}
</a>
</th>
- <th class="right-align">
- <a href="{% get_sort_by_url_toggle 'total' %}">
+ {% get_sort_by_toggle 'total' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Total" context "Orders table header" %}
</a>
</th>
diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.html
index 7e57350a42..1662649f16 100644
--- a/templates/dashboard/product/list.html
+++ b/templates/dashboard/product/list.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load prices_i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load product_first_image from product_images %}
{% load utils %}
@@ -74,23 +74,17 @@
<label for="select-all-products"></label>
</th>
<th></th>
- <th class="wide{% if request.GET.sort_by == 'name' or request.GET.sort_by == '-name' %} active{% endif %}">
- <a href="{% get_sort_by_url_toggle 'name' %}">
- {% if is_descending %}
- <svg data-src="{% static '/images/arrow_down_icon.svg' %}" />
- {% else %}
- <svg data-src="{% static '/images/arrow_up_icon.svg' %}" />
- {% endif %}
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Products list table header" %}
</a>
</th>
- <th class="right-align {% if request.GET.sort_by == 'price' or request.GET.sort_by == '-price' %} active{% endif %}">
- <a href="{% get_sort_by_url_toggle 'price' %}">
- {% if is_descending %}
- <svg data-src="{% static '/images/arrow_down_icon.svg' %}" />
- {% else %}
- <svg data-src="{% static '/images/arrow_up_icon.svg' %}" />
- {% endif %}
+ {% get_sort_by_toggle 'price' as toggle %}
+ <th class="right-align {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Price" context "Products list table header" %}
</a>
</th>
diff --git a/templates/dashboard/product/product_attribute/list.html b/templates/dashboard/product/product_attribute/list.html
index 7fe1a000f5..66a195d58a 100644
--- a/templates/dashboard/product/product_attribute/list.html
+++ b/templates/dashboard/product/product_attribute/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -42,8 +42,10 @@
<table class="bordered highlight data-table data-table-description">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Dashboard attributes list table header name" %}
</a>
</th>
diff --git a/templates/dashboard/product/product_class/list.html b/templates/dashboard/product/product_class/list.html
index 9b058c562f..d161c573b2 100644
--- a/templates/dashboard/product/product_class/list.html
+++ b/templates/dashboard/product/product_class/list.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load prices_i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -43,8 +43,10 @@
<table class="bordered highlight data-table data-table-description">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Dashboard product types list table header name" %}
</a>
</th>
diff --git a/templates/dashboard/product/stock_location/list.html b/templates/dashboard/product/stock_location/list.html
index eb1074616e..388458f912 100644
--- a/templates/dashboard/product/stock_location/list.html
+++ b/templates/dashboard/product/stock_location/list.html
@@ -1,7 +1,7 @@
{% extends 'dashboard/base.html' %}
{% load i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -40,8 +40,10 @@
<table class="highlight bordered data-table no-align">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Dashboard stock location table header name" %}
</a>
</th>
diff --git a/templates/dashboard/shipping/list.html b/templates/dashboard/shipping/list.html
index 4831d33072..3d8fe05a1d 100644
--- a/templates/dashboard/shipping/list.html
+++ b/templates/dashboard/shipping/list.html
@@ -1,7 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load prices_i18n %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load status %}
{% load utils %}
{% load static %}
@@ -40,8 +40,10 @@
<table class="bordered highlight responsive data-table">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Shipping methods table header" %}
</a>
</th>
diff --git a/templates/dashboard/staff/list.html b/templates/dashboard/staff/list.html
index 022ae591d7..0d852ad4d1 100644
--- a/templates/dashboard/staff/list.html
+++ b/templates/dashboard/staff/list.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load prices_i18n %}
{% load materializecss %}
-{% load get_sort_by_url_toggle from shop %}
+{% load get_sort_by_toggle from shop %}
{% load staticfiles %}
{% load utils %}
@@ -43,18 +43,24 @@
<table class="bordered highlight responsive data-table">
<thead>
<tr>
- <th>
- <a href="{% get_sort_by_url_toggle 'email' %}">
+ {% get_sort_by_toggle 'email' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Email" context "Staff members table header" %}
</a>
</th>
- <th class="wide">
- <a href="{% get_sort_by_url_toggle 'name' %}">
+ {% get_sort_by_toggle 'name' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Name" context "Staff members table header" %}
</a>
</th>
- <th>
- <a href="{% get_sort_by_url_toggle 'location' %}">
+ {% get_sort_by_toggle 'location' as toggle %}
+ <th class="wide {{ toggle.is_active }}">
+ <a href="{{ toggle.url }}">
+ <svg data-src="{{ toggle.arrow_src }}" />
{% trans "Location" context "Staff members table header" %}
</a>
</th>