ソースを参照

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年前
コミット
0b373df5ab
1個のファイルの変更14行の追加4行の削除
  1. 14
    4
      oscar/apps/dashboard/catalogue/views.py

+ 14
- 4
oscar/apps/dashboard/catalogue/views.py ファイルの表示

255
         if is_valid and cross_form_validation_result:
255
         if is_valid and cross_form_validation_result:
256
             return self.forms_valid(form, formsets)
256
             return self.forms_valid(form, formsets)
257
         else:
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
             return self.forms_invalid(form, formsets)
258
             return self.forms_invalid(form, formsets)
263
 
259
 
264
     # form_valid and form_invalid are called depending on the validation result
260
     # form_valid and form_invalid are called depending on the validation result
293
         return HttpResponseRedirect(self.get_success_url())
289
         return HttpResponseRedirect(self.get_success_url())
294
 
290
 
295
     def forms_invalid(self, form, formsets):
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
         messages.error(self.request,
306
         messages.error(self.request,
297
                        _("Your submitted data was not valid - please "
307
                        _("Your submitted data was not valid - please "
298
                          "correct the below errors"))
308
                          "correct the below errors"))

読み込み中…
キャンセル
保存