diff options
Diffstat (limited to 'exam/views.py')
-rw-r--r-- | exam/views.py | 77 |
1 files changed, 48 insertions, 29 deletions
diff --git a/exam/views.py b/exam/views.py index 52c5664..0bec977 100644 --- a/exam/views.py +++ b/exam/views.py @@ -11,10 +11,27 @@ from django.template import RequestContext from exam.models import Quiz, Question, QuestionPaper, Profile, Answer from exam.forms import UserRegisterForm, UserLoginForm from exam.xmlrpc_clients import python_server +from settings import URL_ROOT # The directory where user data can be saved. OUTPUT_DIR = abspath(join(dirname(__file__), pardir, 'output')) + +def my_redirect(url): + """An overridden redirect to deal with URL_ROOT-ing. See settings.py + for details.""" + return redirect(URL_ROOT + url) + +def my_render_to_response(template, context=None, **kwargs): + """Overridden render_to_response. + """ + if context is None: + context = {'URL_ROOT': URL_ROOT} + else: + context['URL_ROOT'] = URL_ROOT + return render_to_response(template, context, **kwargs) + + def gen_key(no_of_chars): """Generate a random key of the number of characters.""" allowed_chars = string.digits+string.uppercase @@ -29,9 +46,9 @@ def index(request): """ user = request.user if user.is_authenticated(): - return redirect("/exam/start/") + return my_redirect("/exam/start/") - return redirect("/exam/login/") + return my_redirect("/exam/login/") def user_register(request): """ Register a new user. @@ -39,7 +56,7 @@ def user_register(request): user = request.user if user.is_authenticated(): - return redirect("/exam/start/") + return my_redirect("/exam/start/") if request.method == "POST": form = UserRegisterForm(request.POST) @@ -49,38 +66,40 @@ def user_register(request): new_user = authenticate(username = u_name, password = pwd) login(request, new_user) - return redirect("/exam/start/") + return my_redirect("/exam/start/") else: - return render_to_response('exam/register.html',{'form':form}, + return my_render_to_response('exam/register.html', + {'form':form}, context_instance=RequestContext(request)) else: form = UserRegisterForm() - return render_to_response('exam/register.html',{'form':form}, - context_instance=RequestContext(request)) + return my_render_to_response('exam/register.html', + {'form':form}, + context_instance=RequestContext(request)) def user_login(request): """Take the credentials of the user and log the user in.""" user = request.user if user.is_authenticated(): - return redirect("/exam/start/") + return my_redirect("/exam/start/") if request.method == "POST": form = UserLoginForm(request.POST) if form.is_valid(): user = form.cleaned_data login(request, user) - return redirect("/exam/start/") + return my_redirect("/exam/start/") else: - context = {"form": form,} - return render_to_response('exam/login.html', context, - context_instance=RequestContext(request)) + context = {"form": form} + return my_render_to_response('exam/login.html', context, + context_instance=RequestContext(request)) else: form = UserLoginForm() context = {"form": form} - return render_to_response('exam/login.html', context, - context_instance=RequestContext(request)) + return my_render_to_response('exam/login.html', context, + context_instance=RequestContext(request)) def start(request): user = request.user @@ -120,18 +139,18 @@ def start(request): # Show the user the intro page. context = {'user': user} ci = RequestContext(request) - return render_to_response('exam/intro.html', context, - context_instance=ci) + return my_render_to_response('exam/intro.html', context, + context_instance=ci) def question(request, q_id): user = request.user if not user.is_authenticated(): - return redirect('exam/login/') + return my_redirect('/exam/login/') q = get_object_or_404(Question, pk=q_id) try: paper = QuestionPaper.objects.get(user=request.user) except QuestionPaper.DoesNotExist: - redirect('/exam/start') + my_redirect('/exam/start') time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') @@ -140,8 +159,8 @@ def question(request, q_id): 'quiz_name': quiz_name, 'time_left': time_left} ci = RequestContext(request) - return render_to_response('exam/question.html', context, - context_instance=ci) + return my_render_to_response('exam/question.html', context, + context_instance=ci) def show_question(request, q_id): """Show a question if possible.""" @@ -154,7 +173,7 @@ def show_question(request, q_id): def check(request, q_id): user = request.user if not user.is_authenticated(): - return redirect('exam/login/') + return my_redirect('/exam/login/') question = get_object_or_404(Question, pk=q_id) paper = QuestionPaper.objects.get(user=user) answer = request.POST.get('answer') @@ -191,15 +210,15 @@ def check(request, q_id): 'quiz_name': paper.quiz.description, 'time_left': time_left} - return render_to_response('exam/question.html', context, - context_instance=ci) + return my_render_to_response('exam/question.html', context, + context_instance=ci) else: next_q = paper.answered_question(question.id) return show_question(request, next_q) def quit(request): - return render_to_response('exam/quit.html', - context_instance=RequestContext(request)) + return my_render_to_response('exam/quit.html', + context_instance=RequestContext(request)) def complete(request, reason=None): user = request.user @@ -211,9 +230,9 @@ def complete(request, reason=None): # Logout the user and quit with the message given. logout(request) context = {'message': message} - return render_to_response('exam/complete.html', context) + return my_render_to_response('exam/complete.html', context) else: - return redirect('/exam/') + return my_redirect('/exam/') def monitor(request): """Monitor the progress of the papers taken so far.""" @@ -243,5 +262,5 @@ def monitor(request): reverse=True) context = {'paper_list': paper_list} - return render_to_response('exam/monitor.html', context, - context_instance=RequestContext(request)) + return my_render_to_response('exam/monitor.html', context, + context_instance=RequestContext(request)) |