Skip to content

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

Add pagination for customer list view

Marcin Gębalabdd496473e1a
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index caefe3accb..4c546f0ae5 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -1,3 +1,4 @@
+from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Count, Max, Q
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
@@ -34,14 +35,24 @@ def customer_list(request):
customers = User.objects.prefetch_related('orders', 'addresses').annotate(
num_orders=Count('orders', distinct=True),
last_order=Max('orders', distinct=True))
- form = CustomerSearchForm(request.POST or request.GET or None)
+ form = CustomerSearchForm(request.GET or None)
if form.is_valid():
customers = _search_customers(customers, form.cleaned_data)
title = _('Search results')
else:
customers = _get_users_with_open_orders(customers)
title = _('Customers with open orders')
- ctx = {'customers': customers, 'form': form, 'title': title}
+
+ paginator = Paginator(customers, 5)
+ try:
+ customers = paginator.page(request.GET.get('page'))
+ except PageNotAnInteger:
+ customers = paginator.page(1)
+ except EmptyPage:
+ customers = paginator.page(paginator.num_pages)
+
+ ctx = {'customers': customers, 'form': form, 'title': title,
+ 'paginator': paginator}
return TemplateResponse(request, 'dashboard/customer/list.html', ctx)
diff --git a/saleor/dashboard/templates/dashboard/customer/list.html b/saleor/dashboard/templates/dashboard/customer/list.html
index fcaa8c40d2..510e971f44 100644
--- a/saleor/dashboard/templates/dashboard/customer/list.html
+++ b/saleor/dashboard/templates/dashboard/customer/list.html
@@ -16,8 +16,7 @@
<div class="panel panel-default">
<div class="panel-heading">{% trans "Search customers" %}</div>
<div class="panel-body">
- <form method="post" class="form-inline">
- {% csrf_token %}
+ <form method="get" class="form-inline">
<div class="form-group">
<label class="control-label"for="amount">{% trans "Email" %}</label>
{% render_widget form.email class="form-control" %}
@@ -26,7 +25,7 @@
<label class="control-label"for="amount">{% trans "Name" %}</label>
{% render_widget form.name class="form-control" %}
</div>
- <button type="submit" class="btn btn-default" name='search'><i class="glyphicon glyphicon-search"></i> Search</button>
+ <button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i> Search</button>
<a href="{% url 'dashboard:customers' %}">Clear</a>
</form>
</div>
@@ -74,6 +73,7 @@
{% endfor %}
</tbody>
</table>
+ {% include "dashboard/includes/_pagination.html" with page_obj=customers %}
{% else %}
<span>No customers found</span>
{% endif %}