diff options
-rw-r--r-- | yaksh/models.py | 15 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | 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<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/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 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,\ @@ -130,6 +130,85 @@ def results_user(request): @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. Create a new question and store it.""" @@ -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', |