diff options
Diffstat (limited to 'testapp/exam/views.py')
-rw-r--r-- | testapp/exam/views.py | 385 |
1 files changed, 207 insertions, 178 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py index a529983..325d0aa 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -21,7 +21,6 @@ from settings import URL_ROOT # The directory where user data can be saved. OUTPUT_DIR = abspath(join(dirname(__file__), pardir, 'output')) - def my_redirect(url): """An overridden redirect to deal with URL_ROOT-ing. See settings.py for details.""" @@ -36,7 +35,6 @@ def my_render_to_response(template, context=None, **kwargs): 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 @@ -44,10 +42,11 @@ def gen_key(no_of_chars): def get_user_dir(user): """Return the output directory for the user.""" + user_dir = join(OUTPUT_DIR, str(user.username)) if not exists(user_dir): os.mkdir(user_dir) - # Make it rwx by others. + # 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) @@ -58,8 +57,8 @@ def index(request): """ user = request.user if user.is_authenticated(): - if user.groups.filter(name='moderator').count() > 0: - return my_redirect('/exam/manage/') + if user.groups.filter(name='moderator').count() > 0: + return my_redirect('/exam/manage/') return my_redirect("/exam/start/") return my_redirect("/exam/login/") @@ -81,7 +80,7 @@ def user_register(request): new_user = authenticate(username = u_name, password = pwd) login(request, new_user) return my_redirect("/exam/quizlist/") - + else: return my_render_to_response('exam/register.html', {'form':form}, @@ -93,108 +92,132 @@ def user_register(request): context_instance=RequestContext(request)) def edit_quiz(request): - """Edit the list of quizzes seleted by the user for editing.""" + """Edit the list of quizzes seleted by the user for editing.""" - 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!') + 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!') - start_date = request.POST.getlist('start_date') - duration = request.POST.getlist('duration') - active = request.POST.getlist('active') - description = request.POST.getlist('description') - - j = 0 - for i in quizlist: - quiz = Quiz.objects.get(id=i) - quiz.start_date = start_date[j] - quiz.duration = duration[j] - quiz.active = active[j] - quiz.description = description[j] - quiz.save() - j += 1 - return my_redirect("/exam/manage/showquiz/") + start_date = request.POST.getlist('start_date') + duration = request.POST.getlist('duration') + active = request.POST.getlist('active') + description = request.POST.getlist('description') + + j = 0 + for i in quizlist: + quiz = Quiz.objects.get(id=i) + quiz.start_date = start_date[j] + quiz.duration = duration[j] + quiz.active = active[j] + quiz.description = description[j] + quiz.save() + j += 1 + return my_redirect("/exam/manage/showquiz/") def edit_question(request): - """Edit the list of quizzes seleted by the user for editing.""" - 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!') - - summary = request.POST.getlist('summary') - description = request.POST.getlist('description') - points = request.POST.getlist('points') - test = request.POST.getlist('test') - options = request.POST.getlist('options') - type = request.POST.getlist('type') - active = request.POST.getlist('active') - j = 0 - for id_list in editquestionlist: - question = Question.objects.get(id=id_list) - question.summary = summary[j] - question.description = description[j] - question.points = points[j] - question.test = test[j] - question.options = options[j] - question.type = type[j] - question.active = active[j] - question.save() - j += 1 - return my_redirect("/exam/manage/questions") + """Edit the list of questions seleted by the user for editing.""" + 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!') + + summary = request.POST.getlist('summary') + description = request.POST.getlist('description') + points = request.POST.getlist('points') + test = request.POST.getlist('test') + options = request.POST.getlist('options') + type = request.POST.getlist('type') + active = request.POST.getlist('active') + tags = request.POST.getlist('tags') + j = 0 + for id_list in editquestionlist: + question = Question.objects.get(id=id_list) + question.summary = summary[j] + question.description = description[j] + 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.save() + for tag in question.tags.all(): + question.tags.remove(tag) + tags_split = edit_tags.split(',') + for i in range(0,len(tags_split)-1): + tag = tags_split[i].strip() + question.tags.add(tag) + j += 1 + return my_redirect("/exam/manage/questions") 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 if not user.is_authenticated() or user.groups.filter(name='moderator').count() == 0 : - raise Http404('You are not allowed to view this page!') + raise Http404('You are not allowed to view this page!') if request.method == "POST": - form = QuestionForm(request.POST) + form = QuestionForm(request.POST) if form.is_valid(): data = form.cleaned_data - if question_id == None: + if question_id == None: form.save() question = Question.objects.order_by("-id")[0] - question.tags.add(form['tags'].data) - return my_redirect("/exam/manage/questions") - else: - d = Question.objects.get(id=question_id) - d.summary = form['summary'].data - d.description = form['description'].data - d.points = form['points'].data - d.test = form['test'].data - d.options = form['options'].data - d.type = form['type'].data - d.active = form['active'].data - d.save() - return my_redirect("/exam/manage/questions") + tags = form['tags'].data.split(',') + for i in range(0,len(tags)-1): + tag = tags[i].strip() + question.tags.add(tag) + return my_redirect("/exam/manage/questions") + + else: + d = Question.objects.get(id=question_id) + d.summary = form['summary'].data + d.description = form['description'].data + d.points = form['points'].data + d.test = form['test'].data + d.options = form['options'].data + d.type = form['type'].data + d.active = form['active'].data + d.save() + question = Question.objects.get(id=question_id) + for tag in question.tags.all(): + question.tags.remove(tag) + tags = form['tags'].data.split(',') + for i in range(0,len(tags)-1): + tag = tags[i].strip() + question.tags.add(tag) + return my_redirect("/exam/manage/questions") else: return my_render_to_response('exam/add_question.html', {'form':form}, context_instance=RequestContext(request)) else: - if question_id == None: + if question_id == None: form = QuestionForm() return my_render_to_response('exam/add_question.html', {'form':form}, context_instance=RequestContext(request)) - else: + else: - d = Question.objects.get(id=question_id) - form = QuestionForm() - form.initial['summary']= d.summary - form.initial['description'] = d.description - form.initial['points']= d.points - form.initial['test'] = d.test - form.initial['options'] = d.options - form.initial['type'] = d.type - form.initial['active'] = d.active - - return my_render_to_response('exam/add_question.html', - {'form':form}, - context_instance=RequestContext(request)) + d = Question.objects.get(id=question_id) + form = QuestionForm() + form.initial['summary']= d.summary + form.initial['description'] = d.description + form.initial['points']= d.points + form.initial['test'] = d.test + form.initial['options'] = d.options + form.initial['type'] = d.type + form.initial['active'] = d.active + form_tags = d.tags.all() + form_tags_split = form_tags.values('name') + initial_tags = "" + + for tag in form_tags_split: + initial_tags = initial_tags + str(tag['name']).strip() + "," + if (initial_tags == ","): + initial_tags = "" + form.initial['tags']=initial_tags + return my_render_to_response('exam/add_question.html',{'form':form},context_instance=RequestContext(request)) def add_quiz(request,quiz_id=None): @@ -202,54 +225,55 @@ def add_quiz(request,quiz_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!') + raise Http404('You are not allowed to view this page!') if request.method == "POST": - form = QuizForm(request.POST) + form = QuizForm(request.POST) if form.is_valid(): data = form.cleaned_data - if quiz_id == None: + if quiz_id == None: form.save() - return my_redirect("/exam/manage/showquiz") - else: - d = Quiz.objects.get(id=quiz_id) - d.start_date = form['start_date'].data - d.duration = form['duration'].data - d.active = form['active'].data - d.description = form['description'].data - d.save() - return my_redirect("/exam/manage/showquiz") + quiz = Quiz.objects.order_by("-id")[0] + tags = form['tags'].data.split(',') + for tag in tags: + tag = tag.strip() + quiz.tags.add(tag) + return my_redirect("/exam/manage/showquiz") + else: + d = Quiz.objects.get(id=quiz_id) + d.start_date = form['start_date'].data + d.duration = form['duration'].data + d.active = form['active'].data + d.description = form['description'].data + d.save() + return my_redirect("/exam/manage/showquiz") else: return my_render_to_response('exam/add_quiz.html', {'form':form}, context_instance=RequestContext(request)) else: - if quiz_id == None: + if quiz_id == None: form = QuizForm() return my_render_to_response('exam/add_quiz.html', {'form':form}, context_instance=RequestContext(request)) - else: - - d = Quiz.objects.get(id=quiz_id) - form = QuizForm() - form.initial['start_date']= d.start_date - form.initial['duration'] = d.duration - form.initial['description']= d.description - form.initial['active'] = d.active - - return my_render_to_response('exam/add_quiz.html', - {'form':form}, - context_instance=RequestContext(request)) + else: + d = Quiz.objects.get(id=quiz_id) + form = QuizForm() + form.initial['start_date']= d.start_date + form.initial['duration'] = d.duration + form.initial['description']= d.description + form.initial['active'] = d.active + return my_render_to_response('exam/add_quiz.html',{'form':form},context_instance=RequestContext(request)) def prof_manage(request): """Take credentials of the user with professor/moderator rights/permissions and log in.""" - + 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) + context = {'user':user} + return render_to_response('manage.html',context) return my_redirect('/exam/login/') def user_login(request): @@ -257,8 +281,8 @@ def user_login(request): user = request.user if user.is_authenticated(): - if user.groups.filter(name='moderator').count() > 0 : - return my_redirect('/exam/manage/') + if user.groups.filter(name='moderator').count() > 0 : + return my_redirect('/exam/manage/') return my_redirect("/exam/quizlist/") if request.method == "POST": @@ -266,8 +290,8 @@ def user_login(request): if form.is_valid(): user = form.cleaned_data login(request, user) - if user.groups.filter(name='moderator').count() > 0 : - return my_redirect('/exam/manage/') + if user.groups.filter(name='moderator').count() > 0 : + return my_redirect('/exam/manage/') return my_redirect('/exam/login/') else: context = {"form": form} @@ -289,7 +313,7 @@ def start(request,quiz_id=None): quiz = Quiz.objects.get(id=quiz_id) except Quiz.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: @@ -432,7 +456,7 @@ def complete(request,reason = None): 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. Thanks for spending some time with the application' + message = 'You are successfully Logged out. Thanks for spending some time with the application' if request.method == 'POST' and 'no' in request.POST: no = True if not no: @@ -494,11 +518,10 @@ def show_all_users(request): 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 !') + raise Http404('You are not allowed to view this page !') user = User.objects.filter(username__contains="") questionpaper = QuestionPaper.objects.all() context = { 'question': questionpaper } - print context return my_render_to_response('exam/showusers.html',context,context_instance=RequestContext(request)) def quizlist(request): @@ -512,59 +535,58 @@ def quizlist(request): context_instance=RequestContext(request)) - - def show_all_quiz(request): """Generates a list of all the quizzes that are currently in the database.""" 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 !') + raise Http404('You are not allowed to view this page !') if request.method == 'POST' and request.POST.get('delete')=='delete': - data = request.POST.getlist('quiz') - if data == None: - quizzes = Quiz.objects.all() + data = request.POST.getlist('quiz') + + if data == None: + quizzes = Quiz.objects.all() context = {'papers': [], 'quiz': None, 'quizzes':quizzes} return my_render_to_response('exam/show_quiz.html', context, context_instance=RequestContext(request)) - for i in data: - quiz = Quiz.objects.get(id=i).delete() - quizzes = Quiz.objects.all() - context = {'papers': [], + else: + for i in data: + quiz = Quiz.objects.get(id=i).delete() + quizzes = Quiz.objects.all() + context = {'papers': [], 'quiz': None, 'quizzes':quizzes} - return my_render_to_response('exam/show_quiz.html', context, - context_instance=RequestContext(request)) + return my_render_to_response('exam/show_quiz.html', context, + context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit')=='edit': data = request.POST.getlist('quiz') - global quizlist - quizlist = data - if data == None: + global quizlist + quizlist = data + if data == None: quiz = Quiz.objects.all() context = {'papers': [], - 'quiz': None, - 'quizzes':quiz} + 'quiz': None, + 'quizzes':quiz} return my_render_to_response('exam/showquiz.html', context, context_instance=RequestContext(request)) - - forms = [] - for j in data: - d = Quiz.objects.get(id=j) - form = QuizForm() - form.initial['start_date']= d.start_date - form.initial['duration'] = d.duration - form.initial['active']= d.active - form.initial['description'] = d.description - forms.append(form) - - return my_render_to_response('exam/edit_quiz.html', + else: + forms = [] + for j in data: + d = Quiz.objects.get(id=j) + form = QuizForm() + form.initial['start_date']= d.start_date + form.initial['duration'] = d.duration + form.initial['active']= d.active + form.initial['description'] = d.description + forms.append(form) + return my_render_to_response('exam/edit_quiz.html', {'forms':forms}, context_instance=RequestContext(request)) - + else: quizzes = Quiz.objects.all() context = {'papers': [], @@ -579,7 +601,7 @@ def show_all_questions(request): 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 !") + raise Http404("You are not allowed to view this page !") if request.method == 'POST' and request.POST.get('delete')=='delete': data = request.POST.getlist('question') @@ -590,43 +612,50 @@ def show_all_questions(request): 'questions':questions} return my_render_to_response('exam/showquestions.html', context, context_instance=RequestContext(request)) - for i in data: - question = Question.objects.get(id=i).delete() - questions = Question.objects.all() - context = {'papers': [], - 'question': None, - 'questions':questions} - return my_render_to_response('exam/showquestions.html', context, + else: + for i in data: + question = Question.objects.get(id=i).delete() + questions = Question.objects.all() + context = {'papers': [], + 'question': None, + 'questions':questions} + return my_render_to_response('exam/showquestions.html', context, context_instance=RequestContext(request)) elif request.method == 'POST' and request.POST.get('edit')=='edit': data = request.POST.getlist('question') - global editquestionlist - editquestionlist = data - if data == None: + global editquestionlist + editquestionlist = data + if data == 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)) - - forms = [] - for j in data: - d = Question.objects.get(id=j) - form = QuestionForm() - form.initial['summary']= d.summary - form.initial['description'] = d.description - form.initial['points']= d.points - form.initial['test'] = d.test - form.initial['options'] = d.options - form.initial['type'] = d.type - form.initial['active'] = d.active - forms.append(form) - - return my_render_to_response('exam/edit_question.html', - {'forms':forms}, - context_instance=RequestContext(request)) + else: + forms = [] + for j in data: + d = Question.objects.get(id=j) + form = QuestionForm() + form.initial['summary']= d.summary + form.initial['description'] = d.description + form.initial['points']= d.points + form.initial['test'] = d.test + form.initial['options'] = d.options + form.initial['type'] = d.type + form.initial['active'] = d.active + form_tags = d.tags.all() + form_tags_split = form_tags.values('name') + initial_tags = "" + for tag in form_tags_split: + initial_tags = initial_tags + str(tag['name']).strip() + "," + if (initial_tags == ","): + initial_tags = "" + form.initial['tags']=initial_tags + forms.append(form) + return my_render_to_response('exam/edit_question.html',{'forms':forms},context_instance=RequestContext(request)) + else: questions = Question.objects.all() context = {'papers': [], |