From f5ef323d03dd80e1ae00f306ac44f5ee3efb3e7c Mon Sep 17 00:00:00 2001 From: hardythe1 Date: Mon, 1 Jul 2013 11:56:32 +0530 Subject: edited JS to give a msg when the answer is correct --- testapp/exam/views.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 6c977ee..4b9d438 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -218,10 +218,10 @@ def edit_question(request): question.points = points[j] question.test = test[j] question.options = options[j] - question.type = type[j] - edit_tags = tags[j] question.active = active[j] question.snippet = snippet[j] + question.type = type[j] + edit_tags = tags[j] question.save() for tag in question.tags.all(): question.tags.remove(tag) @@ -264,7 +264,7 @@ def add_question(request, question_id=None): question = Question.objects.get(id=question_id) for tag in question.tags.all(): question.tags.remove(tag) - tags = form['tags'].data.split(', ') + tags = form['tags'].data.split(',') for i in range(0, len(tags)-1): tag = tags[i].strip() question.tags.add(tag) @@ -303,7 +303,7 @@ def add_question(request, question_id=None): def add_quiz(request, quiz_id=None): """To add a new quiz in the database. - Create a new question and store it.""" + Create a new quiz and store it.""" user = request.user if not user.is_authenticated() or not is_moderator(user): @@ -638,7 +638,7 @@ def start(request, questionpaper_id=None): context_instance=ci) -def question(request, q_id, questionpaper_id): +def question(request, q_id, questionpaper_id,success_msg=None): """Check the credentials of the user and start the exam.""" user = request.user @@ -658,21 +658,29 @@ def question(request, q_id, questionpaper_id): if time_left == 0: return complete(request, reason='Your time is up!') quiz_name = paper.question_paper.quiz.description - context = {'question': q, 'paper': paper, 'user': user, - 'quiz_name': quiz_name, - 'time_left': time_left} + if success_msg is None: + context = {'question': q, 'paper': paper, 'user': user, + 'quiz_name': quiz_name, + 'time_left': time_left,} + + else: + context = {'question': q, 'paper': paper, 'user': user, + 'quiz_name': quiz_name, + 'time_left': time_left, + 'success_msg':success_msg} + ci = RequestContext(request) return my_render_to_response('exam/question.html', context, context_instance=ci) -def show_question(request, q_id, questionpaper_id): +def show_question(request, q_id, questionpaper_id,success_msg=None): """Show a question if possible.""" if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' return complete(request, msg) else: - return question(request, q_id, questionpaper_id) + return question(request, q_id, questionpaper_id,success_msg) def check(request, q_id, questionpaper_id=None): @@ -745,7 +753,8 @@ def check(request, q_id, questionpaper_id=None): context_instance=ci) else: next_q = paper.completed_question(question.id) - return show_question(request, next_q, questionpaper_id) + success_msg = True + return show_question(request, next_q, questionpaper_id,success_msg) def quit(request, answerpaper_id=None): -- cgit From b09885165ccb5be44827507b092216d50dd953b2 Mon Sep 17 00:00:00 2001 From: Hardik Ghaghada Date: Tue, 2 Jul 2013 16:21:27 +0530 Subject: Edited view to give a message if all the questions are correctly answered --- testapp/exam/views.py | 87 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 23 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 4b9d438..6d9806d 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -417,12 +417,18 @@ def automatic_questionpaper(request, questionpaper_id=None): if request.POST.get('save') == 'save': quiz = Quiz.objects.order_by("-id")[0] quest_paper = QuestionPaper() + questions = request.POST.getlist('questions') + tot_marks = 0 + for quest in questions: + if quest.isdigit(): + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz + quest_paper.total_marks = tot_marks quest_paper.save() - questions = request.POST.getlist('questions') - for i in questions: - if i.isdigit(): - q = Question.objects.get(id=i) + for quest in questions: + if quest.isdigit(): + q = Question.objects.get(id=quest) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: @@ -455,9 +461,16 @@ def automatic_questionpaper(request, questionpaper_id=None): if request.POST.get('save') == 'save': quest_paper = QuestionPaper.objects.get(id=questionpaper_id) questions = request.POST.getlist('questions') - for i in questions: - if i.isdigit(): - q = Question.objects.get(id=i) + tot_marks = quest_paper.total_marks + for quest in questions: + if quest.isdigit(): + q = Question.objects.get(id=quest) + tot_marks += q.points + quest_paper.total_marks = tot_marks + quest_paper.save() + for quest in questions: + if quest.isdigit(): + q = Question.objects.get(id=quest) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: @@ -495,9 +508,15 @@ def manual_questionpaper(request, questionpaper_id=None): if request.method == "POST": if request.POST.get('save') == 'save': questions = request.POST.getlist('questions') - quiz = Quiz.objects.order_by("-id")[0] quest_paper = QuestionPaper() + quiz = Quiz.objects.order_by("-id")[0] + tot_marks = 0 + for quest in questions: + if quest.isdigit(): + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz + quest_paper.total_marks = tot_marks quest_paper.save() for i in questions: q = Question.objects.get(id=i) @@ -524,12 +543,18 @@ def manual_questionpaper(request, questionpaper_id=None): else: if request.method == "POST": if request.POST.get('save') == 'save': - quest_paper = QuestionPaper.objects.get(id=questionpaper_id) - questions = request.POST.getlist('questions') - for i in questions: - q = Question.objects.get(id=i) - quest_paper.questions.add(q) - return my_redirect('/exam/manage/showquiz') + quest_paper = QuestionPaper.objects.get(id=questionpaper_id) + questions = request.POST.getlist('questions') + tot_marks = quest_paper.total_marks + for quest in questions: + q = Question.objects.get(id=quest) + tot_marks += q.points + quest_paper.total_marks = tot_marks + quest_paper.save() + for i in questions: + q = Question.objects.get(id=i) + quest_paper.questions.add(q) + return my_redirect('/exam/manage/showquiz') else: fetched_questions = fetch_questions(request) n = len(fetched_questions) @@ -549,6 +574,7 @@ def manual_questionpaper(request, questionpaper_id=None): context, context_instance=RequestContext(request)) + def prof_manage(request): """Take credentials of the user with professor/moderator rights/permissions and log in.""" @@ -674,11 +700,12 @@ def question(request, q_id, questionpaper_id,success_msg=None): context_instance=ci) -def show_question(request, q_id, questionpaper_id,success_msg=None): +def show_question(request, q_id, questionpaper_id,success_msg=None, + answerpaper_id=None): """Show a question if possible.""" if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' - return complete(request, msg) + return complete(request, msg,answerpaper_id) else: return question(request, q_id, questionpaper_id,success_msg) @@ -754,7 +781,8 @@ def check(request, q_id, questionpaper_id=None): else: next_q = paper.completed_question(question.id) success_msg = True - return show_question(request, next_q, questionpaper_id,success_msg) + return show_question(request, next_q, questionpaper_id,success_msg, + paper.id) def quit(request, answerpaper_id=None): @@ -766,17 +794,30 @@ def quit(request, answerpaper_id=None): def complete(request, reason=None, answerpaper_id=None): """Show a page to inform user that the quiz has been compeleted.""" - + user = request.user - if answerpaper_id is None: - logout(request) - context = {'message': "You are successfully Logged out."} - return my_render_to_response('exam/complete.html', context) + logout(request) + context = {'message': "You are successfully Logged out."} + return my_render_to_response('exam/complete.html', context) + else: + paper = AnswerPaper.objects.get(id=answerpaper_id) + obt_marks = paper.get_total_marks() + tot_marks = paper.question_paper.total_marks + print tot_marks + if obt_marks == paper.question_paper.total_marks: + context = {'message': "Hurray ! You did an excellent job.\ + you answered all the questions correctly.\ + You have been logged out successfully,\ + Thank You !"} + else: + context = {'message': reason} + logout(request) + 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: - message = 'You are successfully Logged out.' + message = 'You are successfully Logged out.' if request.method == 'POST' and 'no' in request.POST: no = True if not no: -- cgit From a0ca4408b45a446e622837ef659e722ac83af149 Mon Sep 17 00:00:00 2001 From: Hardik Ghaghada Date: Tue, 2 Jul 2013 16:56:57 +0530 Subject: corrected indentation at various places --- testapp/exam/views.py | 212 +++++++++++++++++++++++++------------------------- 1 file changed, 106 insertions(+), 106 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 6d9806d..06da6d6 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -26,7 +26,7 @@ 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.""" + for details.""" return redirect(URL_ROOT + url) @@ -54,8 +54,8 @@ def get_user_dir(user): os.mkdir(user_dir) # Make it rwx by others. os.chmod(user_dir, stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH\ - | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR\ - | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP) + | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR\ + | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP) return user_dir @@ -123,12 +123,12 @@ def user_register(request): return my_redirect("/exam/start/") else: return my_render_to_response('exam/register.html', - {'form': form}, - context_instance=RequestContext(request)) + {'form': form}, + context_instance=RequestContext(request)) else: form = UserRegisterForm() return my_render_to_response('exam/register.html', - {'form': form}, context_instance=RequestContext(request)) + {'form': form}, context_instance=RequestContext(request)) def quizlist_user(request): @@ -145,7 +145,7 @@ def quizlist_user(request): for paper in user_answerpapers: for quiz in avail_quiz: if paper.question_paper.id == quiz.id and \ - paper.end_time != paper.start_time: + paper.end_time != paper.start_time: avail_quiz.remove(quiz) context = {'quizzes': avail_quiz, 'user': user} @@ -271,12 +271,12 @@ def add_question(request, question_id=None): return my_redirect("/exam/manage/questions") else: return my_render_to_response('exam/add_question.html', - {'form': form}, context_instance=RequestContext(request)) + {'form': form}, context_instance=RequestContext(request)) else: if question_id is None: form = QuestionForm() return my_render_to_response('exam/add_question.html', - {'form': form}, context_instance=RequestContext(request)) + {'form': form}, context_instance=RequestContext(request)) else: d = Question.objects.get(id=question_id) form = QuestionForm() @@ -297,8 +297,8 @@ def add_question(request, question_id=None): initial_tags = "" form.initial['tags'] = initial_tags return my_render_to_response('exam/add_question.html', - {'form': form}, - context_instance=RequestContext(request)) + {'form': form}, + context_instance=RequestContext(request)) def add_quiz(request, quiz_id=None): @@ -337,14 +337,14 @@ def add_quiz(request, quiz_id=None): return my_redirect("/exam/manage/showquiz") else: return my_render_to_response('exam/add_quiz.html', - {'form': form}, - context_instance=RequestContext(request)) + {'form': form}, + context_instance=RequestContext(request)) else: if quiz_id is None: form = QuizForm() return my_render_to_response('exam/add_quiz.html', - {'form': form}, - context_instance=RequestContext(request)) + {'form': form}, + context_instance=RequestContext(request)) else: d = Quiz.objects.get(id=quiz_id) form = QuizForm() @@ -361,8 +361,8 @@ def add_quiz(request, quiz_id=None): initial_tags = "" form.initial['tags'] = initial_tags return my_render_to_response('exam/add_quiz.html', - {'form': form}, - context_instance=RequestContext(request)) + {'form': form}, + context_instance=RequestContext(request)) def design_questionpaper(request, questionpaper_id=None): @@ -370,7 +370,7 @@ def design_questionpaper(request, questionpaper_id=None): if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') return my_render_to_response('exam/add_questionpaper.html', {}, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def show_all_questionpapers(request, questionpaper_id=None): @@ -380,7 +380,7 @@ def show_all_questionpapers(request, questionpaper_id=None): if request.method == "POST" and request.POST.get('add') == "add": return my_redirect("/exam/manage/designquestionpaper/" + \ - questionpaper_id) + questionpaper_id) if request.method == "POST" and request.POST.get('delete') == "delete": data = request.POST.getlist('papers') @@ -390,19 +390,19 @@ def show_all_questionpapers(request, questionpaper_id=None): question_paper = QuestionPaper.objects.all() context = {'papers': question_paper} return my_render_to_response('exam/showquestionpapers.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) if questionpaper_id is None: qu_papers = QuestionPaper.objects.all() context = {'papers': qu_papers} return my_render_to_response('exam/showquestionpapers.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz questions = qu_papers.questions.all() context = {'papers': {'quiz': quiz, 'questions': questions}} return my_render_to_response('exam/editquestionpaper.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def automatic_questionpaper(request, questionpaper_id=None): @@ -420,9 +420,9 @@ def automatic_questionpaper(request, questionpaper_id=None): questions = request.POST.getlist('questions') tot_marks = 0 for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - tot_marks += q.points + if quest.isdigit(): + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() @@ -445,16 +445,16 @@ def automatic_questionpaper(request, questionpaper_id=None): of Questions...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions, - 'tags': tags, - 'msg': msg}} + 'tags': tags, + 'msg': msg}} return my_render_to_response(\ - 'exam/automatic_questionpaper.html', context, - context_instance=RequestContext(request)) + 'exam/automatic_questionpaper.html', context, + context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=RequestContext(request)) else: if request.method == "POST": @@ -490,13 +490,13 @@ def automatic_questionpaper(request, questionpaper_id=None): 'tags': tags, 'msg': msg}} return my_render_to_response\ - ('exam/automatic_questionpaper.html', context, - context_instance=RequestContext(request)) + ('exam/automatic_questionpaper.html', context, + context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=RequestContext(request)) def manual_questionpaper(request, questionpaper_id=None): @@ -512,9 +512,9 @@ def manual_questionpaper(request, questionpaper_id=None): quiz = Quiz.objects.order_by("-id")[0] tot_marks = 0 for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - tot_marks += q.points + if quest.isdigit(): + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() @@ -530,31 +530,31 @@ def manual_questionpaper(request, questionpaper_id=None): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions,\ - 'tags': tags, 'msg': msg}} + 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', - context, - context_instance=RequestContext(request)) + context, + context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=RequestContext(request)) else: if request.method == "POST": if request.POST.get('save') == 'save': - quest_paper = QuestionPaper.objects.get(id=questionpaper_id) - questions = request.POST.getlist('questions') - tot_marks = quest_paper.total_marks - for quest in questions: - q = Question.objects.get(id=quest) - tot_marks += q.points - quest_paper.total_marks = tot_marks - quest_paper.save() - for i in questions: - q = Question.objects.get(id=i) - quest_paper.questions.add(q) - return my_redirect('/exam/manage/showquiz') + quest_paper = QuestionPaper.objects.get(id=questionpaper_id) + questions = request.POST.getlist('questions') + tot_marks = quest_paper.total_marks + for quest in questions: + q = Question.objects.get(id=quest) + tot_marks += q.points + quest_paper.total_marks = tot_marks + quest_paper.save() + for i in questions: + q = Question.objects.get(id=i) + quest_paper.questions.add(q) + return my_redirect('/exam/manage/showquiz') else: fetched_questions = fetch_questions(request) n = len(fetched_questions) @@ -563,15 +563,15 @@ def manual_questionpaper(request, questionpaper_id=None): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions,\ - 'tags': tags, 'msg': msg}} + 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', - context, - context_instance=RequestContext(request)) + context, + context_instance=RequestContext(request)) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=RequestContext(request)) @@ -580,7 +580,7 @@ def prof_manage(request): rights/permissions and log in.""" user = request.user if user.is_authenticated()\ - and user.groups.filter(name='moderator').count() > 0: + and user.groups.filter(name='moderator').count() > 0: context = {'user': user} return my_render_to_response('manage.html', context) return my_redirect('/exam/login/') @@ -606,7 +606,7 @@ def user_login(request): else: context = {"form": form} return my_render_to_response('exam/login.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) else: form = UserLoginForm() context = {"form": form} @@ -626,7 +626,7 @@ def start(request, questionpaper_id=None): questionpaper = QuestionPaper.objects.get(id=questionpaper_id) except QuestionPaper.DoesNotExist: msg = 'Quiz not found, please contact your '\ - 'instructor/administrator. Please login again thereafter.' + 'instructor/administrator. Please login again thereafter.' return complete(request, reason=msg) try: @@ -644,7 +644,7 @@ def start(request, questionpaper_id=None): raise Http404(msg) new_paper = AnswerPaper(user=user, user_ip=ip, - question_paper=questionpaper, profile=profile) + question_paper=questionpaper, profile=profile) new_paper.start_time = datetime.datetime.now() new_paper.end_time = datetime.datetime.now() # Make user directory. @@ -661,7 +661,7 @@ def start(request, questionpaper_id=None): context = {'user': user, 'paper_id': questionpaper_id} ci = RequestContext(request) return my_render_to_response('exam/intro.html', context, - context_instance=ci) + context_instance=ci) def question(request, q_id, questionpaper_id,success_msg=None): @@ -674,7 +674,7 @@ def question(request, q_id, questionpaper_id,success_msg=None): try: q_paper = QuestionPaper.objects.get(id=questionpaper_id) paper = AnswerPaper.objects.get(\ - user=request.user, question_paper=q_paper) + user=request.user, question_paper=q_paper) except AnswerPaper.DoesNotExist: return my_redirect('/exam/start/') if not paper.question_paper.quiz.active: @@ -697,11 +697,11 @@ def question(request, q_id, questionpaper_id,success_msg=None): ci = RequestContext(request) return my_render_to_response('exam/question.html', context, - context_instance=ci) + context_instance=ci) def show_question(request, q_id, questionpaper_id,success_msg=None, - answerpaper_id=None): + answerpaper_id=None): """Show a question if possible.""" if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' @@ -782,14 +782,14 @@ def check(request, q_id, questionpaper_id=None): next_q = paper.completed_question(question.id) success_msg = True return show_question(request, next_q, questionpaper_id,success_msg, - paper.id) + paper.id) def quit(request, answerpaper_id=None): """Show the quit page when the user logs out.""" context = {'id': answerpaper_id} return my_render_to_response('exam/quit.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def complete(request, reason=None, answerpaper_id=None): @@ -797,27 +797,27 @@ def complete(request, reason=None, answerpaper_id=None): user = request.user if answerpaper_id is None: - logout(request) - context = {'message': "You are successfully Logged out."} - return my_render_to_response('exam/complete.html', context) + logout(request) + context = {'message': "You are successfully Logged out."} + return my_render_to_response('exam/complete.html', context) else: - paper = AnswerPaper.objects.get(id=answerpaper_id) - obt_marks = paper.get_total_marks() - tot_marks = paper.question_paper.total_marks - print tot_marks - if obt_marks == paper.question_paper.total_marks: - context = {'message': "Hurray ! You did an excellent job.\ + paper = AnswerPaper.objects.get(id=answerpaper_id) + obt_marks = paper.get_total_marks() + tot_marks = paper.question_paper.total_marks + print tot_marks + if obt_marks == paper.question_paper.total_marks: + context = {'message': "Hurray ! You did an excellent job.\ you answered all the questions correctly.\ You have been logged out successfully,\ Thank You !"} - else: - context = {'message': reason} - logout(request) - return my_render_to_response('exam/complete.html',context) + else: + context = {'message': reason} + logout(request) + 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: - message = 'You are successfully Logged out.' + message = 'You are successfully Logged out.' if request.method == 'POST' and 'no' in request.POST: no = True if not no: @@ -843,7 +843,7 @@ def monitor(request, quiz_id=None): 'quiz': None, 'quizzes': q_paper} return my_render_to_response('exam/monitor.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) # quiz_id is not None. try: quiz = QuestionPaper.objects.get(id=quiz_id) @@ -852,7 +852,7 @@ def monitor(request, quiz_id=None): quiz = None else: papers = AnswerPaper.objects.all().annotate( - total=Sum('answers__marks')).order_by('-total') + total=Sum('answers__marks')).order_by('-total') context = {'papers': papers, 'quiz': quiz, 'quizzes': None} return my_render_to_response('exam/monitor.html', context, @@ -888,7 +888,7 @@ def show_all_users(request): questionpaper = AnswerPaper.objects.all() context = {'question': questionpaper} return my_render_to_response('exam/showusers.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def show_all_quiz(request): @@ -905,19 +905,19 @@ def show_all_quiz(request): if data is None: quizzes = Quiz.objects.all() context = {'papers': [], - 'quiz': None, - 'quizzes': quizzes} + 'quiz': None, + 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) else: for i in data: quiz = Quiz.objects.get(id=i).delete() quizzes = Quiz.objects.all() context = {'papers': [], - 'quiz': None, - 'quizzes': quizzes} + 'quiz': None, + 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit') == 'edit': data = request.POST.getlist('quiz') @@ -939,15 +939,15 @@ def show_all_quiz(request): form.initial['tags'] = initial_tags forms.append(form) return my_render_to_response('exam/edit_quiz.html', - {'forms': forms, 'data': data}, - context_instance=RequestContext(request)) + {'forms': forms, 'data': data}, + context_instance=RequestContext(request)) else: quizzes = Quiz.objects.all() context = {'papers': [], 'quiz': None, 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def show_all_questions(request): @@ -962,19 +962,19 @@ def show_all_questions(request): if data is None: questions = Question.objects.all() context = {'papers': [], - 'question': None, - 'questions': questions} + 'question': None, + 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) else: for i in data: question = Question.objects.get(id=i).delete() questions = Question.objects.all() context = {'papers': [], - 'question': None, - 'questions': questions} + 'question': None, + 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit') == 'edit': data = request.POST.getlist('question') @@ -999,15 +999,15 @@ def show_all_questions(request): form.initial['tags'] = initial_tags forms.append(form) return my_render_to_response('exam/edit_question.html', - {'forms': forms, 'data': data}, - context_instance=RequestContext(request)) + {'forms': forms, 'data': data}, + context_instance=RequestContext(request)) else: questions = Question.objects.all() context = {'papers': [], - 'question': None, - 'questions': questions} + 'question': None, + 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def user_data(request, username): @@ -1042,13 +1042,13 @@ def grade_user(request, username): last_ans.marks = marks last_ans.save() paper.comments = request.POST.get(\ - 'comments_%d' % paper.question_paper.id) + 'comments_%d' % paper.question_paper.id) paper.save() context = {'data': data} return my_render_to_response('exam/user_data.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) else: context = {'data': data} return my_render_to_response('exam/grade_user.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) -- cgit From 156bb47ecebcca190683b8e59f917a5b742d11fd Mon Sep 17 00:00:00 2001 From: Hardik Ghaghada Date: Wed, 3 Jul 2013 16:37:57 +0530 Subject: corrected a small malfunction --- testapp/exam/views.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 06da6d6..e2ff763 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -810,6 +810,8 @@ def complete(request, reason=None, answerpaper_id=None): you answered all the questions correctly.\ You have been logged out successfully,\ Thank You !"} + logout(request) + return my_render_to_response('exam/complete.html',context) else: context = {'message': reason} logout(request) -- cgit From 754244da53f7e0a63c272ce6d1ffb15c1d5be0ae Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 8 Jul 2013 11:22:01 +0530 Subject: Added few questions. Made changes to fix minor bugs which were found during testing. --- testapp/exam/views.py | 85 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 33 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 55ccd38..2545d8b 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -627,7 +627,7 @@ def start(request, questionpaper_id=None): except QuestionPaper.DoesNotExist: msg = 'Quiz not found, please contact your '\ 'instructor/administrator. Please login again thereafter.' - return complete(request, reason=msg) + return complete(request, msg, questionpaper_id) try: old_paper = AnswerPaper.objects.get(\ @@ -678,7 +678,8 @@ def question(request, q_id, questionpaper_id,success_msg=None): except AnswerPaper.DoesNotExist: return my_redirect('/exam/start/') if not paper.question_paper.quiz.active: - return complete(request, reason='The quiz has been deactivated!') + reason='The quiz has been deactivated!' + return complete(request, reason, questionpaper_id) time_left = paper.time_left() if time_left == 0: @@ -700,12 +701,11 @@ def question(request, q_id, questionpaper_id,success_msg=None): context_instance=ci) -def show_question(request, q_id, questionpaper_id,success_msg=None, - answerpaper_id=None): +def show_question(request, q_id, questionpaper_id,success_msg=None): """Show a question if possible.""" if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' - return complete(request, msg,answerpaper_id) + return complete(request, msg,questionpaper_id) else: return question(request, q_id, questionpaper_id,success_msg) @@ -722,16 +722,21 @@ def check(request, q_id, questionpaper_id=None): snippet_code = request.POST.get('snippet') user_answer = request.POST.get('answer') skip = request.POST.get('skip', None) + success_msg = False + success = True if skip is not None: next_q = paper.skip() return show_question(request, next_q, questionpaper_id) if question.type == 'mcq': # Add the answer submitted, regardless of it being correct or not. - new_answer = Answer(question=question, answer=user_answer, - correct=False) - new_answer.save() - paper.answers.add(new_answer) + if user_answer is not None : + print "EEERRRROOOORRRRRRR :P" + new_answer = Answer(question=question, answer=user_answer, + correct=False) + new_answer.save() + paper.answers.add(new_answer) + else: """Add the answer submitted with the Snippet code, regardless of it being correct or not.""" @@ -745,13 +750,16 @@ def check(request, q_id, questionpaper_id=None): # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. if question.type == 'mcq': - success = True # Only one attempt allowed for MCQ's. - if user_answer.strip() == question.test.strip(): - new_answer.correct = True - new_answer.marks = question.points - new_answer.error = 'Correct answer' - else: - new_answer.error = 'Incorrect answer' + if user_answer is not None: + success = True # Only one attempt allowed for MCQ's. + if user_answer.strip() == question.test.strip(): + new_answer.correct = True + new_answer.marks = question.points + new_answer.error = 'Correct answer' + success_msg = True + else: + new_answer.error = 'Incorrect answer' + new_answer.save() else: user_dir = get_user_dir(user) success, err_msg = code_server.run_code(answer_check, question.test, @@ -761,15 +769,17 @@ def check(request, q_id, questionpaper_id=None): # Note the success and save it along with the marks. new_answer.correct = success new_answer.marks = question.points + success_msg = True + new_answer.save() - new_answer.save() - + time_left = paper.time_left() if not success: # Should only happen for non-mcq questions. - time_left = paper.time_left() if time_left == 0: - return complete(request, reason='Your time is up!') + reason='Your time is up!' + return complete(request, reason, questionpaper_id) if not paper.question_paper.quiz.active: - return complete(request, reason='The quiz has been deactivated!') + reason='The quiz has been deactivated!' + return complete(request, reason, questionpaper_id) context = {'question': question, 'error_message': err_msg, 'paper': paper, 'last_attempt': user_answer, 'quiz_name': paper.question_paper.quiz.description, @@ -779,32 +789,39 @@ def check(request, q_id, questionpaper_id=None): return my_render_to_response('exam/question.html', context, context_instance=ci) else: - next_q = paper.completed_question(question.id) - success_msg = True - return show_question(request, next_q, questionpaper_id,success_msg, - paper.id) + if time_left <= 0: + print "success" + print str(time_left) + reason='Your time is up!' + return complete(request, reason, questionpaper_id) + else: + print "fail" + print time_left + next_q = paper.completed_question(question.id) + return show_question(request, next_q, questionpaper_id,success_msg) -def quit(request, answerpaper_id=None): +def quit(request, questionpaper_id=None): """Show the quit page when the user logs out.""" - context = {'id': answerpaper_id} + context = {'id': questionpaper_id} return my_render_to_response('exam/quit.html', context, context_instance=RequestContext(request)) -def complete(request, reason=None, answerpaper_id=None): +def complete(request, reason=None, questionpaper_id=None): """Show a page to inform user that the quiz has been compeleted.""" user = request.user - if answerpaper_id is None: + if questionpaper_id is None: logout(request) - context = {'message': "You are successfully Logged out."} + message = reason or "You are successfully logged out." + context = {'message': message} return my_render_to_response('exam/complete.html', context) else: - paper = AnswerPaper.objects.get(id=answerpaper_id) + q_paper = QuestionPaper.objects.get(id=questionpaper_id) + paper = AnswerPaper.objects.get(user=user, question_paper=q_paper) obt_marks = paper.get_total_marks() tot_marks = paper.question_paper.total_marks - print tot_marks if obt_marks == paper.question_paper.total_marks: context = {'message': "Hurray ! You did an excellent job.\ you answered all the questions correctly.\ @@ -813,7 +830,8 @@ def complete(request, reason=None, answerpaper_id=None): logout(request) return my_render_to_response('exam/complete.html',context) else: - context = {'message': reason} + message = reason or "You are successfully logged out" + context = {'message': message } logout(request) return my_render_to_response('exam/complete.html',context) no = False @@ -991,6 +1009,7 @@ def show_all_questions(request): form.initial['options'] = d.options form.initial['type'] = d.type form.initial['active'] = d.active + form.initial['snippet'] = d.snippet form_tags = d.tags.all() form_tags_split = form_tags.values('name') initial_tags = "" -- cgit From fbeed0456532b2b36f02a26dbfffc132235d5e4c Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 19 Aug 2013 13:09:10 +0530 Subject: This branch was created for testing the app. Changes made here are during the testing phase. And the feedbacks recevied are implemented. --- testapp/exam/views.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 2545d8b..f21357e 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -151,6 +151,14 @@ def quizlist_user(request): context = {'quizzes': avail_quiz, 'user': user} return my_render_to_response("exam/quizzes_user.html", context) +def intro(request, questionpaper_id): + """Show introduction page before quiz starts""" + user = request.user + context = {'user': user, 'paper_id': questionpaper_id } + ci = RequestContext(request) + return my_render_to_response('exam/intro.html', context, + context_instance=ci) + def results_user(request): """Show list of Results of Quizzes that is taken by logged-in user.""" @@ -158,7 +166,11 @@ def results_user(request): papers = AnswerPaper.objects.filter(user=user) quiz_marks = [] for paper in papers: - temp = paper.question_paper.quiz.description, paper.get_total_marks() + marks_obtained = paper.get_total_marks() + max_marks = paper.question_paper.total_marks + percentage = round((marks_obtained/max_marks)*100,2) + temp = paper.question_paper.quiz.description, marks_obtained,\ + max_marks, percentage quiz_marks.append(temp) context = {'papers': quiz_marks} return my_render_to_response("exam/results_user.html", context) @@ -653,16 +665,9 @@ def start(request, questionpaper_id=None): questions = [str(_.id) for _ in questionpaper.questions.all()] random.shuffle(questions) - #questions = questionpaper.questions - #random.shuffle(questions) new_paper.questions = "|".join(questions) new_paper.save() - # Show the user the intro page. - context = {'user': user, 'paper_id': questionpaper_id} - ci = RequestContext(request) - return my_render_to_response('exam/intro.html', context, - context_instance=ci) - + return start(request, questionpaper_id) def question(request, q_id, questionpaper_id,success_msg=None): """Check the credentials of the user and start the exam.""" @@ -680,7 +685,9 @@ def question(request, q_id, questionpaper_id,success_msg=None): if not paper.question_paper.quiz.active: reason='The quiz has been deactivated!' return complete(request, reason, questionpaper_id) - + #if new: + # paper.start_time = datetime.datetime.now() + # paper.end_time = datetime.datetime.now() time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') @@ -731,7 +738,6 @@ def check(request, q_id, questionpaper_id=None): if question.type == 'mcq': # Add the answer submitted, regardless of it being correct or not. if user_answer is not None : - print "EEERRRROOOORRRRRRR :P" new_answer = Answer(question=question, answer=user_answer, correct=False) new_answer.save() @@ -790,13 +796,9 @@ def check(request, q_id, questionpaper_id=None): context_instance=ci) else: if time_left <= 0: - print "success" - print str(time_left) reason='Your time is up!' return complete(request, reason, questionpaper_id) else: - print "fail" - print time_left next_q = paper.completed_question(question.id) return show_question(request, next_q, questionpaper_id,success_msg) @@ -850,14 +852,14 @@ def complete(request, reason=None, questionpaper_id=None): return my_redirect('/exam/') -def monitor(request, quiz_id=None): +def monitor(request, questionpaper_id=None): """Monitor the progress of the papers taken so far.""" user = request.user if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') - if quiz_id is None: + if questionpaper_id is None: q_paper = QuestionPaper.objects.all() context = {'papers': [], 'quiz': None, @@ -866,15 +868,15 @@ def monitor(request, quiz_id=None): context_instance=RequestContext(request)) # quiz_id is not None. try: - quiz = QuestionPaper.objects.get(id=quiz_id) + q_paper = QuestionPaper.objects.get(id=questionpaper_id) except QuestionPaper.DoesNotExist: papers = [] - quiz = None + q_paper = None else: - papers = AnswerPaper.objects.all().annotate( + papers = AnswerPaper.objects.filter(question_paper=q_paper).annotate( total=Sum('answers__marks')).order_by('-total') - context = {'papers': papers, 'quiz': quiz, 'quizzes': None} + context = {'papers': papers, 'quiz': q_paper, 'quizzes': None} return my_render_to_response('exam/monitor.html', context, context_instance=RequestContext(request)) -- cgit From fa1cdf5c8f92715f2b3866f0a17e2439d27557c5 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 12 May 2014 15:13:33 +0530 Subject: Files modified according to the pep8 coding standard. --- testapp/exam/views.py | 198 ++++++++++++++++++++++++++------------------------ 1 file changed, 105 insertions(+), 93 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index f21357e..f24215b 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -52,10 +52,10 @@ def get_user_dir(user): user_dir = join(OUTPUT_DIR, str(user.username)) if not exists(user_dir): os.mkdir(user_dir) - # Make it rwx by others. - os.chmod(user_dir, stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH\ - | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR\ - | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP) + # Make it rwx by others. + os.chmod(user_dir, stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH + | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR + | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP) return user_dir @@ -110,6 +110,7 @@ def user_register(request): Create a user and corresponding profile and store roll_number also.""" user = request.user + ci = RequestContext(request) if user.is_authenticated(): return my_redirect("/exam/start/") @@ -122,13 +123,11 @@ def user_register(request): login(request, new_user) return my_redirect("/exam/start/") else: - return my_render_to_response('exam/register.html', - {'form': form}, - context_instance=RequestContext(request)) + return my_render_to_response('exam/register.html', {'form': form}, + context_instance=ci) else: form = UserRegisterForm() - return my_render_to_response('exam/register.html', - {'form': form}, context_instance=RequestContext(request)) + return my_render_to_response('exam/register.html', {'form': form}) def quizlist_user(request): @@ -151,14 +150,15 @@ def quizlist_user(request): context = {'quizzes': avail_quiz, 'user': user} return my_render_to_response("exam/quizzes_user.html", context) + def intro(request, questionpaper_id): """Show introduction page before quiz starts""" user = request.user - context = {'user': user, 'paper_id': questionpaper_id } + context = {'user': user, 'paper_id': questionpaper_id} ci = RequestContext(request) return my_render_to_response('exam/intro.html', context, - context_instance=ci) - + context_instance=ci) + def results_user(request): """Show list of Results of Quizzes that is taken by logged-in user.""" @@ -168,7 +168,7 @@ def results_user(request): for paper in papers: marks_obtained = paper.get_total_marks() max_marks = paper.question_paper.total_marks - percentage = round((marks_obtained/max_marks)*100,2) + percentage = round((marks_obtained/max_marks)*100, 2) temp = paper.question_paper.quiz.description, marks_obtained,\ max_marks, percentage quiz_marks.append(temp) @@ -248,6 +248,7 @@ def add_question(request, question_id=None): """To add a new question in the database. Create a new question and store it.""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if request.method == "POST": @@ -283,12 +284,14 @@ def add_question(request, question_id=None): return my_redirect("/exam/manage/questions") else: return my_render_to_response('exam/add_question.html', - {'form': form}, context_instance=RequestContext(request)) + {'form': form}, + context_instance=ci) else: if question_id is None: form = QuestionForm() return my_render_to_response('exam/add_question.html', - {'form': form}, context_instance=RequestContext(request)) + {'form': form}, + context_instance=ci) else: d = Question.objects.get(id=question_id) form = QuestionForm() @@ -310,7 +313,7 @@ def add_question(request, question_id=None): form.initial['tags'] = initial_tags return my_render_to_response('exam/add_question.html', {'form': form}, - context_instance=RequestContext(request)) + context_instance=ci) def add_quiz(request, quiz_id=None): @@ -318,6 +321,7 @@ def add_quiz(request, quiz_id=None): Create a new quiz and store it.""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if request.method == "POST": @@ -350,13 +354,13 @@ def add_quiz(request, quiz_id=None): else: return my_render_to_response('exam/add_quiz.html', {'form': form}, - context_instance=RequestContext(request)) + context_instance=ci) else: if quiz_id is None: form = QuizForm() return my_render_to_response('exam/add_quiz.html', {'form': form}, - context_instance=RequestContext(request)) + context_instance=ci) else: d = Quiz.objects.get(id=quiz_id) form = QuizForm() @@ -374,24 +378,26 @@ def add_quiz(request, quiz_id=None): form.initial['tags'] = initial_tags return my_render_to_response('exam/add_quiz.html', {'form': form}, - context_instance=RequestContext(request)) + context_instance=ci) def design_questionpaper(request, questionpaper_id=None): user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') return my_render_to_response('exam/add_questionpaper.html', {}, - context_instance=RequestContext(request)) + context_instance=ci) def show_all_questionpapers(request, questionpaper_id=None): user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if request.method == "POST" and request.POST.get('add') == "add": - return my_redirect("/exam/manage/designquestionpaper/" + \ + return my_redirect("/exam/manage/designquestionpaper/" + questionpaper_id) if request.method == "POST" and request.POST.get('delete') == "delete": @@ -402,25 +408,26 @@ def show_all_questionpapers(request, questionpaper_id=None): question_paper = QuestionPaper.objects.all() context = {'papers': question_paper} return my_render_to_response('exam/showquestionpapers.html', context, - context_instance=RequestContext(request)) + context_instance=ci) if questionpaper_id is None: qu_papers = QuestionPaper.objects.all() context = {'papers': qu_papers} return my_render_to_response('exam/showquestionpapers.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz questions = qu_papers.questions.all() context = {'papers': {'quiz': quiz, 'questions': questions}} return my_render_to_response('exam/editquestionpaper.html', context, - context_instance=RequestContext(request)) + context_instance=ci) def automatic_questionpaper(request, questionpaper_id=None): """Generate automatic question paper for a particular quiz""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') @@ -459,14 +466,14 @@ def automatic_questionpaper(request, questionpaper_id=None): context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} - return my_render_to_response(\ + return my_render_to_response( 'exam/automatic_questionpaper.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=ci) else: if request.method == "POST": @@ -501,18 +508,19 @@ def automatic_questionpaper(request, questionpaper_id=None): context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} - return my_render_to_response\ - ('exam/automatic_questionpaper.html', context, - context_instance=RequestContext(request)) + return my_render_to_response( + 'exam/automatic_questionpaper.html', context, + context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=ci) def manual_questionpaper(request, questionpaper_id=None): user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') @@ -541,16 +549,16 @@ def manual_questionpaper(request, questionpaper_id=None): if (n == 0): msg = 'No matching Question found...' tags = Tag.objects.all() - context = {'data': {'questions': fetched_questions,\ + context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', - context, context_instance=RequestContext(request)) + context, context_instance=ci) else: if request.method == "POST": @@ -574,17 +582,16 @@ def manual_questionpaper(request, questionpaper_id=None): if (n == 0): msg = 'No matching Question found...' tags = Tag.objects.all() - context = {'data': {'questions': fetched_questions,\ + context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', - context, context_instance=RequestContext(request)) - + context, context_instance=ci) def prof_manage(request): @@ -602,6 +609,7 @@ def user_login(request): """Take the credentials of the user and log the user in.""" user = request.user + ci = RequestContext(request) if user.is_authenticated(): if user.groups.filter(name='moderator').count() > 0: return my_redirect('/exam/manage/') @@ -618,12 +626,12 @@ def user_login(request): else: context = {"form": form} return my_render_to_response('exam/login.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: form = UserLoginForm() context = {"form": form} return my_render_to_response('exam/login.html', context, - context_instance=RequestContext(request)) + context_instance=ci) def start(request, questionpaper_id=None): @@ -642,7 +650,7 @@ def start(request, questionpaper_id=None): return complete(request, msg, questionpaper_id) try: - old_paper = AnswerPaper.objects.get(\ + old_paper = AnswerPaper.objects.get( question_paper=questionpaper, user=user) q = old_paper.current_question() return show_question(request, q, questionpaper_id) @@ -669,7 +677,8 @@ def start(request, questionpaper_id=None): new_paper.save() return start(request, questionpaper_id) -def question(request, q_id, questionpaper_id,success_msg=None): + +def question(request, q_id, questionpaper_id, success_msg=None): """Check the credentials of the user and start the exam.""" user = request.user @@ -678,12 +687,12 @@ def question(request, q_id, questionpaper_id,success_msg=None): q = get_object_or_404(Question, pk=q_id) try: q_paper = QuestionPaper.objects.get(id=questionpaper_id) - paper = AnswerPaper.objects.get(\ + paper = AnswerPaper.objects.get( user=request.user, question_paper=q_paper) except AnswerPaper.DoesNotExist: return my_redirect('/exam/start/') if not paper.question_paper.quiz.active: - reason='The quiz has been deactivated!' + reason = 'The quiz has been deactivated!' return complete(request, reason, questionpaper_id) #if new: # paper.start_time = datetime.datetime.now() @@ -695,30 +704,28 @@ def question(request, q_id, questionpaper_id,success_msg=None): if success_msg is None: context = {'question': q, 'paper': paper, 'user': user, 'quiz_name': quiz_name, - 'time_left': time_left,} - + 'time_left': time_left, } else: context = {'question': q, 'paper': paper, 'user': user, 'quiz_name': quiz_name, 'time_left': time_left, - 'success_msg':success_msg} - + 'success_msg': success_msg} ci = RequestContext(request) - return my_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,success_msg=None): +def show_question(request, q_id, questionpaper_id, success_msg=None): """Show a question if possible.""" if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' - return complete(request, msg,questionpaper_id) + return complete(request, msg, questionpaper_id) else: - return question(request, q_id, questionpaper_id,success_msg) + return question(request, q_id, questionpaper_id, success_msg) def check(request, q_id, questionpaper_id=None): - """Checks the answers of the user for particular question""" + """Checks the answers of the user for particular question""" user = request.user if not user.is_authenticated(): @@ -734,10 +741,10 @@ def check(request, q_id, questionpaper_id=None): if skip is not None: next_q = paper.skip() return show_question(request, next_q, questionpaper_id) - + if question.type == 'mcq': # Add the answer submitted, regardless of it being correct or not. - if user_answer is not None : + if user_answer is not None: new_answer = Answer(question=question, answer=user_answer, correct=False) new_answer.save() @@ -756,7 +763,7 @@ def check(request, q_id, questionpaper_id=None): # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. if question.type == 'mcq': - if user_answer is not None: + if user_answer is not None: success = True # Only one attempt allowed for MCQ's. if user_answer.strip() == question.test.strip(): new_answer.correct = True @@ -765,10 +772,10 @@ def check(request, q_id, questionpaper_id=None): success_msg = True else: new_answer.error = 'Incorrect answer' - new_answer.save() + new_answer.save() else: user_dir = get_user_dir(user) - success, err_msg = code_server.run_code(answer_check, question.test, + success, err_msg = code_server.run_code(answer_check, question.test, user_dir, question.type) new_answer.error = err_msg if success: @@ -781,10 +788,10 @@ def check(request, q_id, questionpaper_id=None): time_left = paper.time_left() if not success: # Should only happen for non-mcq questions. if time_left == 0: - reason='Your time is up!' + reason = 'Your time is up!' return complete(request, reason, questionpaper_id) if not paper.question_paper.quiz.active: - reason='The quiz has been deactivated!' + reason = 'The quiz has been deactivated!' return complete(request, reason, questionpaper_id) context = {'question': question, 'error_message': err_msg, 'paper': paper, 'last_attempt': user_answer, @@ -792,27 +799,28 @@ def check(request, q_id, questionpaper_id=None): 'time_left': time_left} ci = RequestContext(request) - return my_render_to_response('exam/question.html', context, + return my_render_to_response('exam/question.html', context, context_instance=ci) else: if time_left <= 0: - reason='Your time is up!' + reason = 'Your time is up!' return complete(request, reason, questionpaper_id) else: next_q = paper.completed_question(question.id) - return show_question(request, next_q, questionpaper_id,success_msg) + return show_question(request, next_q, + questionpaper_id, success_msg) def quit(request, questionpaper_id=None): """Show the quit page when the user logs out.""" context = {'id': questionpaper_id} return my_render_to_response('exam/quit.html', context, - context_instance=RequestContext(request)) + context_instance=RequestContext(request)) def complete(request, reason=None, questionpaper_id=None): """Show a page to inform user that the quiz has been compeleted.""" - + user = request.user if questionpaper_id is None: logout(request) @@ -826,16 +834,16 @@ def complete(request, reason=None, questionpaper_id=None): tot_marks = paper.question_paper.total_marks if obt_marks == paper.question_paper.total_marks: context = {'message': "Hurray ! You did an excellent job.\ - you answered all the questions correctly.\ - You have been logged out successfully,\ - Thank You !"} + you answered all the questions correctly.\ + You have been logged out successfully,\ + Thank You !"} logout(request) - return my_render_to_response('exam/complete.html',context) + return my_render_to_response('exam/complete.html', context) else: message = reason or "You are successfully logged out" - context = {'message': message } + context = {'message': message} logout(request) - return my_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: @@ -856,16 +864,17 @@ def monitor(request, questionpaper_id=None): """Monitor the progress of the papers taken so far.""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: q_paper = QuestionPaper.objects.all() - context = {'papers': [], - 'quiz': None, + context = {'papers': [], + 'quiz': None, 'quizzes': q_paper} return my_render_to_response('exam/monitor.html', context, - context_instance=RequestContext(request)) + context_instance=ci) # quiz_id is not None. try: q_paper = QuestionPaper.objects.get(id=questionpaper_id) @@ -878,7 +887,7 @@ def monitor(request, questionpaper_id=None): context = {'papers': papers, 'quiz': q_paper, 'quizzes': None} return my_render_to_response('exam/monitor.html', context, - context_instance=RequestContext(request)) + context_instance=ci) def get_user_data(username): @@ -896,7 +905,7 @@ def get_user_data(username): profile = None data['user'] = user data['profile'] = profile - data['papers'] = papers + data['papers'] = papers return data @@ -918,6 +927,7 @@ def show_all_quiz(request): that are currently in the database.""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page !') @@ -926,20 +936,20 @@ def show_all_quiz(request): if data is None: quizzes = Quiz.objects.all() - context = {'papers': [], - 'quiz': None, + context = {'papers': [], + 'quiz': None, 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: for i in data: quiz = Quiz.objects.get(id=i).delete() quizzes = Quiz.objects.all() - context = {'papers': [], - 'quiz': None, + context = {'papers': [], + 'quiz': None, 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=ci) elif request.method == 'POST' and request.POST.get('edit') == 'edit': data = request.POST.getlist('quiz') @@ -962,20 +972,21 @@ def show_all_quiz(request): forms.append(form) return my_render_to_response('exam/edit_quiz.html', {'forms': forms, 'data': data}, - context_instance=RequestContext(request)) + context_instance=ci) else: quizzes = Quiz.objects.all() - context = {'papers': [], - 'quiz': None, + context = {'papers': [], + 'quiz': None, 'quizzes': quizzes} return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + context_instance=ci) def show_all_questions(request): """Show a list of all the questions currently in the databse.""" user = request.user + ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404("You are not allowed to view this page !") @@ -987,7 +998,7 @@ def show_all_questions(request): 'question': None, 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: for i in data: question = Question.objects.get(id=i).delete() @@ -996,7 +1007,7 @@ def show_all_questions(request): 'question': None, 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=ci) elif request.method == 'POST' and request.POST.get('edit') == 'edit': data = request.POST.getlist('question') @@ -1023,14 +1034,14 @@ def show_all_questions(request): forms.append(form) return my_render_to_response('exam/edit_question.html', {'forms': forms, 'data': data}, - context_instance=RequestContext(request)) + context_instance=ci) else: questions = Question.objects.all() context = {'papers': [], 'question': None, 'questions': questions} return my_render_to_response('exam/showquestions.html', context, - context_instance=RequestContext(request)) + context_instance=ci) def user_data(request, username): @@ -1052,6 +1063,7 @@ def grade_user(request, username): and update all their marks and also give comments for each paper. """ current_user = request.user + ci = RequestContext(request) if not current_user.is_authenticated() or not is_moderator(current_user): raise Http404('You are not allowed to view this page!') @@ -1064,14 +1076,14 @@ def grade_user(request, username): last_ans = answers[-1] last_ans.marks = marks last_ans.save() - paper.comments = request.POST.get(\ + paper.comments = request.POST.get( 'comments_%d' % paper.question_paper.id) paper.save() context = {'data': data} return my_render_to_response('exam/user_data.html', context, - context_instance=RequestContext(request)) + context_instance=ci) else: context = {'data': data} return my_render_to_response('exam/grade_user.html', context, - context_instance=RequestContext(request)) + context_instance=ci) -- cgit From e84e28166b085781c69326ae4feb936d3f6a9ded Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 5 Jun 2014 15:08:55 +0530 Subject: main files indented. comments removed from views file --- testapp/exam/views.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index f24215b..da4b5cd 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -694,9 +694,6 @@ def question(request, q_id, questionpaper_id, success_msg=None): if not paper.question_paper.quiz.active: reason = 'The quiz has been deactivated!' return complete(request, reason, questionpaper_id) - #if new: - # paper.start_time = datetime.datetime.now() - # paper.end_time = datetime.datetime.now() time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') -- cgit From 18cd0fa1def34bb320f38062596789f745bee530 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 5 Jun 2014 16:01:17 +0530 Subject: isDigit() issue resolved --- testapp/exam/views.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'testapp/exam/views.py') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index da4b5cd..92dd029 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -439,19 +439,17 @@ def automatic_questionpaper(request, questionpaper_id=None): questions = request.POST.getlist('questions') tot_marks = 0 for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - tot_marks += q.points + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - quest_paper.questions.add(q) + q = Question.objects.get(id=quest) + quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: - no_questions = int(request.POST.get('questions')) + no_questions = int(request.POST.get('num_questions')) fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' @@ -482,18 +480,16 @@ def automatic_questionpaper(request, questionpaper_id=None): questions = request.POST.getlist('questions') tot_marks = quest_paper.total_marks for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - tot_marks += q.points + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.total_marks = tot_marks quest_paper.save() for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - quest_paper.questions.add(q) + q = Question.objects.get(id=quest) + quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: - no_questions = int(request.POST.get('questions')) + no_questions = int(request.POST.get('num_questions')) fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' @@ -532,9 +528,8 @@ def manual_questionpaper(request, questionpaper_id=None): quiz = Quiz.objects.order_by("-id")[0] tot_marks = 0 for quest in questions: - if quest.isdigit(): - q = Question.objects.get(id=quest) - tot_marks += q.points + q = Question.objects.get(id=quest) + tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() -- cgit