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 | |
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')
-rw-r--r-- | yaksh/models.py | 1 | ||||
-rw-r--r-- | yaksh/static/yaksh/js/add_question.js | 67 | ||||
-rw-r--r-- | yaksh/templates/yaksh/add_question.html | 27 | ||||
-rw-r--r-- | yaksh/templates/yaksh/add_testcase.html | 39 | ||||
-rw-r--r-- | yaksh/templates/yaksh/show_testcase.html | 31 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 371 |
7 files changed, 250 insertions, 288 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index db4b7a8..d15fdf2 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -54,6 +54,7 @@ def get_model_class(model): ctype = ContentType.objects.get(app_label="yaksh", model=model) model_class = ctype.model_class() + return model_class ############################################################################### diff --git a/yaksh/static/yaksh/js/add_question.js b/yaksh/static/yaksh/js/add_question.js index 946c139..e435dde 100644 --- a/yaksh/static/yaksh/js/add_question.js +++ b/yaksh/static/yaksh/js/add_question.js @@ -109,34 +109,6 @@ function textareaformat() document.getElementById('my').innerHTML = document.getElementById('id_description').value ; }); - $('#id_test').bind('focus', function( event ){ - document.getElementById("id_test").rows=5; - document.getElementById("id_test").cols=40; - }); - - $('#id_test').bind('blur', function( event ){ - document.getElementById("id_test").rows=1; - document.getElementById("id_test").cols=40; - }); - - $('#id_options').bind('focus', function( event ){ - document.getElementById("id_options").rows=5; - document.getElementById("id_options").cols=40; - }); - $('#id_options').bind('blur', function( event ){ - document.getElementById("id_options").rows=1; - document.getElementById("id_options").cols=40; - }); - - $('#id_snippet').bind('focus', function( event ){ - document.getElementById("id_snippet").rows=5; - document.getElementById("id_snippet").cols=40; - }); - $('#id_snippet').bind('blur', function( event ){ - document.getElementById("id_snippet").rows=1; - document.getElementById("id_snippet").cols=40; - }); - $('#id_type').bind('focus', function(event){ var type = document.getElementById('id_type'); @@ -147,32 +119,7 @@ function textareaformat() var language = document.getElementById('id_language'); language.style.border = '1px solid #ccc'; }); - - $('#id_type').bind('change',function(event){ - var value = document.getElementById('id_type').value; - if(value == 'mcq' || value == 'mcc') - { - document.getElementById('id_options').style.visibility='visible'; - document.getElementById('label_option').innerHTML="Options :"; - } - else - { - document.getElementById('id_options').style.visibility='hidden'; - document.getElementById('label_option').innerHTML = ""; - } - }); - document.getElementById('my').innerHTML = document.getElementById('id_description').value ; - var value = document.getElementById('id_type').value; - if(value == 'mcq' || value == 'mcc') - { - document.getElementById('id_options').style.visibility='visible'; - document.getElementById('label_option').innerHTML="Options :" - } - else - { - document.getElementById('id_options').style.visibility='hidden'; - document.getElementById('label_option').innerHTML = ""; - } + document.getElementById('my').innerHTML = document.getElementById('id_description').value ; } function autosubmit() @@ -190,16 +137,4 @@ function autosubmit() return false; } - - if (type.value == 'mcq' || type.value == 'mcc') - { - var value = document.getElementById('id_options').value; - if(value.split('\n').length < 4) - { - alert("Please Enter 4 options. One option per line."); - return false; - } - return true; - } - } diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index 44aca9d..bba8db2 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -1,6 +1,5 @@ {% extends "manage.html" %} - {% block subtitle %}Add Question{% endblock %} {% block css %} @@ -30,23 +29,23 @@ <!-- <tr><td id='label_option'>Options: <td>{{ form.options }} {{form.options.errors}} --> <!-- <tr><td id='label_solution'>Test: <td>{{ form.test }} {{form.test.errors}} --> <!-- <tr><td id='label_ref_code_path'>Reference Code Path: <td>{{ form.ref_code_path }} {{form.ref_code_path.errors}} --> - <tr><td> test_case_type: <td> {{ form.test_case_type }}{{ form.test_case_type.errors }} - - <form method="post" action=""> - {% if formset%} - {{ formset.management_form }} - {% for form in formset %} - {{ form }} - {% endfor %} - {% endif %} - </form> + <tr><td> Test Case Type: <td> {{ form.test_case_type }}{{ form.test_case_type.errors }} + + <div class="form-group"> + {{ test_case_formset.management_form }} + + {% for form in test_case_formset %} + <div class="link-formset"> + {{ form }} + </div> + {% endfor %} + + </div> + </table></center> <center> <button class="btn" type="submit" name="save_question">Save</button> <button class="btn" type="button" name="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/questions/");'>Cancel</button> - {% if question_id %} - <button class="btn" type="button" name="testcase_button" onClick='location.replace("{{URL_ROOT}}/exam/manage/showtestcase/{{ question_id }}/");'>Go To TestCases</button> - {% endif %} </center> </form> {% endblock %} diff --git a/yaksh/templates/yaksh/add_testcase.html b/yaksh/templates/yaksh/add_testcase.html deleted file mode 100644 index 7e29765..0000000 --- a/yaksh/templates/yaksh/add_testcase.html +++ /dev/null @@ -1,39 +0,0 @@ -{% extends "manage.html" %} - -{% block subtitle %}Add Test Case{% endblock %} - -{% block css %} -<link rel="stylesheet" href="{{ URL_ROOT }}/static/yaksh/css/question_quiz.css" type="text/css" /> -{% endblock %} - -{% block manage %} -<form name=frm id=frm action="" method="post" > - {% csrf_token %} - <center> - <table class=span1> - {{ form.as_table }} - </table> - </center> - - <center><button class="btn primary" type="submit" id="submit" name="testcase">Save</button> - <button class="btn primary" type="button" name="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/showtestcase/{{question_id}}");'>Cancel</button> </center> -</form> -{% endblock %} - -<!-- {% comment %}{% extends "manage.html" %} - -{% block subtitle %}Add Testcase{% endblock %} - -{% block script %} -<script type="text/javascript" src="{{ URL_ROOT }}/static/yaksh/js/jquery-1.4.2.min.js"></script> -{% endblock %} - -{% block onload %} onload='javascript:textareaformat();' {% endblock %} - -{% block manage %} -<form name=testcase_frm id=testcase_frm action="" method="post"> -{{ form.as_table }} -</form> -<button class="btn" type="submit" name="save_testcase">Save</button> -{% endblock %}{% endcomment %} - --> diff --git a/yaksh/templates/yaksh/show_testcase.html b/yaksh/templates/yaksh/show_testcase.html deleted file mode 100644 index b9cb55f..0000000 --- a/yaksh/templates/yaksh/show_testcase.html +++ /dev/null @@ -1,31 +0,0 @@ -{% extends "manage.html" %} - -{% block title %} Test Case List {% endblock title %} - -{% block script %} -<script src="{{ URL_ROOT }}/static/yaksh/js/show_testcase.js"></script> -{% endblock %} - -{% block subtitle %} Test Case List {% endblock %} -{% block manage %} -{% if not test_cases %} -<center><h5> No Test Cases Available for This Question. </h5></center> -<button class="btn" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/testcase/{{ question_id }}");'>Add New Test Case</button> -{% endif %} - - -{% if test_cases %} -<form method="post" action="" name='frm'> -{% csrf_token %} - -{% for test_case in test_cases %} -<input type=checkbox name='test_case' value={{test_case.id}} /> <a href="{{URL_ROOT}}/exam/manage/testcase/{{ question_id }}/{{ test_case.id }}">{{ test_case }}</a><br> -{% endfor %} - -<br><br> -<button class="btn" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/testcase/{{ question_id }}");'>Add New Test Case</button> -<button class="btn" type="submit" name="delete" value='delete' onClick="return confirm_delete(frm);">Delete Selected</button> -</form> -{% endif %} - -{% endblock %} diff --git a/yaksh/urls.py b/yaksh/urls.py index 18a64c2..85d7fd1 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -43,7 +43,7 @@ urlpatterns += [ url(r'^manage/addquestion/$', views.add_question), url(r'^manage/addquestion/(?P<question_id>\d+)/$', views.add_question), url(r'^manage/addquiz/$', views.add_quiz), - url(r'^manage/addquiz/(?P<quiz_id>\d+)/$', views.add_quiz), + url(r'^manage/addquiz/(?P<quiz_id>\d+)/$', views.edit_quiz), url(r'^manage/gradeuser/$', views.grade_user), url(r'^manage/gradeuser/(?P<quiz_id>\d+)/$',views.grade_user), url(r'^manage/gradeuser/(?P<quiz_id>\d+)/(?P<user_id>\d+)/$',views.grade_user), 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. |