diff options
-rw-r--r-- | yaksh/models.py | 8 | ||||
-rw-r--r-- | yaksh/templates/yaksh/add_question.html | 69 | ||||
-rw-r--r-- | yaksh/templates/yaksh/new_question.html | 86 | ||||
-rw-r--r-- | yaksh/urls.py | 4 | ||||
-rw-r--r-- | yaksh/views.py | 195 |
5 files changed, 119 insertions, 243 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 7a64bba..77e77ee 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1126,8 +1126,8 @@ class TestCase(models.Model): type = models.CharField(max_length=24, choices=test_case_types, null=True) class StandardTestCase(TestCase): - test_case = models.CharField(blank=True, max_length=100) - weight = models.FloatField(default=0.0) + test_case = models.TextField() + weight = models.FloatField(default=1.0) def get_field_value(self): return {"test_case": self.test_case, @@ -1142,7 +1142,7 @@ class StandardTestCase(TestCase): class StdioBasedTestCase(TestCase): expected_input = models.CharField(max_length=100, blank=True) expected_output = models.CharField(max_length=100) - weight = models.IntegerField(default=0.0) + weight = models.IntegerField(default=1.0) def get_field_value(self): return {"expected_output": self.expected_output, @@ -1170,4 +1170,4 @@ class McqTestCase(TestCase): class HookTestCase(TestCase): code = models.TextField() - weight = models.FloatField(default=0.0) + weight = models.FloatField(default=1.0) diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index e1b05fe..91c7de4 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -13,20 +13,20 @@ {% block onload %} onload='javascript:textareaformat();' {% endblock %} {% block content %} -<form action="" method="post" name=frm onSubmit="return autosubmit();" enctype="multipart/form-data"> +<form action="{{ URL_ROOT }}/exam/manage/addquestion/{{ question.id }}/" method="post" name=frm onSubmit="return autosubmit();" enctype="multipart/form-data"> {% csrf_token %} <center><table class="table"> - <tr><td>Summary: <td>{{ form.summary }}{{ form.summary.errors }} - <tr><td> Language: <td> {{form.language}}{{form.language.errors}} - <tr><td> Type: <td> {{ form.type }}{{form.type.errors}} - <tr><td>Points:<td><button class="btn-mini" type="button" onClick="increase(frm);">+</button>{{form.points }}<button class="btn-mini" type="button" onClick="decrease(frm);">-</button>{{ form.points.errors }} + <tr><td>Summary: <td>{{ qform.summary }}{{ qform.summary.errors }} + <tr><td> Language: <td> {{qform.language}}{{qform.language.errors}} + <tr><td> Type: <td> {{ qform.type }}{{qform.type.errors}} + <tr><td>Points:<td><button class="btn-mini" type="button" onClick="increase(frm);">+</button>{{qform.points }}<button class="btn-mini" type="button" onClick="decrease(frm);">-</button>{{ qform.points.errors }} <tr><td><strong>Rendered: </strong><td><p id='my'></p> - <tr><td>Description: <td>{{ form.description}} {{form.description.errors}} - <tr><td>Tags: <td>{{ form.tags }} - <tr><td>Snippet: <td>{{ form.snippet }} - <tr><td>Partial Grading: <td>{{ form.partial_grading }} - <tr><td> Test Case Type: <td> {{ form.test_case_type }}{{ form.test_case_type.errors }} - <tr><td> File: <td> {{ upload_form.file_field }}{{ upload_form.file_field.errors }} + <tr><td>Description: <td>{{ qform.description}} {{qform.description.errors}} + <tr><td>Tags: <td>{{ qform.tags }} + <tr><td>Snippet: <td>{{ qform.snippet }} + <tr><td>Partial Grading: <td>{{ qform.partial_grading }} + <tr><td> Test Case Type: <td> {{ qform.test_case_type }}{{ qform.test_case_type.errors }} + <tr><td> File: <td> {{ fileform.file_field }}{{ fileform.file_field.errors }} {% if uploaded_files %}<br><b>Uploaded files:</b><br>Check on delete to delete files, extract to extract files and hide to hide files from student(if required)<br> {% for file in uploaded_files %} @@ -36,22 +36,55 @@ <input type="checkbox" name="hide" value="{{file.id}}" >{% if file.hide %} show{% else %} hide{% endif %}</input><br> <a href="{{file.file.url}}">{{ file.file.name }}</a> - <br> {% endfor %}{% endif %} + </table></center> <div class="form-group"> - {{ test_case_formset.management_form }} + {{ standardformset.management_form }} + + {% for stdform in standardformset %} + <div class="link-formset well"> + {{ stdform.as_p }} + </div> + {% endfor %} - {% for form in test_case_formset %} - <div class="link-formset"> - {{ form }} + </div> + <br \> + <div class="form-group"> + {{ stdioformset.management_form }} + {% for ioform in stdioformset %} + <div class="link-formset well"> + {{ ioform.as_p }} </div> {% endfor %} </div> + <div class="form-group"> + {{ mcqformset.management_form }} - </table></center> + {% for mcqform in mcqformset %} + <div class="link-formset well"> + {{ mcqform.as_p }} + </div> + {% endfor %} + </div> + <div class="form-group"> + {{ hookformset.management_form }} + + {% for hookform in hookformset %} + <div class="link-formset well"> + {{ hookform.as_p }} + </div> + {% endfor %} + </div> + <p><label for="case_type">Add Test Case:</label> <select id="case_type" name="case_type" onchange="frm.submit()"> + <option value="" selected="selected">---------</option> + <option value="standardtestcase">Standard </option> + <option value="stdiobasedtestcase">StdIO </option> + <option value="mcqtestcase">MCQ/MCC </option> + <option value="hooktestcase">Hook </option> + </select></p> <center> - <button class="btn" type="submit" name="save_question">Save & Add Testcase</button> + <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/");'>Back to Questions</button> <button class="btn" type="submit" name="delete_files">Delete Selected Files</button> </center> diff --git a/yaksh/templates/yaksh/new_question.html b/yaksh/templates/yaksh/new_question.html deleted file mode 100644 index 9e6f239..0000000 --- a/yaksh/templates/yaksh/new_question.html +++ /dev/null @@ -1,86 +0,0 @@ -{% extends "manage.html" %} - -{% block pagetitle %} Add Question {% endblock pagetitle %} - -{% block css %} -<link rel="stylesheet" media="all" type="text/css" href="{{ URL_ROOT }}/static/yaksh/css/autotaggit.css" /> -{% endblock %} - -{% block script %} -<script src="{{ URL_ROOT }}/static/yaksh/js/add_question.js"></script> -{% endblock %} - -{% block onload %} onload='javascript:textareaformat();' {% endblock %} - -{% block content %} -<form action="{{ URL_ROOT }}/exam/manage/newquestion/{{ question.id }}/" method="post" name="frm" onSubmit="return autosubmit();" enctype="multipart/form-data"> - {% csrf_token %} - {{ qform.as_p}} - {% if uploaded_files %}<br><b>Uploaded files:</b><br>Check on delete to delete files, - extract to extract files and hide to hide files from student(if required)<br> - {% for file in uploaded_files %} - <input type="checkbox" name="clear" value="{{file.id}}"> delete</input> - <input type="checkbox" name="extract" value="{{file.id}}" >{% if file.extract %} dont extract{% else %} - extract{% endif %}</input> - <input type="checkbox" name="hide" value="{{file.id}}" >{% if file.hide %} show{% else %} - hide{% endif %}</input><br> - <a href="{{file.file.url}}">{{ file.file.name }}</a> - <br> - {% endfor %}{% endif %} - <div class="form-group"> - {{ standardformset.management_form }} - - {% for stdform in standardformset %} - <div class="link-formset"> - {{ stdform }} - </div> - {% endfor %} - - </div> - <br \> - <div class="form-group"> - {{ stdioformset.management_form }} - {% for ioform in stdioformset %} - <div class="link-formset"> - {{ ioform }} - </div> - {% endfor %} - - </div> - <br \> - <div class="form-group"> - {{ mcqformset.management_form }} - - {% for mcqform in mcqformset %} - <div class="link-formset"> - {{ mcqform }} - </div> - {% endfor %} - <br \> - </div> - <div class="form-group"> - {{ hookformset.management_form }} - - {% for hookform in hookformset %} - <div class="link-formset"> - {{ hookform }} - </div> - {% endfor %} - <br \> - </div> - <p><label for="case_type">Add Test Case:</label> <select id="case_type" name="case_type" onchange="frm.submit()"> - <option value="" selected="selected">---------</option> - <option value="standardtestcase">Standard </option> - <option value="stdiobasedtestcase">StdIO </option> - <option value="mcqtestcase">MCQ/MCC </option> - <option value="hooktestcase">Hook </option> - </select></p> - - </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/");'>Back to Questions</button> - <button class="btn" type="submit" name="delete_files">Delete Selected Files</button> - </center> -</form> -{% endblock %} diff --git a/yaksh/urls.py b/yaksh/urls.py index 6fc83e4..036c6a3 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -52,9 +52,7 @@ urlpatterns += [ url(r'^view_answerpaper/(?P<questionpaper_id>\d+)/$', views.view_answerpaper, name='view_answerpaper'), url(r'^manage/$', views.prof_manage, name='manage'), url(r'^manage/addquestion/$', views.add_question), - url(r'^manage/addquestion/(?P<question_id>\d+)/$', views.edit_question), - url(r'^manage/newquestion/$', views.new_question), - url(r'^manage/newquestion/(?P<question_id>\d+)/$', views.new_question), + url(r'^manage/addquestion/(?P<question_id>\d+)/$', views.add_question), url(r'^manage/addquiz/(?P<course_id>\d+)/$', views.add_quiz, name='add_quiz'), url(r'^manage/addquiz/(?P<course_id>\d+)/(?P<quiz_id>\d+)/$', views.add_quiz, name='edit_quiz'), url(r'^manage/gradeuser/$', views.grade_user), diff --git a/yaksh/views.py b/yaksh/views.py index afe3813..5adddb8 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -130,7 +130,7 @@ def results_user(request): @login_required -def new_question(request, question_id=None): +def add_question(request, question_id=None): user = request.user ci = RequestContext(request) test_case_type = None @@ -141,24 +141,49 @@ def new_question(request, question_id=None): else: question = Question.objects.get(id=question_id) + if request.method == "POST" and 'delete_files' in request.POST: + remove_files_id = request.POST.getlist('clear') + if remove_files_id: + files = FileUpload.objects.filter(id__in=remove_files_id) + for file in files: + file.remove() + if request.method == 'POST': qform = QuestionForm(request.POST, instance=question) fileform = FileForm(request.POST, request.FILES) + files = request.FILES.getlist('file_field') + extract_files_id = request.POST.getlist('extract') + hide_files_id = request.POST.getlist('hide') + if files: + for file in files: + FileUpload.objects.get_or_create(question=question, file=file) + if extract_files_id: + files = FileUpload.objects.filter(id__in=extract_files_id) + for file in files: + file.set_extract_status() + if hide_files_id: + files = FileUpload.objects.filter(id__in=hide_files_id) + for file in files: + file.toggle_hide_status() if qform.is_valid(): question = qform.save(commit=False) question.user = user question.save() files = request.FILES.getlist('file_field') - if files: - for file in files: - FileUpload.objects.get_or_create(question=question, file=file) - StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=0, fields='__all__') - standardformset = StandardFormSet(request.POST, request.FILES, instance=question) - StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, extra=0, fields='__all__') - stdioformset = StdioFormSet(request.POST, request.FILES, instance=question) - McqFormSet = inlineformset_factory(Question, McqTestCase, extra=0, fields='__all__') + uploaded_files = FileUpload.objects.filter(question_id=question.id) + StandardFormSet = inlineformset_factory(Question, StandardTestCase, + extra=0, fields='__all__') + standardformset = StandardFormSet(request.POST, request.FILES, + instance=question) + StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, + extra=0, fields='__all__') + stdioformset = StdioFormSet(request.POST, request.FILES, + instance=question) + McqFormSet = inlineformset_factory(Question, McqTestCase, extra=0, + fields='__all__') mcqformset = McqFormSet(request.POST, request.FILES, instance=question) - HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, + fields='__all__') hookformset = HookFormSet(request.POST, request.FILES, instance=question) if standardformset.is_valid(): standardformset.save() @@ -170,144 +195,50 @@ def new_question(request, question_id=None): hookformset.save() test_case_type = request.POST.get('case_type', None) else: - context = {'qform': qform, 'fileform': fileform, 'question': question, 'mcqformset': mcqformset, 'stdioformset': stdioformset, - 'standardformset': standardformset, 'hookformset': hookformset} - return my_render_to_response("yaksh/new_question.html", context, context_instance=ci) + context = {'qform': qform, 'fileform': fileform, 'question': question, + 'mcqformset': mcqformset, 'stdioformset': stdioformset, + 'standardformset': standardformset, 'hookformset': hookformset, + 'uploaded_files': uploaded_files} + return my_render_to_response("yaksh/add_question.html", context, + context_instance=ci) qform = QuestionForm(instance=question) fileform = FileForm() - StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=0, fields='__all__') + uploaded_files = FileUpload.objects.filter(question_id=question.id) + StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=0, + fields='__all__') standardformset = StandardFormSet(instance=question) - StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, extra=0, fields='__all__') + StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, extra=0, + fields='__all__') stdioformset = StdioFormSet(instance=question) - McqFormSet = inlineformset_factory(Question, McqTestCase, extra=0, fields='__all__') + McqFormSet = inlineformset_factory(Question, McqTestCase, extra=0, + fields='__all__') mcqformset = McqFormSet(instance=question) - HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, + fields='__all__') hookformset = HookFormSet(instance=question) if test_case_type == 'standardtestcase': - StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=1, fields='__all__') + StandardFormSet = inlineformset_factory(Question, StandardTestCase, + extra=1, fields='__all__') standardformset = StandardFormSet(instance=question) elif test_case_type == 'stdiobasedtestcase': - StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, extra=1, fields='__all__') + StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, + extra=1, fields='__all__') stdioformset = StdioFormSet(instance=question) elif test_case_type == 'mcqtestcase': - McqFormSet = inlineformset_factory(Question, McqTestCase, extra=1, fields='__all__') + McqFormSet = inlineformset_factory(Question, McqTestCase, extra=1, + fields='__all__') mcqformset = McqFormSet(instance=question) elif test_case_type == 'hooktestcase': - HookFormSet = inlineformset_factory(Question, HookTestCase, extra=1, fields='__all__') + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=1, + fields='__all__') hookformset = HookFormSet(instance=question) - context = {'qform': qform, 'fileform': fileform, 'question': question, 'mcqformset': mcqformset, 'stdioformset': stdioformset, - 'standardformset': standardformset, 'hookformset': hookformset} - return my_render_to_response("yaksh/new_question.html", context, context_instance=ci) - - -@login_required -def add_question(request): - """To add a new question in the database. - Create a new question and store it.""" - user = request.user - ci = RequestContext(request) - - if request.method == "POST" and 'save_question' in request.POST: - question_form = QuestionForm(request.POST) - form = FileForm(request.POST, request.FILES) - if question_form.is_valid(): - new_question = question_form.save(commit=False) - new_question.user = user - new_question.save() - files = request.FILES.getlist('file_field') - if files: - for file in files: - FileUpload.objects.get_or_create(question=new_question, file=file) - - return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id)) - else: - return my_render_to_response('yaksh/add_question.html', - {'form': question_form, - 'upload_form': form}, - context_instance=ci) - else: - question_form = QuestionForm() - form = FileForm() - return my_render_to_response('yaksh/add_question.html', - {'form': question_form, - 'upload_form': form}, - context_instance=ci) - -@login_required -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 question_id: - raise Http404('No Question Found') + context = {'qform': qform, 'fileform': fileform, 'question': question, + 'mcqformset': mcqformset, 'stdioformset': stdioformset, + 'standardformset': standardformset, 'hookformset': hookformset, + 'uploaded_files': uploaded_files} + return my_render_to_response("yaksh/add_question.html", context, context_instance=ci) - question_instance = Question.objects.get(id=question_id) - if request.method == "POST" and 'delete_files' in request.POST: - remove_files_id = request.POST.getlist('clear') - if remove_files_id: - files = FileUpload.objects.filter(id__in=remove_files_id) - for file in files: - file.remove() - if request.method == "POST" and 'save_question' in request.POST: - question_form = QuestionForm(request.POST, instance=question_instance) - form = FileForm(request.POST, request.FILES) - files = request.FILES.getlist('file_field') - extract_files_id = request.POST.getlist('extract') - hide_files_id = request.POST.getlist('hide') - if files: - for file in files: - FileUpload.objects.get_or_create(question=question_instance, file=file) - if extract_files_id: - files = FileUpload.objects.filter(id__in=extract_files_id) - for file in files: - file.set_extract_status() - if hide_files_id: - files = FileUpload.objects.filter(id__in=hide_files_id) - for file in files: - file.toggle_hide_status() - if question_form.is_valid(): - new_question = question_form.save(commit=False) - 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, extra=1) - 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/addquestion/{0}".format(new_question.id)) - else: - 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, extra=1) - test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance) - uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) - return my_render_to_response('yaksh/add_question.html', - {'form': question_form, - 'test_case_formset': test_case_formset, - 'question_id': question_id, - 'upload_form': form, - 'uploaded_files': uploaded_files}, - context_instance=ci) - else: - question_form = QuestionForm(instance=question_instance) - form = FileForm() - 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, extra=1) - test_case_formset = TestCaseInlineFormSet(instance=question_instance) - uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) - return my_render_to_response('yaksh/add_question.html', - {'form': question_form, - 'test_case_formset': test_case_formset, - 'question_id': question_id, - 'upload_form': form, - 'uploaded_files': uploaded_files}, - context_instance=ci) @login_required def add_quiz(request, course_id, quiz_id=None): |