From 762d5ed41922150df1f91ef1835ea03a192f7d28 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Tue, 10 May 2016 17:46:52 +0530 Subject: added view functions for godmode, usermode and test_questions --- yaksh/views.py | 87 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/yaksh/views.py b/yaksh/views.py index 2a3adbf..019b631 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -6,7 +6,7 @@ from os.path import dirname, pardir, abspath, join, exists from datetime import datetime import collections import csv -from django.http import HttpResponse +from django.http import HttpResponse, HttpRequest from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect from django.template import RequestContext @@ -123,7 +123,7 @@ def quizlist_user(request): user = request.user avail_quizzes = Quiz.objects.get_active_quizzes() user_answerpapers = AnswerPaper.objects.filter(user=user) - courses = Course.objects.filter(active=True) + courses = Course.objects.filter(active=True, is_trial=False) context = { 'quizzes': avail_quizzes, 'user': user, @@ -225,7 +225,7 @@ def add_quiz(request, quiz_id=None): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page!') - + context = {} if request.method == "POST": if quiz_id is None: form = QuizForm(request.POST, user=user) @@ -237,9 +237,11 @@ def add_quiz(request, quiz_id=None): form = QuizForm(request.POST, user=user, instance=quiz) if form.is_valid(): form.save() + context["quiz_id"]=quiz_id return my_redirect("/exam/manage/") - return my_render_to_response('yaksh/add_quiz.html', - {'form': form}, + + context["form"] = form + return my_render_to_response('yaksh/add_quiz.html', context, context_instance=ci) else: if quiz_id is None: @@ -247,8 +249,10 @@ def add_quiz(request, quiz_id=None): else: quiz = Quiz.objects.get(id=quiz_id) form = QuizForm(user=user, instance=quiz) + context["quiz_id"]=quiz_id + context["form"] = form return my_render_to_response('yaksh/add_quiz.html', - {'form': form}, + context, context_instance=ci) @@ -260,12 +264,12 @@ def show_all_questionpapers(request, questionpaper_id=None): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: - qu_papers = QuestionPaper.objects.all() + qu_papers = QuestionPaper.objects.filter(is_trial=False) context = {'papers': qu_papers} return my_render_to_response('yaksh/showquestionpapers.html', context, context_instance=ci) else: - qu_papers = QuestionPaper.objects.get(id=questionpaper_id) + qu_papers = QuestionPaper.objects.get(id=questionpaper_id,is_trial=False) quiz = qu_papers.quiz fixed_questions = qu_papers.fixed_questions.all() random_questions = qu_papers.random_questions.all() @@ -281,7 +285,11 @@ def prof_manage(request): rights/permissions and log in.""" user = request.user if user.is_authenticated() and is_moderator(user): - question_papers = QuestionPaper.objects.filter(quiz__course__creator=user) + question_papers = QuestionPaper.objects.filter(quiz__course__creator=user, + quiz__is_trial=False + ) + trial_course = Course.objects.delete_trial_course(user) + trial_quiz = Quiz.objects.delete_trial_quiz(user) users_per_paper = [] for paper in question_papers: answer_papers = AnswerPaper.objects.filter(question_paper=paper) @@ -580,7 +588,7 @@ def add_course(request): def enroll_request(request, course_id): user = request.user ci = RequestContext(request) - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) course.request(user) return my_redirect('/exam/manage/') @@ -589,7 +597,7 @@ def enroll_request(request, course_id): def self_enroll(request, course_id): user = request.user ci = RequestContext(request) - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) if course.is_self_enroll(): was_rejected = False course.enroll(was_rejected, user) @@ -602,7 +610,7 @@ def courses(request): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - courses = Course.objects.filter(creator=user) + courses = Course.objects.filter(creator=user, is_trial=False) return my_render_to_response('yaksh/courses.html', {'courses': courses}, context_instance=ci) @@ -615,7 +623,7 @@ def course_detail(request, course_id): if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') @@ -630,7 +638,7 @@ def enroll(request, course_id, user_id=None, was_rejected=False): if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') @@ -653,7 +661,7 @@ def reject(request, course_id, user_id=None, was_enrolled=False): if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') @@ -675,7 +683,7 @@ def toggle_course_status(request, course_id): if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, pk=course_id, is_trial=False) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') @@ -723,8 +731,10 @@ def monitor(request, questionpaper_id=None): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: - q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user)| - Q(quiz__course__teachers=user)).distinct() + q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) + | Q(quiz__course__teachers=user), + quiz__course__is_trial=False + ).distinct() context = {'papers': [], 'quiz': None, 'quizzes': q_paper} @@ -733,7 +743,7 @@ def monitor(request, questionpaper_id=None): # quiz_id is not None. try: q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user)| - Q(quiz__course__teachers=user), + Q(quiz__course__teachers=user), quiz__course__is_trial=False, id=questionpaper_id).distinct() except QuestionPaper.DoesNotExist: papers = [] @@ -834,6 +844,13 @@ def show_all_questions(request): else: msg = "Please select atleast one question" context['msg'] = msg + if request.POST.get('test') == 'test': + question_ids = request.POST.getlist("question") + trial_paper = test_mode(user, "test_questions", question_ids, None) + trial_paper.update_total_marks() + trial_paper.save() + return my_redirect("/exam/start/{0}".format(trial_paper.id)) + questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) @@ -866,7 +883,7 @@ def download_csv(request, questionpaper_id): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') - quiz = Quiz.objects.get(questionpaper=questionpaper_id) + quiz = Quiz.objects.get(questionpaper=questionpaper_id,is_trial=False) if not quiz.course.is_creator(user) and not quiz.course.is_teacher(user): raise Http404('The question paper does not belong to your course') @@ -917,7 +934,7 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): if not current_user.is_authenticated() or not is_moderator(current_user): raise Http404('You are not allowed to view this page!') course_details = Course.objects.filter(Q(creator=current_user)| - Q(teachers=current_user)).distinct() + Q(teachers=current_user), is_trial=False).distinct() context = {"course_details": course_details} if quiz_id is not None: questionpaper_id = QuestionPaper.objects.filter(quiz_id=quiz_id)\ @@ -1099,7 +1116,7 @@ def search_teacher(request, course_id): raise Http404('You are not allowed to view this page!') context = {} - course = get_object_or_404(Course, creator=user, pk=course_id) + course = get_object_or_404(Course, creator=user, pk=course_id, is_trial=False) context['course'] = course if request.method == 'POST': @@ -1131,7 +1148,7 @@ def add_teacher(request, course_id): raise Http404('You are not allowed to view this page!') context = {} - course = get_object_or_404(Course, creator=user, pk=course_id) + course = get_object_or_404(Course, creator=user, pk=course_id, is_trial=False) context['course'] = course if request.method == 'POST': @@ -1157,7 +1174,7 @@ def allotted_courses(request): if not is_moderator(user): raise Http404('You are not allowed to view this page!') - courses = Course.objects.filter(teachers=user) + courses = Course.objects.filter(teachers=user, is_trial=False) return my_render_to_response('yaksh/courses.html', {'courses': courses}, context_instance=ci) @@ -1170,9 +1187,29 @@ def remove_teachers(request, course_id): if not is_moderator(user): raise Http404('You are not allowed to view this page!') - course = get_object_or_404(Course, creator=user, pk=course_id) + course = get_object_or_404(Course, creator=user, pk=course_id, is_trial=False) if request.method == "POST": teacher_ids = request.POST.getlist('remove') teachers = User.objects.filter(id__in=teacher_ids) course.remove_teachers(*teachers) return my_redirect('/exam/manage/courses') + +def test_mode(user, mode, questions_list=None, quiz_id=None): + if questions_list is not None and mode == "test_questions": + trial_course = Course.objects.create_trial_course(user) + trial_quiz = Quiz.objects.create_trial_quiz(trial_course,user) + trial_questionpaper = QuestionPaper.objects.add_details_trial_questionpaper(trial_quiz, + None, questions_list + ) + else: + trial_quiz = Quiz.objects.copy_original_quiz(quiz_id, user, mode) + trial_questionpaper = QuestionPaper.objects.add_details_trial_questionpaper(trial_quiz, + quiz_id, None + ) + return trial_questionpaper + +@login_required +def test_quiz(request, mode,quiz_id): + current_user = request.user + trial_questionpaper = test_mode(current_user, mode, None, quiz_id) + return my_redirect("/exam/start/{0}".format(trial_questionpaper.id)) \ No newline at end of file -- cgit