]> git.parisson.com Git - django_quiz.git/commitdiff
Merge: Allow the display order of multichoice options to be specified #17
authortomwalker <tomwalker0472@gmail.com>
Wed, 20 Aug 2014 16:17:59 +0000 (17:17 +0100)
committertomwalker <tomwalker0472@gmail.com>
Wed, 20 Aug 2014 16:17:59 +0000 (17:17 +0100)
multichoice/models.py
quiz/admin.py

index ad5ee7250c71ec4fa1f00d4f7ca7cb2d4de6c82a..46d78b5b62818e44f09f6be16104272988b3d349 100644 (file)
@@ -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
index 542a6aaa939871297b1d09a238c868c72b9d945c..9478998a748d1d4c5de7eb546cc56267009f1747 100644 (file)
@@ -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',)