Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Reordering attribute choice values functionality
Anna Siwikf02e89b24377
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex e8a1ced230..1b512eac59 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -407,6 +407,26 @@ class AttributeChoiceValueForm(forms.ModelForm):return super().save(commit=commit)+class ReorderAttributeChoiceValuesForm(forms.ModelForm):+ ordered_values = OrderedModelMultipleChoiceField(+ queryset=AttributeChoiceValue.objects.none())++ class Meta:+ model = ProductAttribute+ fields = ()++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)+ if self.instance:+ self.fields['ordered_values'].queryset = self.instance.values.all()++ def save(self):+ for order, value in enumerate(self.cleaned_data['ordered_values']):+ value.sort_order = order+ value.save()+ return self.instance++class ReorderProductImagesForm(forms.ModelForm):ordered_images = OrderedModelMultipleChoiceField(queryset=ProductImage.objects.none())diff --git a/saleor/dashboard/product/urls.py b/saleor/dashboard/product/urls.pyindex 62ecdaccd3..cb54bf45da 100644--- a/saleor/dashboard/product/urls.py+++ b/saleor/dashboard/product/urls.py@@ -75,4 +75,7 @@ urlpatterns = [name='product-attribute-value-update'),url(r'attributes/(?P<attribute_pk>[0-9]+)/value/(?P<value_pk>[0-9]+)/delete/$', # noqaviews.attribute_choice_value_delete,- name='product-attribute-value-delete')]+ name='product-attribute-value-delete'),+ url(r'attributes/(?P<attribute_pk>[0-9]+)/values/reorder/$',+ views.ajax_reorder_attribute_choice_values,+ name='product-attribute-values-reorder')]diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 92e5f27525..2f3d74ee44 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -177,7 +177,6 @@ def product_delete(request, pk):@require_POST@staff_member_required-@permission_required('product.edit_product')def product_bulk_update(request):form = forms.ProductBulkUpdate(request.POST)if form.is_valid():@@ -490,7 +489,6 @@ def product_image_delete(request, product_pk, img_pk):@require_POST@staff_member_required-@permission_required('product.edit_product')def ajax_reorder_product_images(request, product_pk):product = get_object_or_404(Product, pk=product_pk)form = forms.ReorderProductImagesForm(request.POST, instance=product)@@ -654,3 +652,19 @@ def attribute_choice_value_delete(request, attribute_pk, value_pk):request,'dashboard/product/product_attribute/values/modal/confirm_delete.html',{'value': value, 'attribute_pk': attribute_pk})+++@staff_member_required+@permission_required('product.edit_properties')+def ajax_reorder_attribute_choice_values(request, attribute_pk):+ attribute = get_object_or_404(ProductAttribute, pk=attribute_pk)+ form = forms.ReorderAttributeChoiceValuesForm(+ request.POST, instance=attribute)+ status = 200+ ctx = {}+ if form.is_valid():+ form.save()+ elif form.errors:+ status = 400+ ctx = {'error': form.errors}+ return JsonResponse(ctx, status=status)diff --git a/saleor/static/dashboard/js/components/attribute-values.js b/saleor/static/dashboard/js/components/attribute-values.jsnew file mode 100644index 0000000000..5cdd2530fd--- /dev/null+++ b/saleor/static/dashboard/js/components/attribute-values.js@@ -0,0 +1,28 @@+import Sortable from 'sortablejs';+++$('.sortable-items').each(function() {+ Sortable.create(this, {+ handle: '.sortable__drag-area',+ animation: 150,+ onUpdate: () => {+ const sortedItems = $(this)+ .find('.sortable-item[data-id]')+ .map((index, item) => item.dataset.id)+ .toArray();+ const data = {};+ data[$(this).data('post-name')] = sortedItems;++ // TODO: Get rid of ajax() in favour of fetch()+ $.ajax({+ method: 'POST',+ url: $(this).data('post-url'),+ data: data,+ traditional: true,+ headers: {+ 'X-CSRFToken': $.cookie('csrftoken')+ }+ });+ }+ });+});diff --git a/saleor/static/dashboard/js/components/index.js b/saleor/static/dashboard/js/components/index.jsindex 696787d1e6..ec400cf55c 100644--- a/saleor/static/dashboard/js/components/index.js+++ b/saleor/static/dashboard/js/components/index.js@@ -1,4 +1,5 @@import './async-form';+import './attribute-values.js';import './bulk-actions';import './datepicker';import './editor';diff --git a/templates/dashboard/product/product_attribute/detail.html b/templates/dashboard/product/product_attribute/detail.htmlindex c32ffe8cd7..df37618205 100644--- a/templates/dashboard/product/product_attribute/detail.html+++ b/templates/dashboard/product/product_attribute/detail.html@@ -71,6 +71,10 @@<table class="bordered highlight responsive data-table"><thead><tr>+ {% if perms.product.edit_properties %}+ <th class="data-table-icon"></th>+ {% endif %}+<th>{% trans "Display name" %}</th><th>{% trans "Color" %}</th>{% if perms.product.edit_product %}@@ -78,9 +82,14 @@{% endif %}</tr></thead>- <tbody>+ <tbody class="sortable-items" data-post-url="{% url 'dashboard:product-attribute-values-reorder' attribute_pk=attribute.pk %}" data-post-name="ordered_values">{% for value in values %}- <tr>+ <tr class="sortable-item" data-id="{{ value.pk }}">+ {% if perms.product.edit_properties %}+ <td class="data-table-icon ignore-link sortable__drag-area">+ <svg data-src="{% static "dashboard/images/reorder.svg" %}" height="20" width="20" fill="#000" />+ </td>+ {% endif %}<td>{{ value.name }}</td>