-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms.py
46 lines (35 loc) · 1.32 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from django.forms.utils import ValidationError
from classes.models import Class
from subjects.models import Subject
from .models import *
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
fields = ('text', )
class BaseAnswerInlineFormSet(forms.BaseInlineFormSet):
def clean(self):
super().clean()
has_one_correct_answer = False
for form in self.forms:
if not form.cleaned_data.get('DELETE', False):
if form.cleaned_data.get('is_correct', False):
has_one_correct_answer = True
break
if not has_one_correct_answer:
raise ValidationError('Mark at least one answer as correct.', code='no_correct_answer')
class TakeForm(forms.ModelForm):
answer = forms.ModelChoiceField(
queryset=Answer.objects.none(),
widget=forms.RadioSelect(),
required=True,
empty_label=None)
class Meta:
model = StudentAnswer
fields = ('answer', )
def __init__(self, *args, **kwargs):
question = kwargs.pop('question')
super().__init__(*args, **kwargs)
self.fields['answer'].queryset = question.answers.order_by('text')