Skip to content

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

Simplify reordering images by sending only a list of image IDs in target order

Marcin Gębalaa5da3e8938dd
diff --git a/saleor/dashboard/product/api.py b/saleor/dashboard/product/api.py
index 6baef07ddb..5eae80edb3 100644
--- a/saleor/dashboard/product/api.py
+++ b/saleor/dashboard/product/api.py
@@ -11,8 +11,7 @@ from ...product.models import Product
class ReorderProductImagesSerializer(serializers.Serializer):
- pk = serializers.IntegerField()
- order = serializers.IntegerField()
+ order = serializers.ListField(child=serializers.IntegerField())
@staff_member_required
@@ -20,10 +19,9 @@ class ReorderProductImagesSerializer(serializers.Serializer):
@renderer_classes([renderers.JSONRenderer])
def reorder_product_images(request, product_pk):
product = get_object_or_404(Product, pk=product_pk)
- serializer = ReorderProductImagesSerializer(data=request.data['data'], many=True)
+ serializer = ReorderProductImagesSerializer(data=request.data)
if serializer.is_valid():
- for item in serializer.data:
- pk, order = item['pk'], item['order']
+ for order, pk in enumerate(serializer.data['order']):
try:
img = product.images.get(pk=pk)
except ObjectDoesNotExist:
diff --git a/saleor/static/js_src/dashboard.js b/saleor/static/js_src/dashboard.js
index d5fada7158..7952b326eb 100644
--- a/saleor/static/js_src/dashboard.js
+++ b/saleor/static/js_src/dashboard.js
@@ -9,18 +9,16 @@ var sortable = Sortable.create(el, {
onUpdate: function() {
$.ajax({
dataType: 'json',
- data: {
- 'data': (function () {
+ contentType: "application/json",
+ data: JSON.stringify({
+ 'order': (function () {
var postData = [];
$(el).find('.product-gallery-item').each(function (i) {
- postData.push({
- pk: $(this).data('id'),
- order: i
- });
+ postData.push($(this).data('id'));
});
- return JSON.stringify(postData);
+ return postData;
})()
- },
+ }),
headers: {
'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
},