fix stupid bug with determining the number of marks
[edumed.git] / stage2 / forms.py
index b956a23..2ef10e5 100644 (file)
@@ -1,7 +1,10 @@
 # -*- coding: utf-8 -*-
+from decimal import Decimal
+
 from django import forms
 from django.conf import settings
 from django.template.defaultfilters import filesizeformat
+from django.utils.safestring import mark_safe
 
 from stage2.models import Attachment, Mark, FieldOptionSet, FieldOption
 
@@ -59,7 +62,10 @@ class AssignmentFieldForm(forms.Form):
         if self.type == 'options':
             value = self.cleaned_data['value']
             if value:
-                option = FieldOption.objects.get(id=int(value))
+                try:
+                    option = FieldOption.objects.get(id=int(value))
+                except (FieldOption.DoesNotExist, ValueError):
+                    raise forms.ValidationError(u'Nieprawidłowa wartość.')
                 if option.answer != self.answer and option.answer is not None:
                     raise forms.ValidationError(u'Ta opcja została już wybrana przez kogoś innego.')
                 return option
@@ -80,14 +86,16 @@ class AssignmentFieldForm(forms.Form):
                     option.save()
             else:
                 for opt in self.answer.fieldoption_set.all():
-                        opt.answer = None
-                        opt.save()
+                    opt.answer = None
+                    opt.save()
         else:
             self.answer.field_values[self.label] = value
             self.answer.save()
 
 
 class MarkForm(forms.ModelForm):
+    answer_id = forms.CharField(widget=forms.HiddenInput)
+
     class Meta:
         model = Mark
         fields = ['points']
@@ -95,15 +103,12 @@ class MarkForm(forms.ModelForm):
             'points': forms.TextInput(attrs={'type': 'number', 'min': 0, 'step': '0.5'})
         }
 
-    def __init__(self, answer, *args, **kwargs):
+    def __init__(self, answer, criterion, *args, **kwargs):
         super(MarkForm, self).__init__(*args, **kwargs)
-        self.answer = answer
-        self.fields['points'].widget.attrs['max'] = answer.assignment.max_points
-
-    def clean_points(self):
-        points = self.cleaned_data['points']
-        if points > self.answer.assignment.max_points:
-            raise forms.ValidationError('Too many points for this assignment')
-        if points < 0:
-            raise forms.ValidationError('Points cannot be negative')
-        return points
+        self.fields['answer_id'].initial = answer.id
+        points_field = self.fields['points']
+        points_field.label = mark_safe(criterion.form_label())
+        points_field.help_text = '(max %s)' % criterion.max_points
+        points_field.min_value = Decimal(0)
+        points_field.max_value = Decimal(criterion.max_points)
+        points_field.widget.attrs['max'] = criterion.max_points