From dbab99bc13b8483c24706e47a9a0926508e3c332 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 23 Jul 2015 16:32:13 +0530 Subject: Add filters: Minor fixes --- testapp/exam/forms.py | 12 +++++--- testapp/exam/static/exam/js/question_filter.js | 42 ++++++-------------------- testapp/exam/views.py | 2 +- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/testapp/exam/forms.py b/testapp/exam/forms.py index 1c61816..8739bfb 100644 --- a/testapp/exam/forms.py +++ b/testapp/exam/forms.py @@ -242,15 +242,19 @@ class RandomQuestionForm(forms.Form): 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] + def __init__(self, *args, **kwargs): + super(QuestionFilterForm, self).__init__(*args, **kwargs) + questions = Question.objects.all() + points_list = questions.values_list('points', flat=True).distinct() + points_options = [('select', 'Select Marks')] + points_options.extend([(point, point) for point in points_list]) + self.fields['marks'] = forms.FloatField(widget=forms.Select\ + (choices=points_options)) 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,\ diff --git a/testapp/exam/static/exam/js/question_filter.js b/testapp/exam/static/exam/js/question_filter.js index 7dc92a6..065b06b 100644 --- a/testapp/exam/static/exam/js/question_filter.js +++ b/testapp/exam/static/exam/js/question_filter.js @@ -3,7 +3,7 @@ $(document).ready(function(){ $marks = $("#id_marks"); $language = $("#id_language"); - $question_type.change(function() { + function question_filter() { $.ajax({ url: "/exam/ajax/questions/filter/", type: "POST", @@ -18,51 +18,29 @@ $(document).ready(function(){ $("#filtered-questions").html(questions); } }); + } + + $question_type.change(function() { + question_filter() }); $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); - } - }); + question_filter() }); $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); - } - }); + question_filter() }); $("#checkall").live("click", function(){ if($(this).attr("checked")) { $("#filtered-questions input:checkbox").each(function(index, element) { - $(this).attr('checked','checked'); + $(this).attr('checked', true); }); } else { - $("#filtered_questions input:checkbox").each(function(index, element) { - $(this).removeAttr('checked'); + $("#filtered-questions input:checkbox").each(function(index, element) { + $(this).attr('checked', false); }); } }); diff --git a/testapp/exam/views.py b/testapp/exam/views.py index e1c9dc5..f39f3c1 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -1262,7 +1262,7 @@ def ajax_questions_filter(request): if question_type != "select": filter_dict['type'] = str(question_type) - if marks != "": + if marks != "select": filter_dict['points'] = marks if language != "select": -- cgit