From 214d8696fafee4e38c6bf039315aac37e4cd2e2b Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 21 Jul 2015 18:02:56 +0530 Subject: Add filters to question display --- testapp/exam/forms.py | 13 ++++ testapp/exam/static/exam/js/question_filter.js | 69 ++++++++++++++++++++++ .../exam/templates/exam/ajax_question_filter.html | 15 +++++ testapp/exam/templates/exam/showquestions.html | 23 ++++++++ testapp/exam/urls.py | 2 + testapp/exam/views.py | 42 +++++++++++-- 6 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 testapp/exam/static/exam/js/question_filter.js create mode 100644 testapp/exam/templates/exam/ajax_question_filter.html diff --git a/testapp/exam/forms.py b/testapp/exam/forms.py index b7625be..1c61816 100644 --- a/testapp/exam/forms.py +++ b/testapp/exam/forms.py @@ -240,5 +240,18 @@ class RandomQuestionForm(forms.Form): (choices=(('select', 'Select Marks'),))) shuffle_questions = forms.BooleanField(required=False) + +class QuestionFilterForm(forms.Form): + questions = Question.objects.all() + points_list = questions.values_list('points', flat=True).distinct() + points_options = [(i, i) for i in points_list] + + language = forms.CharField(max_length=8, widget=forms.Select\ + (choices=languages)) + question_type = forms.CharField(max_length=8, widget=forms.Select\ + (choices=question_types)) + marks = forms.FloatField(widget=forms.Select(choices=points_options)) + + TestCaseFormSet = inlineformset_factory(Question, TestCase,\ can_order=False, can_delete=False, extra=1) diff --git a/testapp/exam/static/exam/js/question_filter.js b/testapp/exam/static/exam/js/question_filter.js new file mode 100644 index 0000000..7dc92a6 --- /dev/null +++ b/testapp/exam/static/exam/js/question_filter.js @@ -0,0 +1,69 @@ +$(document).ready(function(){ + $question_type = $("#id_question_type"); + $marks = $("#id_marks"); + $language = $("#id_language"); + + $question_type.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $language.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $marks.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $("#checkall").live("click", function(){ + if($(this).attr("checked")) { + $("#filtered-questions input:checkbox").each(function(index, element) { + $(this).attr('checked','checked'); + }); + } + else { + $("#filtered_questions input:checkbox").each(function(index, element) { + $(this).removeAttr('checked'); + }); + } + }); +}); \ No newline at end of file diff --git a/testapp/exam/templates/exam/ajax_question_filter.html b/testapp/exam/templates/exam/ajax_question_filter.html new file mode 100644 index 0000000..11bf660 --- /dev/null +++ b/testapp/exam/templates/exam/ajax_question_filter.html @@ -0,0 +1,15 @@ +