]> git.parisson.com Git - django_quiz.git/commitdiff
changed order of view functions to see user and anon related views
authorTom Walker <tomwalker0472@gmail.com>
Tue, 8 Jul 2014 16:58:55 +0000 (17:58 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Tue, 8 Jul 2014 16:58:55 +0000 (17:58 +0100)
easier. Cleaned up question template. Return a question instead of ID in
sitting.get_first_question().

quiz/models.py
quiz/templates/question.html
quiz/tests.py
quiz/views.py

index 36be0853136218faf86716033848984ea627123e..77de0ec563da030cc332d9387af29f772800fd3c 100644 (file)
@@ -351,8 +351,9 @@ class Sitting(models.Model):
         first_comma = self.question_list.find(',')
         if first_comma == -1 or first_comma == 0:
             return False
-
-        return int(self.question_list[:first_comma])
+        question_id = int(self.question_list[:first_comma])
+        question = Question.objects.get_subclass(id=question_id)
+        return question
 
     def remove_first_question(self):
         first_comma = self.question_list.find(',')
index 382846a8a7fb7fbbe3c92e89fb3801290ac21772..f45a1b4a6668b6247902b8bfcac51e6c68f49633 100644 (file)
 
 {% if previous %}
 
-        <p class="muted"><small>The previous question:</small></p>
-        <p>{{ previous.previous_question }}</p>
+  <p class="muted"><small>The previous question:</small></p>
+  <p>{{ previous.previous_question }}</p>
 
-        {% if previous.previous_outcome %}
-                       <div class="alert alert-success">
-               {% else %}
-                       <div class="alert alert-warning">
-               {% endif %}
+  {% if previous.previous_outcome %}
+       <div class="alert alert-success">
+  {% else %}
+       <div class="alert alert-warning">
+  {% endif %}
+         <p><small>
+               Your answer was </small>
+               <strong>{{ previous.previous_outcome }}</strong>
+         </p>
 
-                       <p><small>Your answer was </small><strong>{{ previous.previous_outcome }}</strong></p>
-                   </div>
+       </div>
 
-                       {% correct_answer previous %}
-                   <p><strong>Explanation:</strong></p>
-                   <div class="well " style="background-color: #fcf8e3;">
-                       <p>{{ previous.previous_question.explanation }}</p>
-                   </div>
+       {% correct_answer previous %}
 
+       <p><strong>Explanation:</strong></p>
+       <div class="well " style="background-color: #fcf8e3;">
+         <p>{{ previous.previous_question.explanation }}</p>
+       </div>
 
-<hr>
+       <hr>
 
 {% endif %}
 
 <br />
 
-
 {% if question %}
 
-       <p><small class="muted">Question category:</small> <strong>{{ question.category }}</strong></p>
-        <p class="lead">{{ question.content }}</p>
+<p>
+  <small class="muted">Question category:</small>
+  <strong>{{ question.category }}</strong>
+</p>
 
-               <form action="{% url 'quiz_question' quiz.url %}" method="get">
-                 <input type=hidden name="question_id" value="{{ question.id }}">
+<p class="lead">{{ question.content }}</p>
 
-                 <table class="table table-hover table-bordered" id="answerchoice">
-                       <tbody>
+<form action="{% url 'quiz_question' quiz.url %}" method="get">
+  <input type=hidden name="question_id" value="{{ question.id }}">
 
-                         {% ifequal question_type 'TF_Question' %}
-                             {% include 'answers_for_tf_question.html' %}
-                         {% endifequal %}
+  <table class="table table-hover table-bordered" id="answerchoice">
+       <tbody>
 
-                         {% ifequal question_type 'MCQuestion' %}
-                         {% answers_for_mc_question question %}
-                         {% endifequal %}
+         {% ifequal question_type 'TF_Question' %}
+           {% include 'answers_for_tf_question.html' %}
+         {% endifequal %}
 
-                       </tbody>
-                 </table>
-                 <input type="submit" value="Check" class="btn btn-large btn-block btn-warning" >
-               </form>
+         {% ifequal question_type 'MCQuestion' %}
+           {% answers_for_mc_question question %}
+         {% endifequal %}
 
+       </tbody>
+  </table>
+  <input type="submit" value="Check" class="btn btn-large btn-block btn-warning" >
+</form>
 
 {% endif %}
 
-    <hr>
-
-
-
+<hr>
 </div>
 
 {% endblock %}
index 67860a6b2b2a1136174280154dc2403599e60163..2bd7ca819db2f9474cc9b24b8163ec917971f903 100644 (file)
@@ -161,10 +161,12 @@ class TestSitting(TestCase):
         self.sitting = Sitting.objects.new_sitting(self.user, self.quiz1)
 
     def test_get_next_remove_first(self):
-        self.assertEqual(self.sitting.get_first_question(), 1)
+        self.assertEqual(self.sitting.get_first_question(),
+                         self.question1)
 
         self.sitting.remove_first_question()
-        self.assertEqual(self.sitting.get_first_question(), 2)
+        self.assertEqual(self.sitting.get_first_question(),
+                         self.question2)
 
         self.sitting.remove_first_question()
         self.assertEqual(self.sitting.get_first_question(), False)
@@ -503,7 +505,7 @@ class TestQuestionViewsUser(TestCase):
         response = self.client.get('/q/tq1/take/',
                                    {'guess': '123',
                                     'question_id':
-                                    next_question})
+                                    next_question.id})
 
         sitting = Sitting.objects.get(quiz=self.quiz1)
         progress_count = Progress.objects.count()
index a6bc4d5b5cb50426069df6a1ac883ed69aab195f..87fae5a2b15b5cdb3ed1930ef1bba7f08bd29f92 100644 (file)
@@ -64,40 +64,145 @@ def quiz_take(request, quiz_name):
     quiz = Quiz.objects.get(url=quiz_name.lower())
 
     if request.user.is_authenticated() is True:
+        return user_load_sitting(request, quiz)
 
-        if quiz.single_attempt is True and\
-           Sitting.objects.filter(user=request.user,
-                                  quiz=quiz,
-                                  complete=True)\
-                          .count() > 0:
-            return render(request, 'single_complete.html')
+    else:  # anon user
+        return anon_load_sitting(request, quiz)
 
-        try:
-            sitting = Sitting.objects.get(user=request.user,
-                                          quiz=quiz,
-                                          complete=False)
 
-        except Sitting.DoesNotExist:
-            sitting = Sitting.objects.new_sitting(request.user, quiz)
+def user_load_sitting(request, quiz):
+    if quiz.single_attempt is True and\
+       Sitting.objects.filter(user=request.user,
+                              quiz=quiz,
+                              complete=True)\
+                      .count() > 0:
 
-        except Sitting.MultipleObjectsReturned:
-            sitting = Sitting.objects.filter(user=request.user,
-                                             quiz=quiz,
-                                             complete=False)[0]
+        return render(request, 'single_complete.html')
 
-        finally:
-            return user_load_next_question(request, sitting, quiz)
+    try:
+        sitting = Sitting.objects.get(user=request.user,
+                                      quiz=quiz,
+                                      complete=False)
 
-    else:  # anon user
-        if quiz.single_attempt is True:
-            return render(request, 'single_complete.html')
-        quiz_id = str(quiz.id)
-        q_list = quiz_id + "_q_list"
+    except Sitting.DoesNotExist:
+        sitting = Sitting.objects.new_sitting(request.user, quiz)
+
+    except Sitting.MultipleObjectsReturned:
+        sitting = Sitting.objects.filter(user=request.user,
+                                         quiz=quiz,
+                                         complete=False)[0]
+
+    finally:
+        return user_load_next_question(request, sitting, quiz)
+
+
+def user_load_next_question(request, sitting, quiz):
+    previous = {}
+    if 'guess' in request.GET:
+        progress, created = Progress.objects.get_or_create(user=request.user)
+        guess = request.GET['guess']
+        question = sitting.get_first_question()
+        is_correct = question.check_if_correct(guess)
+
+        if is_correct is True:
+            sitting.add_to_score(1)
+            progress.update_score(question.category, 1, 1)
 
-        if q_list in request.session:
-            return load_anon_next_question(request, quiz)
         else:
-            return new_anon_quiz_session(request, quiz)
+            sitting.add_incorrect_question(question)
+            progress.update_score(question.category, 0, 1)
+
+        if quiz.answers_at_end is not True:
+            previous = {'previous_answer': guess,
+                        'previous_outcome': is_correct,
+                        'previous_question': question}
+
+        sitting.remove_first_question()
+
+    next_question = sitting.get_first_question()
+    if next_question is False:
+        #  no questions left
+        return final_result_user(request, sitting, previous)
+
+    question_type = next_question.__class__.__name__
+
+    return render_to_response('question.html',
+                              {'quiz': quiz,
+                               'question': next_question,
+                               'question_type': question_type,
+                               'previous': previous},
+                              context_instance=RequestContext(request))
+
+
+def final_result_user(request, sitting, previous):
+    quiz = sitting.quiz
+    score = sitting.get_current_score()
+    incorrect = sitting.get_incorrect_questions()
+    max_score = quiz.question_set.all().select_subclasses().count()
+    percent = sitting.get_percent_correct()
+
+    sitting.mark_quiz_complete()
+
+    if quiz.exam_paper is False:  # if we do not plan to store the outcome
+        sitting.delete()
+
+    if quiz.answers_at_end is False:
+        return render_to_response('result.html',
+                                  {'quiz': quiz,
+                                   'score': score,
+                                   'max_score': max_score,
+                                   'percent': percent,
+                                   'previous': previous},
+                                  context_instance=RequestContext(request))
+    else:
+        questions = quiz.question_set.all().select_subclasses()
+        return render_to_response('result.html',
+                                  {'quiz': quiz,
+                                   'score': score,
+                                   'max_score': max_score,
+                                   'percent': percent,
+                                   'questions': questions,
+                                   'incorrect_questions': incorrect},
+                                  context_instance=RequestContext(request))
+
+
+def anon_load_sitting(request, quiz):
+    if quiz.single_attempt is True:
+        return render(request, 'single_complete.html')
+    quiz_id = str(quiz.id)
+    q_list = quiz_id + "_q_list"
+
+    if q_list in request.session:
+        return load_anon_next_question(request, quiz)
+    else:
+        return new_anon_quiz_session(request, quiz)
+
+
+def anon_session_score(request, add=0, possible=0):
+    """
+    Returns the session score for non-signed in users.
+    If number passed in then add this to the running total and
+    return session score
+
+    examples:
+        x, y = anon_session_score(1, 1) will add 1 out of a possible 1
+        x, y = anon_session_score(0, 2) will add 0 out of a possible 2
+        x, y = anon_session_score()     will return the session score
+                                        without modification
+    """
+    if "session_score" not in request.session:
+        request.session["session_score"] = 0
+        request.session["session_score_possible"] = 0
+
+    if possible > 0:
+        request.session["session_score"] = (request.session["session_score"] +
+                                            add)
+
+        request.session["session_score_possible"] = \
+            (request.session["session_score_possible"] + possible)
+
+    return request.session["session_score"], \
+        request.session["session_score_possible"]
 
 
 def new_anon_quiz_session(request, quiz):
@@ -152,47 +257,6 @@ def load_anon_next_question(request, quiz):
                               context_instance=RequestContext(request))
 
 
-def user_load_next_question(request, sitting, quiz):
-    previous = {}
-    if 'guess' in request.GET:
-        progress, created = Progress.objects.get_or_create(user=request.user)
-        guess = request.GET['guess']
-        previous_question_id = sitting.get_first_question()
-        question = Question.objects.get_subclass(id=previous_question_id)
-        is_correct = question.check_if_correct(guess)
-
-        if is_correct is True:
-            sitting.add_to_score(1)
-            progress.update_score(question.category, 1, 1)
-
-        else:
-            sitting.add_incorrect_question(question)
-            progress.update_score(question.category, 0, 1)
-
-        if quiz.answers_at_end is not True:
-            previous = {'previous_answer': guess,
-                        'previous_outcome': is_correct,
-                        'previous_question': question}
-
-        sitting.remove_first_question()
-
-    question_ID = sitting.get_first_question()
-
-    if question_ID is False:
-        #  no questions left
-        return final_result_user(request, sitting, previous)
-
-    next_question = Question.objects.get_subclass(id=question_ID)
-    question_type = next_question.__class__.__name__
-
-    return render_to_response('question.html',
-                              {'quiz': quiz,
-                               'question': next_question,
-                               'question_type': question_type,
-                               'previous': previous},
-                              context_instance=RequestContext(request))
-
-
 def final_result_anon(request, quiz, previous):
     quiz_id = str(quiz.id)
     score = request.session[quiz_id + "_score"]
@@ -225,38 +289,6 @@ def final_result_anon(request, quiz, previous):
                                   context_instance=RequestContext(request))
 
 
-def final_result_user(request, sitting, previous):
-    quiz = sitting.quiz
-    score = sitting.get_current_score()
-    incorrect = sitting.get_incorrect_questions()
-    max_score = quiz.question_set.all().select_subclasses().count()
-    percent = sitting.get_percent_correct()
-
-    sitting.mark_quiz_complete()
-
-    if quiz.exam_paper is False:  # if we do not plan to store the outcome
-        sitting.delete()  # delete the sitting to free up space
-
-    if quiz.answers_at_end is False:
-        return render_to_response('result.html',
-                                  {'quiz': quiz,
-                                   'score': score,
-                                   'max_score': max_score,
-                                   'percent': percent,
-                                   'previous': previous},
-                                  context_instance=RequestContext(request))
-    else:
-        questions = quiz.question_set.all().select_subclasses()
-        return render_to_response('result.html',
-                                  {'quiz': quiz,
-                                   'score': score,
-                                   'max_score': max_score,
-                                   'percent': percent,
-                                   'questions': questions,
-                                   'incorrect_questions': incorrect},
-                                  context_instance=RequestContext(request))
-
-
 def question_check_anon(request, quiz):
     guess = request.GET['guess']
     question_id = request.GET['question_id']
@@ -280,30 +312,3 @@ def question_check_anon(request, quiz):
 
     else:
         return {}
-
-
-def anon_session_score(request, add=0, possible=0):
-    """
-    Returns the session score for non-signed in users.
-    If number passed in then add this to the running total and
-    return session score
-
-    examples:
-        x, y = anon_session_score(1, 1) will add 1 out of a possible 1
-        x, y = anon_session_score(0, 2) will add 0 out of a possible 2
-        x, y = anon_session_score()     will return the session score
-                                        without modification
-    """
-    if "session_score" not in request.session:
-        request.session["session_score"] = 0
-        request.session["session_score_possible"] = 0
-
-    if possible > 0:
-        request.session["session_score"] = (request.session["session_score"] +
-                                            add)
-
-        request.session["session_score_possible"] = \
-            (request.session["session_score_possible"] + possible)
-
-    return request.session["session_score"], \
-        request.session["session_score_possible"]