Skip to content

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

Add filters to customer list

michalca4f69a2a7cf
diff --git a/saleor/dashboard/customer/filters.py b/saleor/dashboard/customer/filters.py
new file mode 100644
index 0000000000..f88b08c953
--- /dev/null
+++ b/saleor/dashboard/customer/filters.py
@@ -0,0 +1,32 @@
+from __future__ import unicode_literals
+
+from django_filters import (FilterSet, OrderingFilter)
+from django.utils.translation import pgettext_lazy
+
+from ...userprofile.models import User
+
+
+SORT_BY_FIELDS = {
+ 'email': pgettext_lazy(
+ 'Customer list sorting option', 'email'),
+ 'default_billing_address__first_name': pgettext_lazy(
+ 'Customer list sorting option', 'name'),
+ 'default_billing_address__city': pgettext_lazy(
+ 'Customer list sorting option', 'location'),
+ 'num_orders': pgettext_lazy(
+ 'Customer list sorting option', 'number of orders'),
+ 'last_order': pgettext_lazy(
+ 'Customer list sorting option', 'last order'),
+}
+
+
+class CustomerFilter(FilterSet):
+ sort_by = OrderingFilter(
+ label=pgettext_lazy('Product list sorting form', 'Sort by'),
+ fields=SORT_BY_FIELDS.keys(),
+ field_labels=SORT_BY_FIELDS
+ )
+
+ class Meta:
+ model = User
+ fields = ['email', 'is_active', 'is_staff']
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index 724e0b1676..8aaeba1b07 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -11,6 +11,7 @@ from django.utils.translation import pgettext_lazy
from ...core.utils import get_paginator_items
from ...userprofile.models import User
from ..views import staff_member_required
+from .filters import CustomerFilter
@staff_member_required
@@ -24,9 +25,11 @@ def customer_list(request):
num_orders=Count('orders', distinct=True),
last_order=Max('orders', distinct=True))
.order_by('email'))
+ customer_filter = CustomerFilter(request.GET, queryset=customers)
customers = get_paginator_items(
- customers, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))
- ctx = {'customers': customers}
+ customer_filter.qs, settings.DASHBOARD_PAGINATE_BY,
+ request.GET.get('page'))
+ ctx = {'customers': customers, 'filter': customer_filter}
return TemplateResponse(request, 'dashboard/customer/list.html', ctx)
diff --git a/saleor/dashboard/order/filters.py b/saleor/dashboard/order/filters.py
index 058e2258b6..742dbfb2c2 100644
--- a/saleor/dashboard/order/filters.py
+++ b/saleor/dashboard/order/filters.py
@@ -8,9 +8,12 @@ from ...order.models import Order
SORT_BY_FIELDS = {
- 'status': pgettext_lazy('Sale list sorting option', 'status'),
- 'created': pgettext_lazy('Sale list sorting option', 'created'),
- 'user_email': pgettext_lazy('Sale list sorting option', 'user_email')}
+ 'pk': pgettext_lazy('Order list sorting option', '#'),
+ 'status': pgettext_lazy('Order list sorting option', 'status'),
+ 'payments__status': pgettext_lazy('Order list sorting option', 'payment'),
+ 'user__email': pgettext_lazy('Order list sorting option', 'email'),
+ 'created': pgettext_lazy('Order list sorting option', 'created'),
+ 'total_net': pgettext_lazy('Order list sorting option', 'created')}
class OrderFilter(FilterSet):
diff --git a/templates/dashboard/customer/list.html b/templates/dashboard/customer/list.html
index 1093e2eecd..c03be064df 100644
--- a/templates/dashboard/customer/list.html
+++ b/templates/dashboard/customer/list.html
@@ -1,6 +1,7 @@
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load prices_i18n %}
+{% load shop %}
{% load materializecss %}
{% load utils %}
@@ -21,6 +22,7 @@
{% endblock %}
{% block content %}
+ {% include 'dashboard/includes/_filters.html' %}
{% if customers %}
<div class="card">
<div class="data-table-container">
@@ -28,19 +30,29 @@
<thead>
<tr>
<th>
- {% trans "Email" context "Customers table header" %}
+ <a href="{% get_sort_by_url_toggle 'email' %}">
+ {% trans "Email" context "Customers table header" %}
+ </a>
</th>
<th class="wide">
- {% trans "Name" context "Customers table header" %}
+ <a href="{% get_sort_by_url_toggle 'default_billing_address__first_name' %}">
+ {% trans "Name" context "Customers table header" %}
+ </a>
</th>
<th>
- {% trans "Location" context "Customers table header" %}
+ <a href="{% get_sort_by_url_toggle 'default_billing_address__city' %}">
+ {% trans "Location" context "Customers table header" %}
+ </a>
</th>
<th class="right-align">
- {% trans "Number of orders" context "Customers table header" %}
+ <a href="{% get_sort_by_url_toggle 'num_orders' %}">
+ {% trans "Number of orders" context "Customers table header" %}
+ </a>
</th>
<th>
- {% trans "Last order" context "Customers table header" %}
+ <a href="{% get_sort_by_url_toggle 'last_order' %}">
+ {% trans "Last order" context "Customers table header" %}
+ </a>
</th>
</tr>
</thead>
diff --git a/templates/dashboard/includes/_orders_table.html b/templates/dashboard/includes/_orders_table.html
index 232d9be849..c260f28637 100644
--- a/templates/dashboard/includes/_orders_table.html
+++ b/templates/dashboard/includes/_orders_table.html
@@ -1,5 +1,6 @@
{% load i18n %}
{% load prices_i18n %}
+{% load shop %}
{% load status %}
<div class="data-table-container">
@@ -7,22 +8,34 @@
<thead>
<tr>
<th>
- #
+ <a href="{% get_sort_by_url_toggle 'pk' %}">
+ #
+ </a>
</th>
<th>
- {% trans "Order status" context "Orders table header" %}
+ <a href="{% get_sort_by_url_toggle 'status' %}">
+ {% trans "Order status" context "Orders table header" %}
+ </a>
</th>
<th>
- {% trans "Payment status" context "Orders table header" %}
+ <a href="{% get_sort_by_url_toggle 'payments__status' %}">
+ {% trans "Payment status" context "Orders table header" %}
+ </a>
</th>
<th>
- {% trans "Customer" context "Orders table header" %}
+ <a href="{% get_sort_by_url_toggle 'user__email' %}">
+ {% trans "Customer" context "Orders table header" %}
+ </a>
</th>
<th>
- {% trans "Placed on" context "Orders table header" %}
+ <a href="{% get_sort_by_url_toggle 'created' %}">
+ {% trans "Placed on" context "Orders table header" %}
+ </a>
</th>
<th class="right-align">
- {% trans "Total" context "Orders table header" %}
+ <a href="{% get_sort_by_url_toggle 'total_net' %}">
+ {% trans "Total" context "Orders table header" %}
+ </a>
</th>
</tr>
</thead>