1. Jernej Kos
  2. teapoll

Commits

Jernej Kos  committed 8d0fe5f

Use savepoints when saving answers.

  • Participants
  • Parent commits 48b207f
  • Branches default

Comments (0)

Files changed (1)

File survey/models.py

View file
 from django import forms, template
-from django.db import models
+from django.db import models, transaction
 from django.contrib.auth.models import User
 
 import polymorphic
     super(Survey, self).save(*args, **kwargs)
 
   def submit(self, data, subject = None, user = None):
-    if subject is None:
-      subject = Subject(created_by = user)
-      subject.save()
+    try:
+      sid = transaction.savepoint()
+      if subject is None:
+        subject = Subject(created_by = user)
+        subject.save()
 
-    for question in self.questions.all():
-      if question.get_form() is None:
-        continue
+      for question in self.questions.all():
+        if question.get_form() is None:
+          continue
+        
+        try:
+          answer = Answer.objects.get(subject = subject, survey = self, question = question)
+        except Answer.DoesNotExist:
+          answer = Answer(subject = subject, survey = self, question = question)
+
+        answer.data = simplejson.dumps(question.submit(data))
+        answer.save()
       
-      try:
-        answer = Answer.objects.get(subject = subject, survey = self, question = question)
-      except Answer.DoesNotExist:
-        answer = Answer(subject = subject, survey = self, question = question)
-
-      answer.data = simplejson.dumps(question.submit(data))
-      answer.save()
-
+      transaction.savepoint_commit(sid)
+    except:
+      transaction.savepoint_rollback(sid)
+    
     return subject
 
   def get_subjects(self):