# HG changeset patch # User nishanth # Date 1271780599 -19800 # Node ID 4911302379ac94f6ec81ec71b26cdde2dc259b1e # Parent c66b4904ce5a365cad25a313c3d37d419877b832 the basic quiz workflow is ready diff -r c66b4904ce5a -r 4911302379ac quiz/views.py --- a/quiz/views.py Tue Apr 20 21:15:18 2010 +0530 +++ b/quiz/views.py Tue Apr 20 21:53:19 2010 +0530 @@ -2,6 +2,7 @@ from django.db import IntegrityError from django.http import Http404 +from django.utils.datastructures import MultiValueDictKeyError from django.contrib.auth.models import User from django.contrib.auth import login, logout, authenticate @@ -11,7 +12,7 @@ from offline.event.models import Event from offline.quiz.utils import gen_key -from offline.quiz.models import Profile, QuestionBank, Quiz +from offline.quiz.models import Profile, QuestionBank, Quiz, Answer from offline.quiz.forms import UserRegisterForm def start_page(request): @@ -79,8 +80,6 @@ """ user = request.user - if not user.is_authenticated(): - raise Http404 try: event = Event.objects.all()[0] @@ -105,3 +104,60 @@ return redirect("/quiz/answer/%s"%(new_quiz.key)) +def answer(request, quiz_key): + """ see if username and quiz give a proper quiz object + then see if there are any available questions + then render the proper question. + """ + + user = request.user + if not user.is_authenticated(): + raise Http404 + + try: + event = Event.objects.all()[0] + except IndexError: + raise Http404 + + if event.quiz_status == "00": + raise Http404 + + try: + quiz = Quiz.objects.get(event=event,user=user,key=quiz_key) + except Quiz.DoesNotExist: + raise Http404 + + remaining_que = quiz.que_remaining.split('|') + if not remaining_que: + raise Http404 + + que_id = remaining_que[0] + question = QuestionBank.objects.get(id=que_id) + if request.method == "POST": + try: + answer = request.POST['answer'] + except MultiValueDictKeyError: + answer = "" + new_answer = Answer(question=question) + new_answer.submitted_ans = answer + new_answer.save() + + remaining_que.remove(que_id) + quiz.que_remaining = "|".join(remaining_que) + quiz.key = gen_key(10) + quiz.save() + + if not remaining_que: + return redirect('/quiz/complete') + else: + return redirect('/quiz/answer/%s'%quiz.key) + else: + return render_to_response('display_question.html', {'question':question}) + +def quiz_complete(request): + """ display thanq and log the user out. + """ + + user = request.user + logout(request) + render_to_response("quiz_complete.html") diff -r c66b4904ce5a -r 4911302379ac templates/base.html --- a/templates/base.html Tue Apr 20 21:15:18 2010 +0530 +++ b/templates/base.html Tue Apr 20 21:53:19 2010 +0530 @@ -4,6 +4,8 @@ {% block title %} {% endblock %} +{% block script %} +{% endblock %} {% block content %} diff -r c66b4904ce5a -r 4911302379ac templates/display_question.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/display_question.html Tue Apr 20 21:53:19 2010 +0530 @@ -0,0 +1,15 @@ +{% extends 'base.html' %} +{% block script %} + +{% endblock %} +{% block content %} +
+ {{question.description}}
+ +
+{% endblock %}