Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move customer search logic from view to form
Marcin Gębalad377f9f02c7a
diff --git a/saleor/dashboard/customer/forms.py b/saleor/dashboard/customer/forms.pyindex 047fb4da32..13da7439e9 100644--- a/saleor/dashboard/customer/forms.py+++ b/saleor/dashboard/customer/forms.py@@ -1,4 +1,5 @@from django import forms+from django.db.models import Qfrom django.utils.translation import ugettext_lazy as _@@ -6,6 +7,10 @@ class CustomerSearchForm(forms.Form):email = forms.CharField(required=False, label=_('Email'))name = forms.CharField(required=False, label=_('Name'))+ def __init__(self, *args, **kwargs):+ self.queryset = kwargs.pop('queryset')+ super(CustomerSearchForm, self).__init__(*args, **kwargs)+def clean(self):data = self.cleaned_dataif not any(data.values()):@@ -14,3 +19,21 @@ class CustomerSearchForm(forms.Form):for field in data.keys():data[field] = data[field].strip()return data++ def search(self):+ queryset = self.queryset+ data = self.cleaned_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__icontains=data['name']))+ queryset = queryset.filter(query).distinct()+ return querysetdiff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.pyindex 8bb39650bf..ae6622e83e 100644--- a/saleor/dashboard/customer/views.py+++ b/saleor/dashboard/customer/views.py@@ -1,5 +1,5 @@from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger-from django.db.models import Count, Max, Q+from django.db.models import Count, Maxfrom django.shortcuts import get_object_or_404from django.template.response import TemplateResponsefrom django.utils.translation import ugettext_lazy as _@@ -9,23 +9,6 @@ from ..views import staff_member_requiredfrom ...userprofile.models import User-def _search_customers(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 _get_users_with_open_orders(queryset):open_order = ['new', 'payment-pending', 'fully-paid']return queryset.filter(orders__status__in=open_order)@@ -36,15 +19,16 @@ 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.GET or None)++ form = CustomerSearchForm(request.GET or None, queryset=customers)if form.is_valid():- customers = _search_customers(customers, form.cleaned_data)- title = _('Search results')+ customers = form.search()+ title = _('Search results (%s)' % len(customers))else:customers = _get_users_with_open_orders(customers)- title = _('Customers with open orders')+ title = _('Customers with open orders (%s)' % len(customers))- paginator = Paginator(customers, 5)+ paginator = Paginator(customers, 30)try:customers = paginator.page(request.GET.get('page'))except PageNotAnInteger: