Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor customer views
Marcin Gębaladd00492ab5c7
diff --git a/saleor/dashboard/customer/urls.py b/saleor/dashboard/customer/urls.pyindex bc63d05f4e..8e787015b5 100644--- a/saleor/dashboard/customer/urls.py+++ b/saleor/dashboard/customer/urls.py@@ -1,11 +1,8 @@from django.conf.urls import patterns, urlfrom . import views-urlpatterns = patterns('',- url(r'^$', views.CustomerList.as_view(),- name='customers'),- url(r'^(?P<pk>[0-9]+)$',- views.CustomerDetails.as_view(),- name='customer-details')+urlpatterns = patterns(+ '',+ url(r'^$', views.customer_list, name='customers'),+ url(r'^(?P<pk>[0-9]+)$', views.customer_details, name='customer-details'))-diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.pyindex 2ad60d69df..f51224147d 100644--- a/saleor/dashboard/customer/views.py+++ b/saleor/dashboard/customer/views.py@@ -1,85 +1,56 @@+from django.contrib.admin.views.decorators import staff_member_requiredfrom django.db.models import Count, Max, Q-from django.views.generic import ListView, DetailView-from django.views.generic.edit import FormMixin+from django.shortcuts import get_object_or_404+from django.template.response import TemplateResponse+from django.utils.translation import ugettext_lazy as _from .forms import CustomerSearchForm-from ..views import StaffMemberOnlyMixinfrom ...userprofile.models import User-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']- 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)+def _search_customers(queryset, data):+ for k in data.keys():+ data[k] = data[k].strip()+ 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:- return queryset-- def apply_search_filters(self, queryset, data):- data = self._normalize_form_data(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))-- def _normalize_form_data(self, data):- for k in data.keys():- data[k] = data[k].strip()- return data---class CustomerDetails(StaffMemberOnlyMixin, DetailView):- model = User- template_name = 'dashboard/customer/detail.html'- context_object_name = 'customer'-- def get_queryset(self):- qs = super(CustomerDetails, self).get_queryset()- qs = qs.prefetch_related('orders', 'addresses').select_related(- 'default_billing_address')- return qs+ query = Q(addresses__first_name__icontains=data['name']) \+ | Q(addresses__last_name__istartswith=data['name'])+ queryset = queryset.filter(query).distinct()+ return queryset+++def _get_users_with_open_orders(queryset):+ open_order = ['new', 'payment-pending', 'fully-paid']+ return queryset.filter(orders__status__in=open_order)+++@staff_member_required+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)+ 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}+ return TemplateResponse(request, 'dashboard/customer/list.html', ctx)+++@staff_member_required+def customer_details(request, pk):+ qs = User.objects.prefetch_related('orders', 'addresses').select_related(+ 'default_billing_address')+ customer = get_object_or_404(qs, pk=pk)+ ctx = {'customer': customer}+ return TemplateResponse(request, 'dashboard/customer/detail.html', ctx)diff --git a/saleor/dashboard/templates/dashboard/customer/_customers_table.html b/saleor/dashboard/templates/dashboard/customer/_customers_table.htmldeleted file mode 100644index 2cf1eb4007..0000000000--- a/saleor/dashboard/templates/dashboard/customer/_customers_table.html+++ /dev/null@@ -1,43 +0,0 @@-{% load i18n %}-{% load prices_i18n %}--{% if customers %}- <table class="table">- <thead>- <th>{% trans "Email" %}</th>- <th>{% trans "Name" %}</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.address as default_address %}- <td>- <a href="{% url "dashboard:customer-details" pk=customer.pk %}">{{ customer.email }}</a>- </td>- <td>- {{ default_address.first_name }} {{ default_address.last_name }}- </td>- <td>- {{ default_address.city }}, {{ default_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.htmlindex c332a47aea..fcaa8c40d2 100644--- a/saleor/dashboard/templates/dashboard/customer/list.html+++ b/saleor/dashboard/templates/dashboard/customer/list.html@@ -34,31 +34,52 @@<div class="panel panel-default"><div class="panel-heading">- <span class="panel-title">Search results</span>+ <span class="panel-title">{{ title }}</span></div><div class="panel-body">- {% if render_search %}- {% include 'dashboard/customer/_customers_table.html' with customers=customers %}+ {% if customers %}+ <table class="table">+ <thead>+ <th>{% trans "Email" %}</th>+ <th>{% trans "Name" %}</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.address as default_address %}+ <td>+ <a href="{% url "dashboard:customer-details" pk=customer.pk %}">{{ customer.email }}</a>+ </td>+ <td>+ {{ default_address.first_name }} {{ default_address.last_name }}+ </td>+ <td>+ {{ default_address.city }}, {{ default_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>-</span>+ <span>No customers found</span>{% endif %}</div></div></div></div>-- <div class="row">- <div class="col-lg-12">- <div class="panel panel-default">- <div class="panel-heading">- <span class="panel-title">Customers with open orders</span>- </div>- <div class="panel-body">- {% include 'dashboard/customer/_customers_table.html' with customers=have_open_orders %}- </div>- </div>-- </div>- </div>{% endblock %}