From 689e777cf21e30b33fd9fccbd6c2cd5dc4d935ea Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 31 May 2017 19:43:52 +0530 Subject: Validate quiz prerequisite field --- yaksh/forms.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'yaksh/forms.py') diff --git a/yaksh/forms.py b/yaksh/forms.py index 3459be9..14a3db0 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -181,9 +181,13 @@ class QuizForm(forms.ModelForm): user = kwargs.pop('user') course_id = kwargs.pop('course') super(QuizForm, self).__init__(*args, **kwargs) - self.fields['prerequisite'] = forms.ModelChoiceField( - queryset=Quiz.objects.filter(course__id=course_id, - is_trial=False)) + + prerequisite_list = Quiz.objects.filter( + course__id=course_id, + is_trial=False + ).exclude(id=self.instance.id) + + self.fields['prerequisite'] = forms.ModelChoiceField(prerequisite_list) self.fields['prerequisite'].required = False self.fields['course'] = forms.ModelChoiceField( queryset=Course.objects.filter(id=course_id), empty_label=None) @@ -240,6 +244,12 @@ class QuizForm(forms.ModelForm):

""") + def clean_prerequisite(self): + prereq = self.cleaned_data['prerequisite'] + if prereq and prereq.prerequisite.id == self.instance.id: + raise forms.ValidationError("Please set another prerequisite quiz") + return None + class Meta: model = Quiz exclude = ["is_trial"] -- cgit From c277c1f742d8d984fc7ab066cd3ac513e83f0d89 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 31 May 2017 19:44:43 +0530 Subject: Fix conditional logic to pass test --- yaksh/forms.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'yaksh/forms.py') diff --git a/yaksh/forms.py b/yaksh/forms.py index 14a3db0..2740497 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -246,9 +246,10 @@ class QuizForm(forms.ModelForm): def clean_prerequisite(self): prereq = self.cleaned_data['prerequisite'] - if prereq and prereq.prerequisite.id == self.instance.id: - raise forms.ValidationError("Please set another prerequisite quiz") - return None + if prereq and prereq.prerequisite: + if prereq.prerequisite.id == self.instance.id: + raise forms.ValidationError("Please set another prerequisite quiz") + return prereq class Meta: model = Quiz -- cgit