+
+class WTEMSingleForm(forms.ModelForm):
+ answers = forms.CharField()
+
+ class Meta:
+ model = Submission
+ fields = []
+
+ def __init__(self, *args, **kwargs):
+ super(WTEMSingleForm, self).__init__(*args, **kwargs)
+ i, exercise = self.instance.current_exercise()
+ if exercise and exercise['type'] == 'file_upload':
+ self.fields['attachment'] = forms.FileField(required=False)
+
+ def save(self, commit=True):
+ submission = self.instance
+ answers = submission.get_answers()
+ posted_answers = json.loads(self.cleaned_data['answers'])
+ if type(posted_answers) != dict:
+ raise ValueError('answers not dict')
+ if len(posted_answers) != 1:
+ raise ValueError('answers not single')
+ exercise_id, answer = posted_answers.items()[0]
+ # multipost
+ if answers.get(exercise_id) == answer:
+ return
+ i, exercise = submission.current_exercise()
+ if exercise_id != str(exercise['id']):
+ raise ValueError('wrong exercise id')
+ for answer in posted_answers.values():
+ answers[exercise_id] = answer
+ submission.answers = json.dumps(answers)
+ submission.save()
+ for name, attachment_file in self.files.items():
+ m = re.match(r'attachment(?:__(.*))?', name)
+ tag = m.group(1) or None
+ attachment, created = Attachment.objects.get_or_create(
+ submission=submission, exercise_id=exercise_id, tag=tag)
+ attachment.file = attachment_file
+ attachment.save()