banner
[edumed.git] / wtem / forms.py
index 9555c72..ae406a3 100644 (file)
@@ -1,9 +1,9 @@
-import os
+# -*- coding: utf-8 -*-
+import re
 
 from django import forms
-from django.utils import simplejson
 
-from .models import Submission, Attachment
+from .models import Submission, Attachment, exercises
 
 
 class WTEMForm(forms.ModelForm):
@@ -13,20 +13,20 @@ class WTEMForm(forms.ModelForm):
 
     def __init__(self, *args, **kwargs):
         super(WTEMForm, self).__init__(*args, **kwargs)
-
-        ## @@ move this one level up
-        f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
-        exercises = simplejson.loads(f.read())
-        f.close()
-
         for exercise in exercises:
             if exercise['type'] != 'file_upload':
                 continue
-            self.fields['attachment_for_' + str(exercise['id'])] = forms.FileField(required = False)
-
-    def save(self):
-        submission = super(WTEMForm, self).save()
-        for file in self.files.values():
-            attachment = Attachment(file = file, submission = submission)
+            self.fields['attachment_for_' + str(exercise['id'])] = forms.FileField(required=False)
+
+    def save(self, commit=True):
+        submission = super(WTEMForm, self).save(commit=commit)
+        for name, attachment_file in self.files.items():
+            m = re.match(r'attachment_for_(\d+)(?:__(.*))?', name)
+            exercise_id = int(m.group(1))
+            tag = m.group(2) or None
+            try:
+                attachment = Attachment.objects.get(submission=submission, exercise_id=exercise_id, tag=tag)
+            except Attachment.DoesNotExist:
+                attachment = Attachment(submission=submission, exercise_id=exercise_id, tag=tag)
+            attachment.file = attachment_file
             attachment.save()
-