Skip to content

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

Add customer search

Marcin Gębalaeb99a83a1d73
diff --git a/saleor/dashboard/customer/forms.py b/saleor/dashboard/customer/forms.py
new file mode 100644
index 0000000000..9c2a5e55a6
--- /dev/null
+++ b/saleor/dashboard/customer/forms.py
@@ -0,0 +1,7 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+
+class CustomerSearchForm(forms.Form):
+ email = forms.CharField(required=False, label=_('Email'))
+ name = forms.CharField(required=False, label=_('Name'))
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index 81d4be2991..1b3d10b685 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -1,22 +1,70 @@
-from django.db.models import Count, Max
+from django.db.models import Count, Max, Q
from django.views.generic import ListView, DetailView
+from django.views.generic.edit import FormMixin
+from .forms import CustomerSearchForm
from ..views import StaffMemberOnlyMixin
from ...userprofile.models import User
-class CustomerList(StaffMemberOnlyMixin, ListView):
+class CustomerList(StaffMemberOnlyMixin, ListView, FormMixin):
model = User
template_name = 'dashboard/customer/list.html'
paginate_by = 30
+ form_class = CustomerSearchForm
+ context_object_name = 'customers'
+
+ def dispatch(self, request, *args, **kwargs):
+ form_class = self.get_form_class()
+ self.form = self.get_form(form_class)
+ return super(CustomerList, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
qs = super(CustomerList, self).get_queryset()
+ qs = self._annotate(qs)
+ return self.apply_search(qs)
+
+ def get_context_data(self, **kwargs):
+ ctx = super(CustomerList, self).get_context_data(**kwargs)
+ ctx['form'] = self.form
+ ctx['render_search'] = self.form.is_valid()
+ ctx['have_open_orders'] = self.get_users_with_open_orders()
+ return ctx
+
+ def get_users_with_open_orders(self):
+ qs = super(CustomerList, self).get_queryset()
+ qs = self._annotate(qs)
open_order = ['new', 'payment-pending', 'fully-paid']
- qs = qs.prefetch_related('orders').filter(
- orders__status__in=open_order)
- qs = qs.annotate(num_orders=Count('orders', distinct=True),
- last_order=Max('orders', distinct=True))
- return qs
+ return qs.filter(orders__status__in=open_order)
+
+ def apply_search(self, queryset):
+ if self.form.is_valid():
+ return self.apply_search_filters(queryset, self.form.cleaned_data)
+ else:
+ return queryset
+
+ def apply_search_filters(self, queryset, data):
+ if data['email']:
+ queryset = queryset.filter(email__icontains=data['email'])
+ if data['name']:
+ parts = data['name'].split()
+ if len(parts) == 2:
+ query = (Q(addresses__first_name__icontains=parts[0])
+ | Q(addresses__last_name__icontains=parts[1])) \
+ | (Q(addresses__first_name__icontains=parts[1])
+ | Q(addresses__last_name__icontains=parts[0]))
+ else:
+ query = Q(addresses__first_name__icontains=data['name']) \
+ | Q(addresses__last_name__istartswith=data['name'])
+ queryset = queryset.filter(query).distinct()
+ return queryset
+
+ def post(self, request, **kwargs):
+ return self.get(request, **kwargs)
+
+ def _annotate(self, queryset):
+ return queryset.prefetch_related('orders', 'addresses').annotate(
+ num_orders=Count('orders', distinct=True),
+ last_order=Max('orders', distinct=True))
class CustomerDetails(StaffMemberOnlyMixin, DetailView):
diff --git a/saleor/dashboard/templates/dashboard/customer/_customers_table.html b/saleor/dashboard/templates/dashboard/customer/_customers_table.html
new file mode 100644
index 0000000000..81c65549ea
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/customer/_customers_table.html
@@ -0,0 +1,39 @@
+{% load i18n %}
+{% load prices_i18n %}
+
+{% if customers %}
+ <table class="table">
+ <thead>
+ <th>{% trans "Email" %}</th>
+ <th>{% trans "Location" %}</th>
+ <th>{% trans "Number of orders" %}</th>
+ <th>{% trans "Last order" %}</th>
+ </thead>
+ <tbody>
+ {% for customer in customers %}
+ <tr>
+ {% with customer.default_billing_address as default_address %}
+ <td>
+ <a href="{% url "dashboard:customer-details" pk=customer.pk %}">{{ customer.email }}</a>
+ </td>
+ <td>
+ {{ default_address.address.city }}, {{ default_address.address.get_country_display }}
+ </td>
+ {% endwith %}
+ <td>
+ {{ customer.num_orders }}
+ </td>
+ <td>
+ {% if customer.last_order %}
+ <a href="{% url 'dashboard:order-details' pk=customer.last_order %}">{{ customer.last_order }}</a>
+ {% else %}
+ -
+ {% endif %}
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+{% else %}
+ <span>No customers found</span>
+{% endif %}
diff --git a/saleor/dashboard/templates/dashboard/customer/list.html b/saleor/dashboard/templates/dashboard/customer/list.html
index 7a25cbb03c..31dc718c88 100644
--- a/saleor/dashboard/templates/dashboard/customer/list.html
+++ b/saleor/dashboard/templates/dashboard/customer/list.html
@@ -2,53 +2,51 @@
{% load i18n %}
{% load prices_i18n %}
+{% load bootstrap %}
{% block title %}{% trans "Customers" %}{% endblock %}
{% block content %}
+ <h1 class="page-header">{% trans "Customers" %}</h1>
+
+ <div class="row">
+ <div class="col-lg-12">
+
+ <div class="well">
+ <form method="post" class="form-inline">
+ {% csrf_token %}
+ {{ form }}
+ <button type="submit" class="btn btn-default" name='search'><i class="glyphicon glyphicon-search"></i> Search</button>
+ </form>
+ </div>
+
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ <span class="panel-title">Search results</span>
+ </div>
+ <div class="panel-body">
+ {% if render_search %}
+ {% include 'dashboard/customer/_customers_table.html' with customers=customers %}
+ {% else %}
+ <span>-</span>
+ {% endif %}
+ </div>
+ </div>
+ </div>
+ </div>
+
<div class="row">
<div class="col-lg-12">
- <h1 class="page-header">{% trans "Customers" %}</h1>
<div class="panel panel-default">
<div class="panel-heading">
<span class="panel-title">Customers with open orders</span>
</div>
<div class="panel-body">
- <table class="table">
- <thead>
- <th>{% trans "Email" %}</th>
- <th>{% trans "Location" %}</th>
- <th>{% trans "Number of orders" %}</th>
- <th>{% trans "Last order" %}</th>
- </thead>
- <tbody>
- {% for customer in object_list %}
- <tr>
- {% with customer.default_billing_address as default_address %}
- <td>
- <a href="{% url "dashboard:customer-details" pk=customer.pk %}">{{ customer.email }}</a>
- </td>
- <td>
- {{ default_address.address.city }}, {{ default_address.address.get_country_display }}
- </td>
- {% endwith %}
- <td>
- {{ customer.num_orders }}
- </td>
- <td>
- {% if customer.last_order %}
- <a href="{% url 'dashboard:order-details' pk=customer.last_order %}">{{ customer.last_order }}</a>
- {% else %}
- -
- {% endif %}
- </td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
+ {% include 'dashboard/customer/_customers_table.html' with customers=have_open_orders %}
</div>
</div>
+
</div>
</div>
{% endblock %}