b1bcac9eb5dd4a78973ae4ce7d05c1a4212da8ee
[edumed.git] / stage2 / forms.py
1 # -*- coding: utf-8 -*-
2 from django import forms
3 from django.conf import settings
4 from django.template.defaultfilters import filesizeformat
5
6 from stage2.models import Attachment, Mark, FieldOptionSet, FieldOption
7
8
9 class AttachmentForm(forms.ModelForm):
10     assignment_id = forms.CharField(widget=forms.HiddenInput)
11
12     class Meta:
13         model = Attachment
14         fields = ['file']
15
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
21         if extensions:
22             self.fields['file'].widget.attrs = {'data-ext': '|'.join(extensions)}
23         self.extensions = extensions
24
25     def clean_file(self):
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))
33         return file
34
35
36 class AssignmentFieldForm(forms.Form):
37     value = forms.CharField(required=False)
38     assignment_id = forms.CharField(widget=forms.HiddenInput)
39
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)
43         self.answer = answer
44         self.label = label
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()
52             if options:
53                 self.fields['value'].initial = options.get().id
54         else:
55             value = answer.field_values.get(label)
56             self.fields['value'].initial = value or ''
57
58     def clean_value(self):
59         if self.type == 'options':
60             value = self.cleaned_data['value']
61             if value:
62                 try:
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.')
68                 return option
69         return self.cleaned_data['value']
70
71     def save(self):
72         value = self.cleaned_data['value']
73         if self.type == 'options':
74             option = value
75             if option:
76                 if option.answer != self.answer:
77                     # not thread-safe :/
78                     assert not option.answer
79                     for opt in self.answer.fieldoption_set.all():
80                         opt.answer = None
81                         opt.save()
82                     option.answer = self.answer
83                     option.save()
84             else:
85                 for opt in self.answer.fieldoption_set.all():
86                     opt.answer = None
87                     opt.save()
88         else:
89             self.answer.field_values[self.label] = value
90             self.answer.save()
91
92
93 class MarkForm(forms.ModelForm):
94     class Meta:
95         model = Mark
96         fields = ['points']
97         widgets = {
98             'points': forms.TextInput(attrs={'type': 'number', 'min': 0, 'step': '0.5'})
99         }
100
101     def __init__(self, answer, *args, **kwargs):
102         super(MarkForm, self).__init__(*args, **kwargs)
103         self.answer = answer
104         self.fields['points'].widget.attrs['max'] = answer.assignment.max_points
105
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')
110         if points < 0:
111             raise forms.ValidationError('Points cannot be negative')
112         return points