From cb9d4c01d6287ab1288d0755d4f93d5320eb1d3a Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 7 Dec 2016 13:50:33 +0530 Subject: basic interface to add multiple testcases to a question --- yaksh/models.py | 15 ++++++++--- yaksh/urls.py | 2 ++ yaksh/views.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/yaksh/models.py b/yaksh/models.py index 2018198..7a64bba 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -53,6 +53,7 @@ test_case_types = ( ("standardtestcase", "Standard Testcase"), ("stdiobasedtestcase", "StdIO Based Testcase"), ("mcqtestcase", "MCQ Testcase"), + ("hooktestcase", "Hook Testcase"), ) attempts = [(i, i) for i in range(1, 6)] @@ -1122,9 +1123,10 @@ class AssignmentUpload(models.Model): ################################################################################ class TestCase(models.Model): question = models.ForeignKey(Question, blank=True, null = True) + type = models.CharField(max_length=24, choices=test_case_types, null=True) class StandardTestCase(TestCase): - test_case = models.TextField(blank=True) + test_case = models.CharField(blank=True, max_length=100) weight = models.FloatField(default=0.0) def get_field_value(self): @@ -1138,8 +1140,8 @@ class StandardTestCase(TestCase): class StdioBasedTestCase(TestCase): - expected_input = models.TextField(blank=True) - expected_output = models.TextField() + expected_input = models.CharField(max_length=100, blank=True) + expected_output = models.CharField(max_length=100) weight = models.IntegerField(default=0.0) def get_field_value(self): @@ -1154,7 +1156,7 @@ class StdioBasedTestCase(TestCase): class McqTestCase(TestCase): - options = models.TextField() + options = models.CharField(max_length=100) correct = models.BooleanField(default=False) def get_field_value(self): @@ -1164,3 +1166,8 @@ class McqTestCase(TestCase): return u'Question: {0} | Correct: {1}'.format(self.question, self.correct ) + + +class HookTestCase(TestCase): + code = models.TextField() + weight = models.FloatField(default=0.0) diff --git a/yaksh/urls.py b/yaksh/urls.py index bb8cf2e..6fc83e4 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -53,6 +53,8 @@ urlpatterns += [ url(r'^manage/$', views.prof_manage, name='manage'), url(r'^manage/addquestion/$', views.add_question), url(r'^manage/addquestion/(?P\d+)/$', views.edit_question), + url(r'^manage/newquestion/$', views.new_question), + url(r'^manage/newquestion/(?P\d+)/$', views.new_question), url(r'^manage/addquiz/(?P\d+)/$', views.add_quiz, name='add_quiz'), url(r'^manage/addquiz/(?P\d+)/(?P\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 b6cf578..8f8d00b 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,7 @@ import six # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ - has_profile + has_profile, StandardTestCase, McqTestCase, StdioBasedTestCase, HookTestCase from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ @@ -129,6 +129,85 @@ def results_user(request): return my_render_to_response("yaksh/results_user.html", context) +@login_required +def new_question(request, question_id=None): + user = request.user + ci = RequestContext(request) + + if question_id is None: + question = Question(user=user) + question.save() + else: + question = Question.objects.get(id=question_id) + + qform = QuestionForm(instance=question) + fileform = FileForm() + StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=0, fields='__all__') + standardformset = StandardFormSet(instance=question) + StdioFormSet = inlineformset_factory(Question, StdioBasedTestCase, extra=0, fields='__all__') + stdioformset = StdioFormSet(instance=question) + McqFormSet = inlineformset_factory(Question, McqTestCase, extra=0, fields='__all__') + mcqformset = McqFormSet(instance=question) + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') + hookformset = HookFormSet(instance=question) + + if request.method == 'POST': + if 'save_question' in request.POST: + qform = QuestionForm(request.POST, instance=question) + fileform = FileForm(request.POST, request.FILES) + 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__') + mcqformset = McqFormSet(request.POST, request.FILES, instance=question) + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') + hookformset = HookFormSet(request.POST, request.FILES, instance=question) + if standardformset.is_valid(): + standardformset.save() + if mcqformset.is_valid(): + mcqformset.save() + if stdioformset.is_valid(): + stdioformset.save() + if hookformset.is_valid(): + hookformset.save() + return my_redirect("/exam/manage/newquestion/{0}".format(question.id)) + 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) + else: + test_case_type = request.POST.get('case_type', None) + if test_case_type == 'standardtestcase': + 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 = StdioFormSet(instance=question) + elif test_case_type == 'mcqtestcase': + 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 = HookFormSet(instance=question) + pass + 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} + 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. @@ -147,6 +226,7 @@ def add_question(request): 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', -- cgit From 6f472a2237edfc64e2e112600219768c92367feb Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 7 Dec 2016 17:34:15 +0530 Subject: new question template --- yaksh/templates/yaksh/new_question.html | 86 +++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 yaksh/templates/yaksh/new_question.html diff --git a/yaksh/templates/yaksh/new_question.html b/yaksh/templates/yaksh/new_question.html new file mode 100644 index 0000000..9e6f239 --- /dev/null +++ b/yaksh/templates/yaksh/new_question.html @@ -0,0 +1,86 @@ +{% extends "manage.html" %} + +{% block pagetitle %} Add Question {% endblock pagetitle %} + +{% block css %} + +{% endblock %} + +{% block script %} + +{% endblock %} + +{% block onload %} onload='javascript:textareaformat();' {% endblock %} + +{% block content %} +
+ {% csrf_token %} + {{ qform.as_p}} + {% if uploaded_files %}
Uploaded files:
Check on delete to delete files, + extract to extract files and hide to hide files from student(if required)
+ {% for file in uploaded_files %} +  delete  + {% if file.extract %} dont extract{% else %} + extract{% endif %}   + {% if file.hide %} show{% else %} + hide{% endif %}
+ {{ file.file.name }} +
+ {% endfor %}{% endif %} +
+ {{ standardformset.management_form }} + + {% for stdform in standardformset %} + + {% endfor %} + +
+
+
+ {{ stdioformset.management_form }} + {% for ioform in stdioformset %} + + {% endfor %} + +
+
+
+ {{ mcqformset.management_form }} + + {% for mcqform in mcqformset %} + + {% endfor %} +
+
+
+ {{ hookformset.management_form }} + + {% for hookform in hookformset %} + + {% endfor %} +
+
+

+ + +
+ + + +
+
+{% endblock %} -- cgit From 50d76bbd9f349d1cec113c92b58a6f5ad750a841 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 8 Dec 2016 11:55:30 +0530 Subject: form rendering improved --- yaksh/views.py | 99 +++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/yaksh/views.py b/yaksh/views.py index 8f8d00b..afe3813 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -133,6 +133,7 @@ def results_user(request): def new_question(request, question_id=None): user = request.user ci = RequestContext(request) + test_case_type = None if question_id is None: question = Question(user=user) @@ -140,6 +141,39 @@ def new_question(request, question_id=None): else: question = Question.objects.get(id=question_id) + if request.method == 'POST': + qform = QuestionForm(request.POST, instance=question) + fileform = FileForm(request.POST, request.FILES) + 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__') + mcqformset = McqFormSet(request.POST, request.FILES, instance=question) + HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') + hookformset = HookFormSet(request.POST, request.FILES, instance=question) + if standardformset.is_valid(): + standardformset.save() + if mcqformset.is_valid(): + mcqformset.save() + if stdioformset.is_valid(): + stdioformset.save() + if hookformset.is_valid(): + 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) + qform = QuestionForm(instance=question) fileform = FileForm() StandardFormSet = inlineformset_factory(Question, StandardTestCase, extra=0, fields='__all__') @@ -150,59 +184,18 @@ def new_question(request, question_id=None): mcqformset = McqFormSet(instance=question) HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') hookformset = HookFormSet(instance=question) - - if request.method == 'POST': - if 'save_question' in request.POST: - qform = QuestionForm(request.POST, instance=question) - fileform = FileForm(request.POST, request.FILES) - 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__') - mcqformset = McqFormSet(request.POST, request.FILES, instance=question) - HookFormSet = inlineformset_factory(Question, HookTestCase, extra=0, fields='__all__') - hookformset = HookFormSet(request.POST, request.FILES, instance=question) - if standardformset.is_valid(): - standardformset.save() - if mcqformset.is_valid(): - mcqformset.save() - if stdioformset.is_valid(): - stdioformset.save() - if hookformset.is_valid(): - hookformset.save() - return my_redirect("/exam/manage/newquestion/{0}".format(question.id)) - 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) - else: - test_case_type = request.POST.get('case_type', None) - if test_case_type == 'standardtestcase': - 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 = StdioFormSet(instance=question) - elif test_case_type == 'mcqtestcase': - 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 = HookFormSet(instance=question) - pass - 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) - + if test_case_type == 'standardtestcase': + 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 = StdioFormSet(instance=question) + elif test_case_type == 'mcqtestcase': + 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 = 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) -- cgit From e436ec35a4086a16208e30e1384ca0ebc8082570 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 19 Dec 2016 06:35:00 +0530 Subject: change in add question interface --- yaksh/models.py | 8 +- yaksh/templates/yaksh/add_question.html | 69 ++++++++--- yaksh/templates/yaksh/new_question.html | 86 -------------- yaksh/urls.py | 4 +- yaksh/views.py | 195 +++++++++++--------------------- 5 files changed, 119 insertions(+), 243 deletions(-) delete mode 100644 yaksh/templates/yaksh/new_question.html 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 %} -
+ {% csrf_token %}
-
Summary: {{ form.summary }}{{ form.summary.errors }} -
Language: {{form.language}}{{form.language.errors}} -
Type: {{ form.type }}{{form.type.errors}} -
Points:{{form.points }}{{ form.points.errors }} +
Summary: {{ qform.summary }}{{ qform.summary.errors }} +
Language: {{qform.language}}{{qform.language.errors}} +
Type: {{ qform.type }}{{qform.type.errors}} +
Points:{{qform.points }}{{ qform.points.errors }}
Rendered:

-
Description: {{ form.description}} {{form.description.errors}} -
Tags: {{ form.tags }} -
Snippet: {{ form.snippet }} -
Partial Grading: {{ form.partial_grading }} -
Test Case Type: {{ form.test_case_type }}{{ form.test_case_type.errors }} -
File: {{ upload_form.file_field }}{{ upload_form.file_field.errors }} +
Description: {{ qform.description}} {{qform.description.errors}} +
Tags: {{ qform.tags }} +
Snippet: {{ qform.snippet }} +
Partial Grading: {{ qform.partial_grading }} +
Test Case Type: {{ qform.test_case_type }}{{ qform.test_case_type.errors }} +
File: {{ fileform.file_field }}{{ fileform.file_field.errors }} {% if uploaded_files %}
Uploaded files:
Check on delete to delete files, extract to extract files and hide to hide files from student(if required)
{% for file in uploaded_files %} @@ -36,22 +36,55 @@ {% if file.hide %} show{% else %} hide{% endif %}
{{ file.file.name }} -
{% endfor %}{% endif %} +
- {{ test_case_formset.management_form }} + {{ standardformset.management_form }} + + {% for stdform in standardformset %} + + {% endfor %} - {% for form in test_case_formset %} - +
+
+ {{ stdioformset.management_form }} + {% for ioform in stdioformset %} + {% endfor %}
+
+ {{ mcqformset.management_form }} - + {% for mcqform in mcqformset %} + + {% endfor %} +
+
+ {{ hookformset.management_form }} + + {% for hookform in hookformset %} + + {% endfor %} +
+

- +
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 %} - -{% endblock %} - -{% block script %} - -{% endblock %} - -{% block onload %} onload='javascript:textareaformat();' {% endblock %} - -{% block content %} - - {% csrf_token %} - {{ qform.as_p}} - {% if uploaded_files %}
Uploaded files:
Check on delete to delete files, - extract to extract files and hide to hide files from student(if required)
- {% for file in uploaded_files %} -  delete  - {% if file.extract %} dont extract{% else %} - extract{% endif %}   - {% if file.hide %} show{% else %} - hide{% endif %}
- {{ file.file.name }} -
- {% endfor %}{% endif %} -
- {{ standardformset.management_form }} - - {% for stdform in standardformset %} - - {% endfor %} - -
-
-
- {{ stdioformset.management_form }} - {% for ioform in stdioformset %} - - {% endfor %} - -
-
-
- {{ mcqformset.management_form }} - - {% for mcqform in mcqformset %} - - {% endfor %} -
-
-
- {{ hookformset.management_form }} - - {% for hookform in hookformset %} - - {% endfor %} -
-
-

- - -
- - - -
- -{% 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\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\d+)/$', views.edit_question), - url(r'^manage/newquestion/$', views.new_question), - url(r'^manage/newquestion/(?P\d+)/$', views.new_question), + url(r'^manage/addquestion/(?P\d+)/$', views.add_question), url(r'^manage/addquiz/(?P\d+)/$', views.add_quiz, name='add_quiz'), url(r'^manage/addquiz/(?P\d+)/(?P\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): -- cgit From 1662f80d18ea00ad96bab28762352d8f6068f732 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 19 Dec 2016 08:43:17 +0530 Subject: cleaned up the code and made testcase forms dynamic --- yaksh/templates/yaksh/add_question.html | 36 +++-------------- yaksh/views.py | 69 ++++++++------------------------- 2 files changed, 21 insertions(+), 84 deletions(-) diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index 91c7de4..57e5e78 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -38,44 +38,18 @@ {{ file.file.name }} {% endfor %}{% endif %} + {% for formset in formsets %}
- {{ standardformset.management_form }} + {{ formset.management_form }} - {% for stdform in standardformset %} + {% for form in formset %} {% endfor %}
-
-
- {{ stdioformset.management_form }} - {% for ioform in stdioformset %} - - {% endfor %} - -
-
- {{ mcqformset.management_form }} - - {% for mcqform in mcqformset %} - - {% endfor %} -
-
- {{ hookformset.management_form }} - - {% for hookform in hookformset %} - - {% endfor %} -
+ {% endfor %}