Commits

Yohan Boniface  committed 6685cfc

Use savepoints in UploadData instead of one transaction per loop (fix #55)

  • Participants
  • Parent commits db73660
  • Tags 0.2.0

Comments (0)

Files changed (1)

File leaflet_storage/views.py

         })
         return super(UploadData, self).get_context_data(**kwargs)
 
+    @transaction.commit_manually
     def form_valid(self, form):
         FEATURE_TO_MODEL = {
             'Point': Marker,
         category = form.cleaned_data.get('category')
         counter = 0
         for feature in features:
+            sid = transaction.savepoint()
             klass = FEATURE_TO_MODEL.get(feature.geometry['type'], None)
             if not klass:
                 continue  # TODO notify user
                                 kwargs['options'] = {}
                             kwargs['options'][name] = value
                         break
-            with transaction.commit_manually():
-                try:
-                    klass.objects.create(**kwargs)
-                except DatabaseError:
-                    transaction.rollback()
-                    continue  # TODO notify user
-                else:
-                    transaction.commit()
+            try:
+                klass.objects.create(**kwargs)
+            except DatabaseError:
+                transaction.savepoint_rollback(sid)
+                continue  # TODO notify user
+            else:
+                transaction.savepoint_commit(sid)
             counter += 1
+        transaction.commit()
         kwargs = {
             'category': category.json,
             'info': "%d features created!" % counter,