Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fetch products available for voucher by ajax select 2
Anna Siwik428f473e7999
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 237ec87bd0..7e326e229d 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -4,10 +4,13 @@ import uuidfrom django import formsfrom django.conf import settings+from django.urls import reverse_lazyfrom django.utils.translation import pgettext_lazyfrom django_prices.forms import PriceField+from ...core.forms import AjaxSelect2ChoiceFieldfrom ...discount.models import Sale, Voucher+from ...product.models import Productfrom ...shipping.models import ShippingMethodCountry, COUNTRY_CODE_CHOICES@@ -125,6 +128,10 @@ class CommonVoucherForm(forms.ModelForm):class ProductVoucherForm(CommonVoucherForm):+ product = AjaxSelect2ChoiceField(+ queryset=Product.objects.get_available_products(),+ fetch_data_url=reverse_lazy('dashboard:ajax-available-products'),+ required=True)class Meta:model = Voucher@@ -132,7 +139,8 @@ class ProductVoucherForm(CommonVoucherForm):def __init__(self, *args, **kwargs):super(ProductVoucherForm, self).__init__(*args, **kwargs)- self.fields['product'].required = True+ if self.instance.product:+ self.fields['product'].set_initial(self.instance.product)class CategoryVoucherForm(CommonVoucherForm):diff --git a/saleor/dashboard/product/urls.py b/saleor/dashboard/product/urls.pyindex 2237c12092..b12489978d 100644--- a/saleor/dashboard/product/urls.py+++ b/saleor/dashboard/product/urls.py@@ -98,4 +98,6 @@ urlpatterns = [url(r'^ajax/variants/$',views.ajax_available_variants_list, name='ajax-available-variants'),+ url(r'^ajax/products/$',+ views.ajax_available_products_list, name='ajax-available-products'),]diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 1c9338606f..0d0a320fef 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -711,3 +711,21 @@ def ajax_available_variants_list(request):for variant in queryset]return JsonResponse({'results': variants})+++@staff_member_required+def ajax_available_products_list(request):+ """+ Returns products list filtered by request GET parameters.+ Response format is as required by select2 field.+ """++ queryset = Product.objects.get_available_products()+ search_query = request.GET.get('q', '')+ if search_query:+ queryset = queryset.filter(Q(name__icontains=search_query))+ products = [+ {'id': product.id, 'text': str(product)}+ for product in queryset+ ]+ return JsonResponse({'results': products})