Repository: shuup/shuup

Issue:
Admin: make sure the primary image is set when option is enabled
When you add a new image to the product and check the Primary image checkbox it doesn't work. After you save the product changes, it backs to non-primary image status


Discussion:

Resolving patch:
diff --git a/shuup/admin/modules/products/forms/base_forms.py b/shuup/admin/modules/products/forms/base_forms.py
--- a/shuup/admin/modules/products/forms/base_forms.py
+++ b/shuup/admin/modules/products/forms/base_forms.py
@@ -510,14 +510,22 @@ def save(self, commit=True):
         In addition add the first saved image as primary image for the
         product if none is selected as such.
         """
+
         super(ProductImageMediaFormSet, self).save(commit)
 
-        has_primary = any(form.cleaned_data.get("is_primary") for form in (self.forms or []))
-        eligible_forms = [form for form in (self.forms or []) if
-                          (form.cleaned_data.get("file") and not form.cleaned_data.get("DELETE"))]
+        eligible_forms = [
+            form for form in (self.forms or [])
+            if (form.cleaned_data.get("file") and not form.cleaned_data.get("DELETE"))
+        ]
+        has_primary = any(form.cleaned_data.get("is_primary") for form in eligible_forms)
 
-        if eligible_forms and not has_primary and not self.forms[0].product.primary_image:
+        if eligible_forms and not has_primary:
             # make first form be the primary image as well
-            form_instance = self.forms[0]
+            form_instance = eligible_forms[0]
             form_instance.product.primary_image = form_instance.instance
             form_instance.product.save()
+
+        # removed all images, then, clear the primary image
+        if not eligible_forms and not has_primary:
+            self.product.primary_image = None
+            self.product.save()
diff --git a/shuup/admin/modules/products/views/edit_media.py b/shuup/admin/modules/products/views/edit_media.py
--- a/shuup/admin/modules/products/views/edit_media.py
+++ b/shuup/admin/modules/products/views/edit_media.py
@@ -160,13 +160,28 @@ def post(self, *args, **kwargs):
             if not File.objects.filter(id=file_id).exists():
                 return JsonResponse({"response": "error", "message": "invalid file id: %s" % file_id}, status=400)
 
+        added = []
+
         for file_id in ids:
             if not ProductMedia.objects.filter(
-                    product_id=shop_product.product_id, file_id=file_id, kind=kind, shops__in=[shop_id]).exists():
+                    product_id=shop_product.product_id,
+                    file_id=file_id,
+                    kind=kind,
+                    shops__in=[shop_id]).exists():
                 image = ProductMedia.objects.create(
                     product_id=shop_product.product_id,
                     file_id=file_id,
                     kind=kind,
                 )
                 image.shops.add(shop_id)
-        return JsonResponse({"response": "success", "message": force_text(_("Files added to product."))})
+                added.append({
+                    "product": image.product_id,
+                    "file": int(file_id),
+                    "kind": kind.value,
+                    "product_media": image.pk
+                })
+        return JsonResponse({
+            "response": "success",
+            "added": added,
+            "message": force_text(_("Files added to product."))
+        })


Test patch: