1 # -*- coding: utf-8 -*-
2 from decimal import Decimal
4 from django import forms
5 from django.conf import settings
6 from django.template.defaultfilters import filesizeformat
7 from django.utils.safestring import mark_safe
9 from stage2.models import Attachment, Mark, FieldOptionSet, FieldOption
12 class AttachmentForm(forms.ModelForm):
13 assignment_id = forms.CharField(widget=forms.HiddenInput)
19 def __init__(self, assignment, file_no, label, extensions=None, *args, **kwargs):
20 prefix = 'att%s-%s' % (assignment.id, file_no)
21 super(AttachmentForm, self).__init__(*args, prefix=prefix, **kwargs)
22 self.fields['assignment_id'].initial = assignment.id
23 self.fields['file'].label = label
25 self.fields['file'].widget.attrs = {'data-ext': '|'.join(extensions)}
26 self.extensions = extensions
29 file = self.cleaned_data['file']
30 if file.size > settings.MAX_UPLOAD_SIZE:
31 raise forms.ValidationError(
32 'Please keep filesize under %s. Current filesize: %s' % (
33 filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(file.size)))
34 if self.extensions and ('.' not in file.name or file.name.rsplit('.', 1)[1].lower() not in self.extensions):
35 raise forms.ValidationError('Incorrect extension, should be one of: %s' % ', '.join(self.extensions))
39 class AssignmentFieldForm(forms.Form):
40 value = forms.CharField(required=False)
41 assignment_id = forms.CharField(widget=forms.HiddenInput)
43 def __init__(self, label, field_no, options, answer, *args, **kwargs):
44 prefix = 'field%s-%s' % (answer.id, field_no)
45 super(AssignmentFieldForm, self).__init__(prefix=prefix, *args, **kwargs)
48 self.fields['value'].label = label
49 self.type = options['type']
50 self.fields['assignment_id'].initial = answer.assignment.id
51 if self.type == 'options':
52 option_set = FieldOptionSet.objects.get(name=options['option_set'])
53 self.fields['value'].widget = forms.Select(choices=option_set.choices(answer))
54 options = answer.fieldoption_set.all()
56 self.fields['value'].initial = options.get().id
58 value = answer.field_values.get(label)
59 self.fields['value'].initial = value or ''
61 def clean_value(self):
62 if self.type == 'options':
63 value = self.cleaned_data['value']
66 option = FieldOption.objects.get(id=int(value))
67 except (FieldOption.DoesNotExist, ValueError):
68 raise forms.ValidationError(u'Nieprawidłowa wartość.')
69 if option.answer != self.answer and option.answer is not None:
70 raise forms.ValidationError(u'Ta opcja została już wybrana przez kogoś innego.')
72 return self.cleaned_data['value']
75 value = self.cleaned_data['value']
76 if self.type == 'options':
79 if option.answer != self.answer:
81 assert not option.answer
82 for opt in self.answer.fieldoption_set.all():
85 option.answer = self.answer
88 for opt in self.answer.fieldoption_set.all():
92 self.answer.field_values[self.label] = value
96 class MarkForm(forms.ModelForm):
97 answer_id = forms.CharField(widget=forms.HiddenInput)
103 'points': forms.TextInput(attrs={'type': 'number', 'min': 0, 'step': '0.5'})
106 def __init__(self, answer, criterion, *args, **kwargs):
107 super(MarkForm, self).__init__(*args, **kwargs)
108 self.fields['answer_id'].initial = answer.id
109 points_field = self.fields['points']
110 points_field.label = mark_safe(criterion.form_label())
111 points_field.help_text = '(max %s)' % criterion.max_points
112 points_field.min_value = Decimal(0)
113 points_field.max_value = Decimal(criterion.max_points)
114 points_field.widget.attrs['max'] = criterion.max_points