Skip to content

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

Init reorder images form directly with post data

Anna Siwik3b2c3383885b
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 520541ef0e..307c082d5a 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -293,7 +293,8 @@ AttributeChoiceValueFormset = inlineformset_factory(
class OrderedModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def clean(self, value):
qs = super(OrderedModelMultipleChoiceField, self).clean(value)
- return sorted(qs, key=lambda v: value.index(v.pk))
+ keys = list(map(int, value))
+ return sorted(qs, key=lambda v: keys.index(v.pk))
class ReorderProductImagesForm(forms.ModelForm):
@@ -313,6 +314,7 @@ class ReorderProductImagesForm(forms.ModelForm):
for order, image in enumerate(self.cleaned_data['ordered_images']):
image.order = order
image.save()
+ return self.instance
class UploadImageForm(forms.ModelForm):
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 6a105e7652..2ceef435a0 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -562,8 +562,7 @@ def stock_location_delete(request, location_pk):
@staff_member_required
def ajax_reorder_product_images(request, product_pk):
product = get_object_or_404(Product, pk=product_pk)
- ordered_images = json.loads(request.body.decode('utf-8'))
- form = forms.ReorderProductImagesForm(ordered_images, instance=product)
+ form = forms.ReorderProductImagesForm(request.POST, instance=product)
status = 200
ctx = {}
if form.is_valid():
diff --git a/saleor/static/dashboard/js/dashboard.js b/saleor/static/dashboard/js/dashboard.js
index 19498acec0..94e04e075a 100644
--- a/saleor/static/dashboard/js/dashboard.js
+++ b/saleor/static/dashboard/js/dashboard.js
@@ -165,23 +165,21 @@ if (el) {
Sortable.create(el, {
handle: '.sortable__drag-area',
onUpdate: function () {
+ let orderedImages = (function () {
+ let postData = [];
+ $(el).find('.product-gallery-item[data-id]').each(function() {
+ postData.push($(this).data('id'));
+ });
+ return postData;
+ })();
$.ajax({
- dataType: 'json',
- contentType: 'application/json',
- data: JSON.stringify({
- 'ordered_images': (function () {
- let postData = [];
- $(el).find('.product-gallery-item[data-id]').each(function () {
- postData.push($(this).data('id'));
- });
- return postData;
- })()
- }),
+ method: 'post',
+ url: $(el).data('post-url'),
+ data: {ordered_images: orderedImages},
+ traditional: true,
headers: {
'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
- },
- method: 'post',
- url: $(el).data('post-url')
+ }
});
}
});