4 from django import forms
5 from django.utils import simplejson
7 from .models import Submission, Attachment, exercises
10 class WTEMForm(forms.ModelForm):
15 def __init__(self, *args, **kwargs):
16 super(WTEMForm, self).__init__(*args, **kwargs)
17 for exercise in exercises:
18 if exercise['type'] != 'file_upload':
20 self.fields['attachment_for_' + str(exercise['id'])] = forms.FileField(required = False)
22 def save(self, commit=True):
23 submission = super(WTEMForm, self).save(commit=commit)
24 for name, file in self.files.items():
25 m = re.match(r'attachment_for_(\d+)(?:__(.*))?', name)
26 exercise_id = int(m.group(1))
27 tag = m.group(2) or None
29 attachment = Attachment.objects.get(submission = submission, exercise_id = exercise_id, tag=tag)
30 except Attachment.DoesNotExist:
31 attachment = Attachment(submission = submission, exercise_id = exercise_id, tag=tag)
32 attachment.file = file