Просмотр исходного кода

Re-bind image formset if other product formsets fail to validate

We currently don't hold on to images that are uploaded if any of the
other formsets fail when editing/creating products. To prevent
issue #1126, we need to re-bind the formset.

This behaviour should be improved as part of #856.

This commit was originally suggested in #1126,
re-done onto releases/0.6 in 05c185fc because of merge conflicts,
and now cherry-picked onto master again because it was easier again to
do so instead of fixing the merge conflicts.
master
Maik Hoepfel 12 лет назад
Родитель
Сommit
0b373df5ab
1 измененных файлов: 14 добавлений и 4 удалений
  1. 14
    4
      oscar/apps/dashboard/catalogue/views.py

+ 14
- 4
oscar/apps/dashboard/catalogue/views.py Просмотреть файл

@@ -255,10 +255,6 @@ class ProductCreateUpdateView(generic.UpdateView):
255 255
         if is_valid and cross_form_validation_result:
256 256
             return self.forms_valid(form, formsets)
257 257
         else:
258
-            # Delete the temporary product again
259
-            if self.creating and self.object and self.object.pk is not None:
260
-                self.object.delete()
261
-                self.object = None
262 258
             return self.forms_invalid(form, formsets)
263 259
 
264 260
     # form_valid and form_invalid are called depending on the validation result
@@ -293,6 +289,20 @@ class ProductCreateUpdateView(generic.UpdateView):
293 289
         return HttpResponseRedirect(self.get_success_url())
294 290
 
295 291
     def forms_invalid(self, form, formsets):
292
+        # delete the temporary product again
293
+        if self.creating and self.object and self.object.pk is not None:
294
+            self.object.delete()
295
+            self.object = None
296
+
297
+        # We currently don't hold on to images if the other formsets didn't
298
+        # validate. But as the browser won't re-POST any images, we can do no
299
+        # better than re-bind the image formset, which means the user will
300
+        # have to re-select the images
301
+        image_formset_class = self.formsets.get('image_formset')
302
+        if 'image_formset' in formsets and image_formset_class is not None:
303
+            formsets['image_formset'] = image_formset_class(
304
+                self.product_class, self.request.user, instance=self.object)
305
+
296 306
         messages.error(self.request,
297 307
                        _("Your submitted data was not valid - please "
298 308
                          "correct the below errors"))

Загрузка…
Отмена
Сохранить