diff options
author | ankitjavalkar | 2016-04-28 18:19:09 +0530 |
---|---|---|
committer | ankitjavalkar | 2016-05-05 19:20:23 +0530 |
commit | 5967e0e1cce19d356ba622719b332f776127b277 (patch) | |
tree | 8a98600738d1ce586f275ee52e51f09e1d9dc2ce /yaksh/views.py | |
parent | 23216995e0fc0e94fd58c6186eed74c943ae5081 (diff) | |
download | online_test-5967e0e1cce19d356ba622719b332f776127b277.tar.gz online_test-5967e0e1cce19d356ba622719b332f776127b277.tar.bz2 online_test-5967e0e1cce19d356ba622719b332f776127b277.zip |
- Add formsets to Add question templates
- add/modify test cases in place when adding questions
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 371 |
1 files changed, 234 insertions, 137 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 5f07880..e478cfe 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -15,11 +15,12 @@ from django.db.models import Sum, Max, Q from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required from django.contrib.auth.models import Group +from django.forms.models import inlineformset_factory from taggit.models import Tag from itertools import chain import json # Local imports. -from yaksh.models import Quiz, Question, QuestionPaper, QuestionSet, Course +from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ @@ -143,165 +144,261 @@ def results_user(request): @login_required -def add_question(request, question_id=None): +def add_question(request): """To add a new question in the database. Create a new question and store it.""" - - # def add_or_delete_test_form(post_request, instance): - # request_copy = post_request.copy() - # if 'add_test' in post_request: - # request_copy['test-TOTAL_FORMS'] = int(request_copy['test-TOTAL_FORMS']) + 1 - # elif 'delete_test' in post_request: - # request_copy['test-TOTAL_FORMS'] = int(request_copy['test-TOTAL_FORMS']) - 1 - # test_case_formset = TestCaseFormSet(request_copy, prefix='test', instance=instance) - # return test_case_formset - 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": - form = QuestionForm(request.POST) - if form.is_valid(): - if question_id is None: - # test_case_formset = add_or_delete_test_form(request.POST, form.save(commit=False)) - if 'save_question' in request.POST: - qtn = form.save(commit=False) - qtn.user = user - qtn.save() - test_case_formset = TestCaseFormSet(request.POST, prefix='test', instance=qtn) - form.save() - question = Question.objects.order_by("-id")[0] - if test_case_formset.is_valid(): - test_case_formset.save() - else: - return my_render_to_response('yaksh/add_question.html', - {'form': form, - 'formset': test_case_formset}, - context_instance=ci) - return my_redirect("/exam/manage/questions") - - return my_render_to_response('yaksh/add_question.html', - # {'form': form}, - {'form': form, - 'question_id': question_id}, - context_instance=ci) - else: - d = Question.objects.get(id=question_id) - if 'save_question' in request.POST: - qtn = form.save(commit=False) - form.save() - question = Question.objects.get(id=question_id) - return my_redirect("/exam/manage/questions") - return my_render_to_response('yaksh/add_question.html', - # {'form': form}, - {'form': form, - 'question_id': question_id}, - context_instance=ci) + if request.method == "POST" and 'save_question' in request.POST: + question_form = QuestionForm(request.POST) + if question_form.is_valid(): + new_question = question_form.save() + tags = question_form['tags'].data.split(',') + for i in range(0, len(tags)): + tag = tags[i].strip() + new_question.tags.add(tag) + new_question.save() + return my_redirect("/exam/manage/questions") else: return my_render_to_response('yaksh/add_question.html', - # {'form': form}, - {'form': form, - 'question_id': question_id}, + {'form': question_form}, context_instance=ci) else: - if question_id is None: - form = QuestionForm() - # test_case_formset = TestCaseFormSet(prefix='test', instance=Question()) - return my_render_to_response('yaksh/add_question.html', - # {'form': form}, - {'form': form, - 'question_id': question_id}, - context_instance=ci) - else: - d = Question.objects.get(id=question_id) - form = QuestionForm(instance=d) - return my_render_to_response('yaksh/add_question.html', - # {'form': form}, - {'form': form, - 'question_id': question_id}, - context_instance=ci) - -@login_required -def show_testcase(request, question_id=None): - """Show all test cases related to Questions""" - - 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 not question_id: - raise Http404('No Question Found') - - question = Question.objects.get(id=question_id) - test_cases = question.get_test_cases() - - if request.POST.get('delete') == 'delete': - data = request.POST.getlist('test_case') - for i in data: - for t in test_cases: - if int(i) == t.id: - test_case_deleted = t.delete() - test_cases = question.get_test_cases() - - return my_render_to_response('yaksh/show_testcase.html', - {'test_cases': test_cases, - 'question_id': question_id}, - context_instance=ci) - + question_form = QuestionForm() + return my_render_to_response('yaksh/add_question.html', + {'form': question_form}, + context_instance=ci) @login_required -def add_testcase(request, question_id=None, test_case_id=None): - """To add new test case for a question""" - +def edit_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 not question_id: raise Http404('No Question Found') - question = Question.objects.get(id=question_id) - test_case_type = question.test_case_type - - if test_case_id: - instance = question.get_test_case(test_case_id) - else: - instance = None - - # test_cases = self.testcase_set.all() - # for test in test_cases: - # test_case_child_instance = test.get_child_instance(test_case_type) - - test_case_form_object = get_object_form(model=test_case_type, exclude_fields=['question']) - - # if test_case_type == "standardtestcase": - # from yaksh.forms import StandardTestCaseForm - # if request.method == "POST": - # form = StandardTestCaseForm(request.POST) - # form = StandardTestCaseForm(initial) - - if request.method == "POST": - form = test_case_form_object(request.POST, instance=instance) - if form.is_valid(): - form_data = form.save(commit=False) - form_data.question = question - form_data.save() - return my_redirect("/exam/manage/showtestcase/{0}".format(question_id)) + question_instance = Question.objects.get(id=question_id) + + if request.method == "POST" and 'save_question' in request.POST: + question_form = QuestionForm(request.POST, instance=question_instance) + if question_form.is_valid(): + new_question = question_form.save(commit=False) + tags = question_form['tags'].data.split(',') + for i in range(0, len(tags)): + tag = tags[i].strip() + new_question.tags.add(tag) + # new_question.save() + test_case_type = question_form.cleaned_data.get('test_case_type') + test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question']) + test_case_model_class = get_model_class(test_case_type) + TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class) + test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=new_question) + if test_case_formset.is_valid(): + new_question.save() + test_case_formset.save() + return my_redirect("/exam/manage/questions") else: - return my_render_to_response('yaksh/add_testcase.html', - {'form': form, + return my_render_to_response('yaksh/add_question.html', + {'form': question_form, + 'test_case_formset': test_case_formset, 'question_id': question_id}, context_instance=ci) - else: - form = test_case_form_object(initial={"question": question}, instance=instance) - return my_render_to_response('yaksh/add_testcase.html', - {'form': form, + question_form = QuestionForm(instance=question_instance) + test_case_type = question_instance.test_case_type + test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question']) + test_case_model_class = get_model_class(test_case_type) + TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class) + test_case_formset = TestCaseInlineFormSet(instance=question_instance) + + return my_render_to_response('yaksh/add_question.html', + {'form': question_form, + 'test_case_formset': test_case_formset, 'question_id': question_id}, context_instance=ci) +# def add_question(request, question_id=None): +# 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": +# form = QuestionForm(request.POST) +# if form.is_valid(): +# if question_id is None: +# if 'save_question' in request.POST: +# form.save() +# question = Question.objects.order_by("-id")[0] +# 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") + +# return my_render_to_response('yaksh/add_question.html', +# # {'form': form}, +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) + +# else: +# d = Question.objects.get(id=question_id) +# if 'save_question' in request.POST: +# d.summary = form['summary'].data +# d.description = form['description'].data +# d.points = form['points'].data +# # d.options = form['options'].data +# d.type = form['type'].data +# d.active = form['active'].data +# d.language = form['language'].data +# # d.snippet = form['snippet'].data +# # d.ref_code_path = form['ref_code_path'].data +# # d.test = form['test'].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") + +# return my_render_to_response('yaksh/add_question.html', +# # {'form': form}, +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) + +# else: +# return my_render_to_response('yaksh/add_question.html', +# # {'form': form}, +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) +# else: +# form = QuestionForm() +# if question_id is None: +# form = QuestionForm() +# return my_render_to_response('yaksh/add_question.html', +# # {'form': form}, +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) +# 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['options'] = d.options +# form.initial['type'] = d.type +# form.initial['active'] = d.active +# form.initial['language'] = d.language +# # form.initial['snippet'] = d.snippet +# # form.initial['ref_code_path'] = d.ref_code_path +# # form.initial['test'] = d.test +# 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('yaksh/add_question.html', +# # {'form': form}, +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) + +# @login_required +# def show_testcase(request, question_id=None): +# """Show all test cases related to Questions""" + +# 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 not question_id: +# raise Http404('No Question Found') + +# question = Question.objects.get(id=question_id) +# test_cases = question.get_test_cases() + +# if request.POST.get('delete') == 'delete': +# data = request.POST.getlist('test_case') +# for i in data: +# for t in test_cases: +# if int(i) == t.id: +# test_case_deleted = t.delete() +# test_cases = question.get_test_cases() + +# return my_render_to_response('yaksh/show_testcase.html', +# {'test_cases': test_cases, +# 'question_id': question_id}, +# context_instance=ci) + + +# @login_required +# def add_testcase(request, question_id=None, test_case_id=None): +# """To add new test case for a question""" + +# 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 not question_id: +# raise Http404('No Question Found') + +# question = Question.objects.get(id=question_id) +# test_case_type = question.test_case_type + +# if test_case_id: +# instance = question.get_test_case(test_case_id) +# else: +# instance = None + +# # test_cases = self.testcase_set.all() +# # for test in test_cases: +# # test_case_child_instance = test.get_child_instance(test_case_type) + +# test_case_form_object = get_object_form(model=test_case_type, exclude_fields=['question']) + +# # if test_case_type == "standardtestcase": +# # from yaksh.forms import StandardTestCaseForm +# # if request.method == "POST": +# # form = StandardTestCaseForm(request.POST) +# # form = StandardTestCaseForm(initial) + +# if request.method == "POST": +# form = test_case_form_object(request.POST, instance=instance) +# if form.is_valid(): +# form_data = form.save(commit=False) +# form_data.question = question +# form_data.save() +# return my_redirect("/exam/manage/showtestcase/{0}".format(question_id)) +# else: +# return my_render_to_response('yaksh/add_testcase.html', +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) + +# else: +# form = test_case_form_object(initial={"question": question}, instance=instance) +# return my_render_to_response('yaksh/add_testcase.html', +# {'form': form, +# 'question_id': question_id}, +# context_instance=ci) + @login_required def add_quiz(request, quiz_id=None): """To add a new quiz in the database. |