summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/forms.py17
-rw-r--r--yaksh/views.py28
2 files changed, 25 insertions, 20 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py
index 3459be9..2740497 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,13 @@ class QuizForm(forms.ModelForm):
</p>
""")
+ def clean_prerequisite(self):
+ prereq = self.cleaned_data['prerequisite']
+ 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
exclude = ["is_trial"]
diff --git a/yaksh/views.py b/yaksh/views.py
index db45a89..7f907aa 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -256,30 +256,24 @@ def add_quiz(request, course_id, quiz_id=None):
if form.is_valid():
form.save()
return my_redirect("/exam/manage/courses/")
- else:
- context["form"] = form
- return my_render_to_response('yaksh/add_quiz.html',
- context,
- context_instance=ci)
+
else:
quiz = Quiz.objects.get(id=quiz_id)
form = QuizForm(request.POST, user=user, course=course_id,
- instance=quiz)
+ instance=quiz
+ )
if form.is_valid():
form.save()
- context["quiz_id"] = quiz_id
return my_redirect("/exam/manage/courses/")
+
else:
- if quiz_id is None:
- form = QuizForm(course=course_id, user=user)
- else:
- quiz = Quiz.objects.get(id=quiz_id)
- form = QuizForm(user=user,course=course_id, instance=quiz)
- context["quiz_id"] = quiz_id
- context["form"] = form
- return my_render_to_response('yaksh/add_quiz.html',
- context,
- context_instance=ci)
+ quiz = Quiz.objects.get(id=quiz_id) if quiz_id else None
+ form = QuizForm(user=user,course=course_id, instance=quiz)
+ context["quiz_id"] = quiz_id
+ context["form"] = form
+ return my_render_to_response('yaksh/add_quiz.html',
+ context,
+ context_instance=ci)
@login_required
@email_verified