diff options
-rw-r--r-- | testapp/exam/views.py | 162 |
1 files changed, 88 insertions, 74 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 3c6f0e5..1228519 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -7,7 +7,7 @@ import datetime from django.http import HttpResponse from django.contrib.auth import login, logout, authenticate -from django.shortcuts import render_to_response, get_object_or_404, redirect +from django.shortcuts import my_render_to_response, get_object_or_404, my_redirect from django.template import RequestContext from django.http import Http404 from django.db.models import Sum @@ -24,6 +24,20 @@ OUTPUT_DIR = abspath(join(dirname(__file__), pardir, 'output')) set1 = set() set2 = set() +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 @@ -47,10 +61,10 @@ def index(request): user = request.user if user.is_authenticated(): if user.groups.filter(name='moderator').count() > 0: - return redirect('/exam/manage/') - return redirect("/exam/start/") + return my_redirect('/exam/manage/') + return my_redirect("/exam/start/") - return redirect("/exam/login/") + return my_redirect("/exam/login/") def user_register(request): """ Register a new user. @@ -58,7 +72,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) @@ -68,15 +82,15 @@ 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', + return my_render_to_response('exam/register.html', {'form':form}, context_instance=RequestContext(request)) else: form = UserRegisterForm() - return render_to_response('exam/register.html', + return my_render_to_response('exam/register.html', {'form':form}, context_instance=RequestContext(request)) @@ -89,7 +103,7 @@ def quizlist_user(request): if user_answerpapers.count() == 0: context = {'quizzes':avail_quiz} - return render_to_response("exam/quizzes_user.html",context) + return my_render_to_response("exam/quizzes_user.html",context) for paper in user_answerpapers: for quiz in avail_quiz: @@ -97,7 +111,7 @@ def quizlist_user(request): avail_quiz.remove(quiz) context = {'quizzes':avail_quiz,'user':user} - return render_to_response("exam/quizzes_user.html",context) + return my_render_to_response("exam/quizzes_user.html",context) def results_user(request): """Show list of Results of Quizzes that is taken by logged-in user.""" @@ -110,7 +124,7 @@ def results_user(request): temp.append(paper.get_total_marks()) quiz_marks.append(temp) context = {'papers':quiz_marks} - return render_to_response("exam/results_user.html",context) + return my_render_to_response("exam/results_user.html",context) def edit_quiz(request): """Edit the list of quizzes seleted by the user for editing.""" @@ -141,7 +155,7 @@ def edit_quiz(request): tag = tags_split[i].strip() quiz.tags.add(tag) j += 1 - return redirect("/exam/manage/showquiz/") + return my_redirect("/exam/manage/showquiz/") def edit_question(request): """Edit the list of questions seleted by the user for editing.""" @@ -177,7 +191,7 @@ def edit_question(request): tag = tags_split[i].strip() question.tags.add(tag) j += 1 - return redirect("/exam/manage/questions") + return my_redirect("/exam/manage/questions") def add_question(request,question_id=None): @@ -196,7 +210,7 @@ def add_question(request,question_id=None): for i in range(0,len(tags)-1): tag = tags[i].strip() question.tags.add(tag) - return redirect("/exam/manage/questions") + return my_redirect("/exam/manage/questions") else: d = Question.objects.get(id=question_id) @@ -215,16 +229,16 @@ def add_question(request,question_id=None): for i in range(0,len(tags)-1): tag = tags[i].strip() question.tags.add(tag) - return redirect("/exam/manage/questions") + return my_redirect("/exam/manage/questions") else: - return render_to_response('exam/add_question.html', + return my_render_to_response('exam/add_question.html', {'form':form}, context_instance=RequestContext(request)) else: if question_id == None: form = QuestionForm() - return render_to_response('exam/add_question.html', + return my_render_to_response('exam/add_question.html', {'form':form}, context_instance=RequestContext(request)) else: @@ -247,7 +261,7 @@ def add_question(request,question_id=None): if (initial_tags == ","): initial_tags = "" form.initial['tags']=initial_tags - return render_to_response('exam/add_question.html',{'form':form},context_instance=RequestContext(request)) + return my_render_to_response('exam/add_question.html',{'form':form},context_instance=RequestContext(request)) def add_quiz(request,quiz_id=None): @@ -267,7 +281,7 @@ def add_quiz(request,quiz_id=None): for tag in tags: tag = tag.strip() quiz.tags.add(tag) - return redirect("/exam/manage/designquestionpaper") + return my_redirect("/exam/manage/designquestionpaper") else: d = Quiz.objects.get(id=quiz_id) d.start_date = form['start_date'].data @@ -282,16 +296,16 @@ def add_quiz(request,quiz_id=None): for i in range(0,len(tags)-1): tag = tags[i].strip() quiz.tags.add(tag) - return redirect("/exam/manage/showquiz") + return my_redirect("/exam/manage/showquiz") else: - return render_to_response('exam/add_quiz.html', + return my_render_to_response('exam/add_quiz.html', {'form':form}, context_instance=RequestContext(request)) else: if quiz_id == None: form = QuizForm() - return render_to_response('exam/add_quiz.html', + return my_render_to_response('exam/add_quiz.html', {'form':form}, context_instance=RequestContext(request)) else: @@ -310,14 +324,14 @@ def add_quiz(request,quiz_id=None): if (initial_tags == ","): initial_tags = "" form.initial['tags']=initial_tags - return render_to_response('exam/add_quiz.html',{'form':form},context_instance=RequestContext(request)) + return my_render_to_response('exam/add_quiz.html',{'form':form},context_instance=RequestContext(request)) def design_questionpaper(request,questionpaper_id=None): user=request.user if not user.is_authenticated() or user.groups.filter(name='moderator').count() == 0 : raise Http404('You are not allowed to view this page!') - return render_to_response('exam/add_questionpaper.html',{},context_instance=RequestContext(request)) + return my_render_to_response('exam/add_questionpaper.html',{},context_instance=RequestContext(request)) def show_all_questionpapers(request,questionpaper_id=None): @@ -326,7 +340,7 @@ def show_all_questionpapers(request,questionpaper_id=None): raise Http404('You are not allowed to view this page!') if request.method=="POST" and request.POST.get('add') == "add": - return redirect("/exam/manage/designquestionpaper/" + questionpaper_id) + return my_redirect("/exam/manage/designquestionpaper/" + questionpaper_id) if request.method=="POST" and request.POST.get('delete') == "delete": data = request.POST.getlist('papers') @@ -334,16 +348,16 @@ def show_all_questionpapers(request,questionpaper_id=None): q_paper = QuestionPaper.objects.get(id=i).delete() question_paper= QuestionPaper.objects.all() context = {'papers': question_paper, } - return render_to_response('exam/showquestionpapers.html', context, + return my_render_to_response('exam/showquestionpapers.html', context, context_instance=RequestContext(request)) qu_papers = QuestionPaper.objects.all() context = {'papers':qu_papers} - return render_to_response('exam/showquestionpapers.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/showquestionpapers.html',context,context_instance=RequestContext(request)) if questionpaper_id == None: qu_papers = QuestionPaper.objects.all() context = {'papers':qu_papers} - return render_to_response('exam/showquestionpapers.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/showquestionpapers.html',context,context_instance=RequestContext(request)) else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz @@ -352,7 +366,7 @@ def show_all_questionpapers(request,questionpaper_id=None): for i in questions: q.append(i) context = {'papers':{'quiz':quiz,'questions':q}} - return render_to_response('exam/editquestionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/editquestionpaper.html',context,context_instance=RequestContext(request)) def automatic_questionpaper(request,questionpaper_id=None): @@ -373,7 +387,7 @@ def automatic_questionpaper(request,questionpaper_id=None): for i in set2: q = Question.objects.get(summary=i) quest_paper.questions.add(q) - return redirect('/exam/manage/showquiz') + return my_redirect('/exam/manage/showquiz') else: set1 = set() set2 = set() @@ -408,11 +422,11 @@ def automatic_questionpaper(request,questionpaper_id=None): msg = 'The given Criteria does not satisfy the number of Questions...' tags = Tag.objects.all() context = {'data':{'questions':set2,'tags':tags,'msg':msg}} - return render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data':{'tags':tags}} - return render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) else: if request.method=="POST": @@ -422,7 +436,7 @@ def automatic_questionpaper(request,questionpaper_id=None): print str(i.id) + " " + i.summary q = Question.objects.get(summary=i) quest_paper.questions.add(q) - return redirect('/exam/manage/showquiz') + return my_redirect('/exam/manage/showquiz') else: set1 = set() set2 = set() @@ -457,11 +471,11 @@ def automatic_questionpaper(request,questionpaper_id=None): msg = 'The given Criteria does not satisfy the number of Questions...' tags = Tag.objects.all() context = {'data':{'questions':set2,'tags':tags,'msg':msg}} - return render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data':{'tags':tags}} - return render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) def manual_questionpaper(request,questionpaper_id=None): user=request.user @@ -481,7 +495,7 @@ def manual_questionpaper(request,questionpaper_id=None): for i in questions: q = Question.objects.get(id=i) quest_paper.questions.add(q) - return redirect('/exam/manage/showquiz') + return my_redirect('/exam/manage/showquiz') else: set1 = set() set2 = set() @@ -511,11 +525,11 @@ def manual_questionpaper(request,questionpaper_id=None): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data':{'questions':set2,'tags':tags,'msg':msg}} - return render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data':{'tags':tags}} - return render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) else: if request.method=="POST": @@ -525,7 +539,7 @@ def manual_questionpaper(request,questionpaper_id=None): for i in questions: q = Question.objects.get(id=i) quest_paper.questions.add(q) - return redirect('/exam/manage/showquiz') + return my_redirect('/exam/manage/showquiz') else: set1 = set() set2 = set() @@ -555,11 +569,11 @@ def manual_questionpaper(request,questionpaper_id=None): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data':{'questions':set2,'tags':tags,'msg':msg}} - return render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data':{'tags':tags}} - return render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) @@ -570,8 +584,8 @@ def prof_manage(request): user = request.user if user.is_authenticated() and user.groups.filter(name='moderator').count() > 0: context = {'user':user} - return render_to_response('manage.html',context) - return redirect('/exam/login/') + return my_render_to_response('manage.html',context) + return my_redirect('/exam/login/') def user_login(request): """Take the credentials of the user and log the user in.""" @@ -579,8 +593,8 @@ def user_login(request): user = request.user if user.is_authenticated(): if user.groups.filter(name='moderator').count() > 0 : - return redirect('/exam/manage/') - return redirect("/exam/intro/") + return my_redirect('/exam/manage/') + return my_redirect("/exam/intro/") if request.method == "POST": form = UserLoginForm(request.POST) @@ -588,23 +602,23 @@ def user_login(request): user = form.cleaned_data login(request, user) if user.groups.filter(name='moderator').count() > 0 : - return redirect('/exam/manage/') - return redirect('/exam/login/') + return my_redirect('/exam/manage/') + return my_redirect('/exam/login/') else: context = {"form": form} - return render_to_response('exam/login.html', context, + 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, + return my_render_to_response('exam/login.html', context, context_instance=RequestContext(request)) def start(request,questionpaper_id=None): """Check the user cedentials and if any quiz is available, start the exam.""" user = request.user if questionpaper_id == None: - return redirect('/exam/quizzes/') + return my_redirect('/exam/quizzes/') try: # Right now the app is designed so there is only one active quiz # at a particular time. @@ -645,7 +659,7 @@ def start(request,questionpaper_id=None): # Show the user the intro page. context = {'user': user,'paper_id':questionpaper_id} ci = RequestContext(request) - return render_to_response('exam/intro.html', context, + return my_render_to_response('exam/intro.html', context, context_instance=ci) def question(request, q_id, questionpaper_id): @@ -653,13 +667,13 @@ def question(request, q_id, questionpaper_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: q_paper = QuestionPaper.objects.get(id=questionpaper_id) paper = AnswerPaper.objects.get(user=request.user, question_paper=q_paper) except AnswerPaper.DoesNotExist: - return redirect('/exam/start/') + return my_redirect('/exam/start/') if not paper.question_paper.quiz.active: return complete(request, reason='The quiz has been deactivated!') @@ -671,7 +685,7 @@ def question(request, q_id, questionpaper_id): 'quiz_name': quiz_name, 'time_left': time_left} ci = RequestContext(request) - return render_to_response('exam/question.html', context, + return my_render_to_response('exam/question.html', context, context_instance=ci) def show_question(request, q_id, questionpaper_id): @@ -687,7 +701,7 @@ def check(request, q_id, questionpaper_id=None): 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) q_paper = QuestionPaper.objects.get(id=questionpaper_id) paper = AnswerPaper.objects.get(user=request.user,question_paper = q_paper) @@ -739,7 +753,7 @@ def check(request, q_id, questionpaper_id=None): 'time_left': time_left} ci = RequestContext(request) - return render_to_response('exam/question.html', context, + return my_render_to_response('exam/question.html', context, context_instance=ci) else: next_q = paper.completed_question(question.id) @@ -748,7 +762,7 @@ def check(request, q_id, questionpaper_id=None): def quit(request, answerpaper_id=None): """Show the quit page when the user logs out.""" context = { 'id':answerpaper_id} - return render_to_response('exam/quit.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/quit.html',context,context_instance=RequestContext(request)) def complete(request,reason = None,answerpaper_id=None): """Show a page to inform user that the quiz has been compeleted.""" @@ -758,7 +772,7 @@ def complete(request,reason = None,answerpaper_id=None): if answerpaper_id == None: logout(request) context = {'message': "You are successfully Logged out."} - return render_to_response('exam/complete.html', context) + return my_render_to_response('exam/complete.html', context) no = False message = reason or 'The quiz has been completed. Thank you.' if user.groups.filter(name='moderator').count() > 0: @@ -770,9 +784,9 @@ def complete(request,reason = None,answerpaper_id=None): answer_paper = AnswerPaper.objects.get(id=answerpaper_id) answer_paper.endtime = datetime.datetime.now() answer_paper.save() - return redirect('/exam/quizzes/') + return my_redirect('/exam/quizzes/') else: - return redirect('/exam/') + return my_redirect('/exam/') def monitor(request, quiz_id=None): """Monitor the progress of the papers taken so far.""" @@ -786,7 +800,7 @@ def monitor(request, quiz_id=None): context = {'papers': [], 'quiz': None, 'quizzes':q_paper} - return render_to_response('exam/monitor.html', context, + return my_render_to_response('exam/monitor.html', context, context_instance=RequestContext(request)) # quiz_id is not None. try: @@ -799,7 +813,7 @@ def monitor(request, quiz_id=None): total=Sum('answers__marks')).order_by('-total') context = {'papers': papers, 'quiz': quiz, 'quizzes': None} - return render_to_response('exam/monitor.html', context, + return my_render_to_response('exam/monitor.html', context, context_instance=RequestContext(request)) def get_user_data(username): @@ -829,7 +843,7 @@ def show_all_users(request): user = User.objects.filter(username__contains="") questionpaper = AnswerPaper.objects.all() context = { 'question': questionpaper } - return render_to_response('exam/showusers.html',context,context_instance=RequestContext(request)) + return my_render_to_response('exam/showusers.html',context,context_instance=RequestContext(request)) def show_all_quiz(request): """Generates a list of all the quizzes that are currently in the database.""" @@ -846,7 +860,7 @@ def show_all_quiz(request): context = {'papers': [], 'quiz': None, 'quizzes':quizzes} - return render_to_response('exam/show_quiz.html', context, + return my_render_to_response('exam/show_quiz.html', context, context_instance=RequestContext(request)) else: for i in data: @@ -855,7 +869,7 @@ def show_all_quiz(request): context = {'papers': [], 'quiz': None, 'quizzes':quizzes} - return render_to_response('exam/show_quiz.html', context, + return my_render_to_response('exam/show_quiz.html', context, context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit')=='edit': @@ -877,7 +891,7 @@ def show_all_quiz(request): initial_tags = "" form.initial['tags']=initial_tags forms.append(form) - return render_to_response('exam/edit_quiz.html',{'forms':forms,'data':data}, + return my_render_to_response('exam/edit_quiz.html',{'forms':forms,'data':data}, context_instance=RequestContext(request)) else: @@ -885,7 +899,7 @@ def show_all_quiz(request): context = {'papers': [], 'quiz': None, 'quizzes':quizzes} - return render_to_response('exam/show_quiz.html', context, + return my_render_to_response('exam/show_quiz.html', context, context_instance=RequestContext(request)) @@ -903,7 +917,7 @@ def show_all_questions(request): context = {'papers': [], 'question': None, 'questions':questions} - return render_to_response('exam/showquestions.html', context, + return my_render_to_response('exam/showquestions.html', context, context_instance=RequestContext(request)) else: for i in data: @@ -912,7 +926,7 @@ def show_all_questions(request): context = {'papers': [], 'question': None, 'questions':questions} - return render_to_response('exam/showquestions.html', context, + return my_render_to_response('exam/showquestions.html', context, context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit')=='edit': @@ -938,14 +952,14 @@ def show_all_questions(request): initial_tags = "" form.initial['tags']=initial_tags forms.append(form) - return render_to_response('exam/edit_question.html',{'forms':forms,'data':data},context_instance=RequestContext(request)) + return my_render_to_response('exam/edit_question.html',{'forms':forms,'data':data},context_instance=RequestContext(request)) else: questions = Question.objects.all() context = {'papers': [], 'question': None, 'questions':questions} - return render_to_response('exam/showquestions.html', context, + return my_render_to_response('exam/showquestions.html', context, context_instance=RequestContext(request)) def user_data(request, username): @@ -958,7 +972,7 @@ def user_data(request, username): data = get_user_data(username) context = {'data': data} - return render_to_response('exam/user_data.html', context, + return my_render_to_response('exam/user_data.html', context, context_instance=RequestContext(request)) def grade_user(request, username): @@ -982,9 +996,9 @@ def grade_user(request, username): paper.save() context = {'data': data} - return render_to_response('exam/user_data.html', context, + return my_render_to_response('exam/user_data.html', context, context_instance=RequestContext(request)) else: context = {'data': data} - return render_to_response('exam/grade_user.html', context, + return my_render_to_response('exam/grade_user.html', context, context_instance=RequestContext(request)) |