From 15eee90c72c9112fd8a0e1f683e4c85bd6b0b96f Mon Sep 17 00:00:00 2001 From: tomwalker Date: Wed, 20 Aug 2014 17:17:59 +0100 Subject: [PATCH] Merge: Allow the display order of multichoice options to be specified #17 --- multichoice/models.py | 23 +++++++++++++++++++++-- quiz/admin.py | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/multichoice/models.py b/multichoice/models.py index ad5ee72..46d78b5 100644 --- a/multichoice/models.py +++ b/multichoice/models.py @@ -1,9 +1,19 @@ from django.db import models from quiz.models import Question +ANSWER_ORDER_OPTIONS = ( + ('content', 'Content'), + ('random', 'Random'), + ('none', 'None'), +) class MCQuestion(Question): + answer_order = models.CharField(max_length=30, null=True, blank=True, + choices=ANSWER_ORDER_OPTIONS, + help_text="The order in which multichoice answer " + "options are displayed to the user") + def check_if_correct(self, guess): answer = Answer.objects.get(id=guess) @@ -12,12 +22,21 @@ class MCQuestion(Question): else: return False + def order_answers(self, queryset): + if self.answer_order == 'content': + return queryset.order_by('content') + if self.answer_order == 'random': + return queryset.order_by('?') + if self.answer_order == 'none': + return queryset.order_by() + return queryset + def get_answers(self): - return Answer.objects.filter(question=self) + return self.order_answers(Answer.objects.filter(question=self)) def get_answers_list(self): return [(answer.id, answer.content) for answer in - Answer.objects.filter(question=self)] + self.order_answers(Answer.objects.filter(question=self))] def answer_choice_to_string(self, guess): return Answer.objects.get(id=guess).content diff --git a/quiz/admin.py b/quiz/admin.py index 542a6aa..9478998 100644 --- a/quiz/admin.py +++ b/quiz/admin.py @@ -66,7 +66,7 @@ class MCQuestionAdmin(admin.ModelAdmin): list_display = ('content', 'category', ) list_filter = ('category',) fields = ('content', 'category', 'sub_category', - 'figure', 'quiz', 'explanation') + 'figure', 'quiz', 'explanation', 'answer_order') search_fields = ('content', 'explanation') filter_horizontal = ('quiz',) -- 2.39.5