Skip to content

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

Fetch available customers list by ajax

Anna Siwikc8f7287fcffe
diff --git a/saleor/account/models.py b/saleor/account/models.py
index aa8c97bfa3..eadb7fab83 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -125,3 +125,11 @@ class User(PermissionsMixin, AbstractBaseUser):
def get_short_name(self):
return self.email
+
+ @property
+ def label(self):
+ address = self.default_billing_address
+ if address:
+ return '%s %s (%s)' % (
+ address.first_name, address.last_name, self.email)
+ return self.email
diff --git a/saleor/dashboard/customer/urls.py b/saleor/dashboard/customer/urls.py
index c13fe90191..8e9f1db1e3 100644
--- a/saleor/dashboard/customer/urls.py
+++ b/saleor/dashboard/customer/urls.py
@@ -7,4 +7,5 @@ urlpatterns = [
url(r'^create/$', views.customer_create, name='customer-create'),
url(r'^(?P<pk>[0-9]+)/$', views.customer_details, name='customer-details'),
url(r'^(?P<pk>[0-9]+)/update/$', views.customer_edit,
- name='customer-update')]
+ name='customer-update'),
+ url(r'^ajax/users/$', views.ajax_users_list, name='ajax-users-list')]
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index 11481a91cb..bc10d19005 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -2,6 +2,7 @@ from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.db.models import Q
+from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.translation import pgettext_lazy
@@ -76,3 +77,19 @@ def customer_edit(request, pk=None):
return redirect('dashboard:customer-details', pk=customer.pk)
ctx = {'form': form, 'customer': customer}
return TemplateResponse(request, 'dashboard/customer/form.html', ctx)
+
+
+@staff_member_required
+@permission_required('account.view_user')
+def ajax_users_list(request):
+ queryset = User.objects.select_related('default_billing_address')
+ search_query = request.GET.get('q', '')
+ if search_query:
+ queryset = queryset.filter(
+ Q(default_billing_address__first_name__icontains=search_query) |
+ Q(default_billing_address__last_name__icontains=search_query) |
+ Q(email__icontains=search_query))
+
+ users = [
+ {'id': user.pk, 'text': user.label} for user in queryset]
+ return JsonResponse({'results': users})
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index c4750004ac..250a630fbf 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -8,6 +8,7 @@ from payments import PaymentError, PaymentStatus
from ...account.i18n import (
AddressForm as StorefrontAddressForm, PossiblePhoneNumberFormField)
+from ...account.models import User
from ...cart.forms import QuantityField
from ...core.exceptions import InsufficientStock
from ...core.utils import ZERO_TAXED_MONEY
@@ -99,6 +100,10 @@ class OrderCustomerForm(forms.ModelForm):
'Update an order with user default addresses',
'Update billing and shipping address'),
initial=True, required=False)
+ user = AjaxSelect2ChoiceField(
+ queryset=User.objects.all(),
+ fetch_data_url=reverse_lazy('dashboard:ajax-users-list'),
+ required=False)
class Meta:
model = Order
@@ -112,6 +117,12 @@ class OrderCustomerForm(forms.ModelForm):
'Order customer email',
'Email')}
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ if self.instance.user:
+ self.fields['user'].set_initial(
+ self.instane.user, label=self.instance.user.label)
+
def clean(self):
cleaned_data = super().clean()
user_email = cleaned_data.get('user_email')