1 # -*- coding: utf-8 -*-
2 from django import forms
3 from django.conf import settings
4 from django.template.defaultfilters import filesizeformat
6 from stage2.models import Attachment, Mark, FieldOptionSet, FieldOption
9 class AttachmentForm(forms.ModelForm):
10 assignment_id = forms.CharField(widget=forms.HiddenInput)
16 def __init__(self, assignment, file_no, label, extensions=None, *args, **kwargs):
17 prefix = 'att%s-%s' % (assignment.id, file_no)
18 super(AttachmentForm, self).__init__(*args, prefix=prefix, **kwargs)
19 self.fields['assignment_id'].initial = assignment.id
20 self.fields['file'].label = label
22 self.fields['file'].widget.attrs = {'data-ext': '|'.join(extensions)}
23 self.extensions = extensions
26 file = self.cleaned_data['file']
27 if file.size > settings.MAX_UPLOAD_SIZE:
28 raise forms.ValidationError(
29 'Please keep filesize under %s. Current filesize: %s' % (
30 filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(file.size)))
31 if self.extensions and ('.' not in file.name or file.name.rsplit('.', 1)[1].lower() not in self.extensions):
32 raise forms.ValidationError('Incorrect extension, should be one of: %s' % ', '.join(self.extensions))
36 class AssignmentFieldForm(forms.Form):
37 value = forms.CharField(required=False)
38 assignment_id = forms.CharField(widget=forms.HiddenInput)
40 def __init__(self, label, field_no, options, answer, *args, **kwargs):
41 prefix = 'field%s-%s' % (answer.id, field_no)
42 super(AssignmentFieldForm, self).__init__(prefix=prefix, *args, **kwargs)
45 self.fields['value'].label = label
46 self.type = options['type']
47 self.fields['assignment_id'].initial = answer.assignment.id
48 if self.type == 'options':
49 option_set = FieldOptionSet.objects.get(name=options['option_set'])
50 self.fields['value'].widget = forms.Select(choices=option_set.choices(answer))
51 options = answer.fieldoption_set.all()
53 self.fields['value'].initial = options.get().id
55 value = answer.field_values.get(label)
56 self.fields['value'].initial = value or ''
58 def clean_value(self):
59 if self.type == 'options':
60 value = self.cleaned_data['value']
63 option = FieldOption.objects.get(id=int(value))
64 except (FieldOption.DoesNotExist, ValueError):
65 raise forms.ValidationError(u'Nieprawidłowa wartość.')
66 if option.answer != self.answer and option.answer is not None:
67 raise forms.ValidationError(u'Ta opcja została już wybrana przez kogoś innego.')
69 return self.cleaned_data['value']
72 value = self.cleaned_data['value']
73 if self.type == 'options':
76 if option.answer != self.answer:
78 assert not option.answer
79 for opt in self.answer.fieldoption_set.all():
82 option.answer = self.answer
85 for opt in self.answer.fieldoption_set.all():
89 self.answer.field_values[self.label] = value
93 class MarkForm(forms.ModelForm):
98 'points': forms.TextInput(attrs={'type': 'number', 'min': 0, 'step': '0.5'})
101 def __init__(self, answer, *args, **kwargs):
102 super(MarkForm, self).__init__(*args, **kwargs)
104 self.fields['points'].widget.attrs['max'] = answer.assignment.max_points
106 def clean_points(self):
107 points = self.cleaned_data['points']
108 if points > self.answer.assignment.max_points:
109 raise forms.ValidationError('Too many points for this assignment')
111 raise forms.ValidationError('Points cannot be negative')