From 344b6441017f72225a0d89f41c6cde38f4bbe187 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 22 Feb 2017 14:58:12 +0530 Subject: Allow moderator to select questions in any order required --- yaksh/static/yaksh/js/question_paper_creation.js | 14 ++++++++++++++ yaksh/templates/yaksh/design_questionpaper.html | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/static/yaksh/js/question_paper_creation.js b/yaksh/static/yaksh/js/question_paper_creation.js index 898e491..430ec4b 100644 --- a/yaksh/static/yaksh/js/question_paper_creation.js +++ b/yaksh/static/yaksh/js/question_paper_creation.js @@ -46,4 +46,18 @@ $(document).ready(function(){ $("#random").click(); }); + var checked_vals = []; + $('input:checkbox[name="questions"]').click(function() { + if($(this).prop("checked") == true){ + checked_vals.push(parseInt($(this).val())); + } + else{ + checked_vals.pop(parseInt($(this).val())); + } + }); + $('#design_q').submit(function(eventObj) { + $(this).append(''); + return true; +}); + });//document diff --git a/yaksh/templates/yaksh/design_questionpaper.html b/yaksh/templates/yaksh/design_questionpaper.html index 4418c27..829e27f 100644 --- a/yaksh/templates/yaksh/design_questionpaper.html +++ b/yaksh/templates/yaksh/design_questionpaper.html @@ -23,7 +23,7 @@ select {% block content %} -
+ {% csrf_token %} @@ -95,8 +95,10 @@ select {% for question in fixed_questions %}
  • {% endfor %} -- cgit From c2ffb981b81410006a04b43997a2441a99dbff3d Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 22 Feb 2017 15:00:11 +0530 Subject: Remove manytomany relationship for fixed_questions in question paper --- yaksh/models.py | 58 ++++++++++++++++++++++++++++++++++++++++++--------------- yaksh/views.py | 23 ++++++++++++++--------- 2 files changed, 57 insertions(+), 24 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 398f508..f4a3a7a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -616,11 +616,11 @@ class Quiz(models.Model): ############################################################################### class QuestionPaperManager(models.Manager): - def _create_trial_from_questionpaper(self, original_quiz_id): + def _create_trial_from_questionpaper(self, original_quiz_id, fixed_que): """Creates a copy of the original questionpaper""" trial_questionpaper = self.get(quiz_id=original_quiz_id) - trial_questions = {"fixed_questions": trial_questionpaper - .fixed_questions.all(), + trial_questions = {"fixed_questions": fixed_que.get_fixed_questions( + trial_questionpaper), "random_questions": trial_questionpaper .random_questions.all() } @@ -635,7 +635,10 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.create(quiz=trial_quiz, total_marks=10, ) - trial_questionpaper.fixed_questions.add(*questions_list) + fixed_que = FixedQuestions() + for q_id in questions_list: + que = Question.objects.get(id=q_id) + fixed_que.add_fixed_questions(trial_questionpaper, que) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): @@ -643,11 +646,16 @@ class QuestionPaperManager(models.Manager): if self.filter(quiz=trial_quiz).exists(): trial_questionpaper = self.get(quiz=trial_quiz) else: + fixed_que = FixedQuestions() trial_questionpaper, trial_questions = \ - self._create_trial_from_questionpaper(original_quiz_id) + self._create_trial_from_questionpaper(original_quiz_id, + fixed_que + ) trial_questionpaper.quiz = trial_quiz - trial_questionpaper.fixed_questions\ - .add(*trial_questions["fixed_questions"]) + for question in trial_questions["fixed_questions"]: + fixed_que.add_fixed_questions(trial_questionpaper, + question + ) trial_questionpaper.random_questions\ .add(*trial_questions["random_questions"]) trial_questionpaper.save() @@ -661,9 +669,6 @@ class QuestionPaper(models.Model): # Question paper belongs to a particular quiz. quiz = models.ForeignKey(Quiz) - # Questions that will be mandatory in the quiz. - fixed_questions = models.ManyToManyField(Question) - # Questions that will be fetched randomly from the Question Set. random_questions = models.ManyToManyField("QuestionSet") @@ -678,7 +683,8 @@ class QuestionPaper(models.Model): def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 - questions = self.fixed_questions.all() + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question in questions: marks += question.points for question_set in self.random_questions.all(): @@ -687,7 +693,8 @@ class QuestionPaper(models.Model): def _get_questions_for_answerpaper(self): """ Returns fixed and random questions for the answer paper""" - questions = list(self.fixed_questions.filter(active=True)) + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question_set in self.random_questions.all(): questions += question_set.get_random_questions() return questions @@ -705,8 +712,10 @@ class QuestionPaper(models.Model): ans_paper.question_paper = self ans_paper.save() questions = self._get_questions_for_answerpaper() - ans_paper.questions.add(*questions) - ans_paper.questions_unanswered.add(*questions) + for question in questions: + ans_paper.questions.add(question) + for question in questions: + ans_paper.questions_unanswered.add(question) return ans_paper def _is_questionpaper_passed(self, user): @@ -748,13 +757,32 @@ class QuestionPaper(models.Model): summary="Yaksh Demo Question", user=user) # add fixed set of questions to the question paper + fixed_que = FixedQuestions() for question in questions: - question_paper.fixed_questions.add(question) + fixed_que.add_fixed_questions(questionpaper, question) def __str__(self): return "Question Paper for " + self.quiz.description +############################################################################### +class FixedQuestions(models.Model): + questionpaper = models.ForeignKey(QuestionPaper) + question = models.ForeignKey(Question) + + def get_fixed_questions(self, qp): + fixed_q = FixedQuestions.objects.filter(questionpaper=qp).order_by('pk') + questions = [que.question for que in fixed_q + if que.question.active == True] + return questions + + def add_fixed_questions(self, qp, que): + FixedQuestions.objects.create(questionpaper=qp, question=que) + + def remove_fixed_questions(self, que_ids): + FixedQuestions.objects.filter(question_id__in=que_ids).delete() + + ############################################################################### class QuestionSet(models.Model): """Question set contains a set of questions from which random questions diff --git a/yaksh/views.py b/yaksh/views.py index 63653e6..9b6fc4e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,8 @@ 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, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase + has_profile, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase,\ + FixedQuestions from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ @@ -265,7 +266,8 @@ def show_all_questionpapers(request, questionpaper_id=None): else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz - fixed_questions = qu_papers.fixed_questions.all() + fixed_ques = FixedQuestions() + fixed_questions = fixed_ques.get_fixed_questions(qu_papers) random_questions = qu_papers.random_questions.all() context = {'quiz': quiz, 'fixed_questions': fixed_questions, 'random_questions': random_questions} @@ -805,8 +807,9 @@ def _remove_already_present(questionpaper_id, questions): if questionpaper_id is None: return questions questionpaper = QuestionPaper.objects.get(pk=questionpaper_id) - questions = questions.exclude( - id__in=questionpaper.fixed_questions.values_list('id', flat=True)) + fixed_questions = FixedQuestions.objects.filter( + questionpaper=questionpaper).values_list('question_id', flat=True) + questions = questions.exclude(id__in=fixed_questions) for random_set in questionpaper.random_questions.all(): questions = questions.exclude( id__in=random_set.questions.values_list('id', flat=True)) @@ -824,6 +827,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): questions = None marks = None state = None + fixed_que = FixedQuestions() if questionpaper_id is None: question_paper = QuestionPaper.objects.get_or_create(quiz_id=quiz_id)[0] else: @@ -839,13 +843,14 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): state = request.POST.get('is_active', None) if 'add-fixed' in request.POST: - question_ids = request.POST.getlist('questions', None) - for question in Question.objects.filter(id__in=question_ids): - question_paper.fixed_questions.add(question) + question_ids = request.POST.get('checked_ques', None).split(',') + for q_id in question_ids: + que = Question.objects.get(id=q_id) + fixed_que.add_fixed_questions(question_paper, que) if 'remove-fixed' in request.POST: question_ids = request.POST.getlist('added-questions', None) - question_paper.fixed_questions.remove(*question_ids) + fixed_que.remove_fixed_questions(question_ids) if 'add-random' in request.POST: question_ids = request.POST.getlist('random_questions', None) @@ -872,7 +877,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): question_paper.update_total_marks() question_paper.save() random_sets = question_paper.random_questions.all() - fixed_questions = question_paper.fixed_questions.all() + fixed_questions = fixed_que.get_fixed_questions(question_paper) context = {'qpaper_form': qpaper_form, 'filter_form': filter_form, 'qpaper': question_paper, 'questions': questions, 'fixed_questions': fixed_questions, 'state': state, 'random_sets': random_sets} -- cgit From b8d6116cd59f68888aca8f2acceecc9b85ebf7de Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 22 Feb 2017 15:01:24 +0530 Subject: Change models and views test to get fixed questions --- yaksh/test_models.py | 28 ++++++++++++++++------------ yaksh/test_views.py | 10 +++++----- 2 files changed, 21 insertions(+), 17 deletions(-) (limited to 'yaksh') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index cd66aed..efd5fd3 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,7 +1,7 @@ import unittest from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, FileUpload, McqTestCase + StdIOBasedTestCase, FileUpload, McqTestCase, FixedQuestions import json from datetime import datetime, timedelta from django.utils import timezone @@ -325,8 +325,10 @@ class QuestionPaperTestCases(unittest.TestCase): ) # add fixed set of questions to the question paper - self.question_paper.fixed_questions.add(self.questions[3], - self.questions[5]) + fixed_ques = FixedQuestions() + fixed_ques.add_fixed_questions(self.question_paper, self.questions[3]) + fixed_ques.add_fixed_questions(self.question_paper, self.questions[5]) + # create two QuestionSet for random questions # QuestionSet 1 self.question_set_1 = QuestionSet.objects.create(marks=2, @@ -374,8 +376,9 @@ class QuestionPaperTestCases(unittest.TestCase): def test_questionpaper(self): """ Test question paper""" self.assertEqual(self.question_paper.quiz.description, 'demo quiz') - self.assertSequenceEqual(self.question_paper.fixed_questions.all(), - [self.questions[3], self.questions[5]]) + fixed_ques = FixedQuestions() + ques = fixed_ques.get_fixed_questions(self.question_paper) + self.assertSequenceEqual(ques, [self.questions[3], self.questions[5]]) self.assertTrue(self.question_paper.shuffle_questions) def test_update_total_marks(self): @@ -415,7 +418,8 @@ class QuestionPaperTestCases(unittest.TestCase): self.assertIsInstance(answerpaper, AnswerPaper) paper_questions = answerpaper.questions.all() self.assertEqual(len(paper_questions), 7) - fixed_questions = set(self.question_paper.fixed_questions.all()) + fixed_ques = FixedQuestions() + fixed_questions = set(fixed_ques.get_fixed_questions(self.question_paper)) self.assertTrue(fixed_questions.issubset(set(paper_questions))) answerpaper.passed = True answerpaper.save() @@ -429,8 +433,8 @@ class QuestionPaperTestCases(unittest.TestCase): self.question_paper.id ) self.assertEqual(trial_paper.quiz, trial_quiz) - self.assertEqual(trial_paper.fixed_questions.all(), - self.question_paper.fixed_questions.all() + self.assertEqual(fixed_ques.get_fixed_questions(trial_paper), + fixed_ques.get_fixed_questions(self.question_paper) ) self.assertEqual(trial_paper.random_questions.all(), self.question_paper.random_questions.all() @@ -442,10 +446,10 @@ class QuestionPaperTestCases(unittest.TestCase): trial_quiz, self.questions_list ) self.assertEqual(trial_paper.quiz, trial_quiz) - self.assertEqual(self.questions_list, - self.question_paper.fixed_questions - .values_list("id", flat=True) - ) + fixed_q = FixedQuestions.objects.filter( + questionpaper=self.question_paper).values_list( + 'question_id', flat=True) + self.assertEqual(self.questions_list, fixed_q) ############################################################################### diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 7757f70..9d74697 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -10,7 +10,7 @@ from django.utils import timezone from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, has_profile + StdIOBasedTestCase, has_profile, FixedQuestions class TestProfile(TestCase): @@ -1161,8 +1161,8 @@ class TestViewAnswerPaper(TestCase): self.question_paper = QuestionPaper.objects.create(quiz=self.quiz, total_marks=1.0) - - self.question_paper.fixed_questions.add(self.question) + fixed_ques = FixedQuestions() + fixed_ques.add_fixed_questions(self.question_paper, self.question) self.question_paper.save() AnswerPaper.objects.create(user_id=3, @@ -1446,8 +1446,8 @@ class TestGrader(TestCase): self.question_paper = QuestionPaper.objects.create(quiz=self.quiz, total_marks=1.0) - - self.question_paper.fixed_questions.add(self.question) + fixed_ques = FixedQuestions() + fixed_ques.add_fixed_questions(self.question_paper, self.question) self.question_paper.save() self.answerpaper = AnswerPaper.objects.create(user_id=3, -- cgit From ca3c488193a7321b0ed0986856c4e74feadc199c Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 8 Mar 2017 15:31:03 +0530 Subject: Change views and models - Remove Fixed Question Model - Create a fixed question order attribute in models --- yaksh/models.py | 60 +++++++++++++++++++++------------------------------- yaksh/test_models.py | 27 ++++++++++++----------- yaksh/test_views.py | 8 +++---- yaksh/views.py | 43 ++++++++++++++++++++++++------------- 4 files changed, 68 insertions(+), 70 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index f4a3a7a..068bd66 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -616,11 +616,11 @@ class Quiz(models.Model): ############################################################################### class QuestionPaperManager(models.Manager): - def _create_trial_from_questionpaper(self, original_quiz_id, fixed_que): + def _create_trial_from_questionpaper(self, original_quiz_id): """Creates a copy of the original questionpaper""" trial_questionpaper = self.get(quiz_id=original_quiz_id) - trial_questions = {"fixed_questions": fixed_que.get_fixed_questions( - trial_questionpaper), + fixed_ques = trial_questionpaper.get_ordered_questions() + trial_questions = {"fixed_questions": fixed_ques, "random_questions": trial_questionpaper .random_questions.all() } @@ -635,10 +635,10 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.create(quiz=trial_quiz, total_marks=10, ) - fixed_que = FixedQuestions() for q_id in questions_list: que = Question.objects.get(id=q_id) - fixed_que.add_fixed_questions(trial_questionpaper, que) + FixedQuestions.objects.add_fixed_questions( + trial_questionpaper, que) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): @@ -646,16 +646,12 @@ class QuestionPaperManager(models.Manager): if self.filter(quiz=trial_quiz).exists(): trial_questionpaper = self.get(quiz=trial_quiz) else: - fixed_que = FixedQuestions() trial_questionpaper, trial_questions = \ self._create_trial_from_questionpaper(original_quiz_id, - fixed_que ) trial_questionpaper.quiz = trial_quiz - for question in trial_questions["fixed_questions"]: - fixed_que.add_fixed_questions(trial_questionpaper, - question - ) + trial_questionpaper.fixed_questions\ + .add(*trial_questions["fixed_questions"]) trial_questionpaper.random_questions\ .add(*trial_questions["random_questions"]) trial_questionpaper.save() @@ -669,6 +665,9 @@ class QuestionPaper(models.Model): # Question paper belongs to a particular quiz. quiz = models.ForeignKey(Quiz) + # Questions that will be mandatory in the quiz. + fixed_questions = models.ManyToManyField(Question) + # Questions that will be fetched randomly from the Question Set. random_questions = models.ManyToManyField("QuestionSet") @@ -678,13 +677,15 @@ class QuestionPaper(models.Model): # Total marks for the question paper. total_marks = models.FloatField(default=0.0, blank=True) + # Sequence or Order of fixed questions + fixed_question_order = models.CharField(max_length=255, blank=True) + objects = QuestionPaperManager() def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 - fixed_ques = FixedQuestions() - questions = fixed_ques.get_fixed_questions(self) + questions = self.fixed_questions.all() for question in questions: marks += question.points for question_set in self.random_questions.all(): @@ -693,8 +694,7 @@ class QuestionPaper(models.Model): def _get_questions_for_answerpaper(self): """ Returns fixed and random questions for the answer paper""" - fixed_ques = FixedQuestions() - questions = fixed_ques.get_fixed_questions(self) + questions = self.get_ordered_questions() for question_set in self.random_questions.all(): questions += question_set.get_random_questions() return questions @@ -757,32 +757,20 @@ class QuestionPaper(models.Model): summary="Yaksh Demo Question", user=user) # add fixed set of questions to the question paper - fixed_que = FixedQuestions() - for question in questions: - fixed_que.add_fixed_questions(questionpaper, question) + question_paper.fixed_questions.add(*questions) + + def get_ordered_questions(self): + ques = [] + if self.fixed_question_order: + que_order = self.fixed_question_order.split(',') + for que_id in que_order: + ques.append(self.fixed_questions.get(id=que_id)) + return ques def __str__(self): return "Question Paper for " + self.quiz.description -############################################################################### -class FixedQuestions(models.Model): - questionpaper = models.ForeignKey(QuestionPaper) - question = models.ForeignKey(Question) - - def get_fixed_questions(self, qp): - fixed_q = FixedQuestions.objects.filter(questionpaper=qp).order_by('pk') - questions = [que.question for que in fixed_q - if que.question.active == True] - return questions - - def add_fixed_questions(self, qp, que): - FixedQuestions.objects.create(questionpaper=qp, question=que) - - def remove_fixed_questions(self, que_ids): - FixedQuestions.objects.filter(question_id__in=que_ids).delete() - - ############################################################################### class QuestionSet(models.Model): """Question set contains a set of questions from which random questions diff --git a/yaksh/test_models.py b/yaksh/test_models.py index efd5fd3..80a385f 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,7 +1,7 @@ import unittest from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, FileUpload, McqTestCase, FixedQuestions + StdIOBasedTestCase, FileUpload, McqTestCases import json from datetime import datetime, timedelta from django.utils import timezone @@ -325,9 +325,9 @@ class QuestionPaperTestCases(unittest.TestCase): ) # add fixed set of questions to the question paper - fixed_ques = FixedQuestions() - fixed_ques.add_fixed_questions(self.question_paper, self.questions[3]) - fixed_ques.add_fixed_questions(self.question_paper, self.questions[5]) + self.question_paper.fixed_questions.add(self.questions[3], + self.questions[5] + ) # create two QuestionSet for random questions # QuestionSet 1 @@ -376,9 +376,9 @@ class QuestionPaperTestCases(unittest.TestCase): def test_questionpaper(self): """ Test question paper""" self.assertEqual(self.question_paper.quiz.description, 'demo quiz') - fixed_ques = FixedQuestions() - ques = fixed_ques.get_fixed_questions(self.question_paper) - self.assertSequenceEqual(ques, [self.questions[3], self.questions[5]]) + self.assertSequenceEqual(self.question_paper.fixed_questions.all(), + [self.questions[3], self.questions[5]] + ) self.assertTrue(self.question_paper.shuffle_questions) def test_update_total_marks(self): @@ -417,9 +417,9 @@ class QuestionPaperTestCases(unittest.TestCase): attempt_num) self.assertIsInstance(answerpaper, AnswerPaper) paper_questions = answerpaper.questions.all() + print (paper_questions) self.assertEqual(len(paper_questions), 7) - fixed_ques = FixedQuestions() - fixed_questions = set(fixed_ques.get_fixed_questions(self.question_paper)) + fixed_questions = set(self.question_paper.fixed_questions.all()) self.assertTrue(fixed_questions.issubset(set(paper_questions))) answerpaper.passed = True answerpaper.save() @@ -433,8 +433,8 @@ class QuestionPaperTestCases(unittest.TestCase): self.question_paper.id ) self.assertEqual(trial_paper.quiz, trial_quiz) - self.assertEqual(fixed_ques.get_fixed_questions(trial_paper), - fixed_ques.get_fixed_questions(self.question_paper) + self.assertEqual(trial_paper.fixed_questions.all(), + self.question_paper.fixed_questions.all() ) self.assertEqual(trial_paper.random_questions.all(), self.question_paper.random_questions.all() @@ -446,9 +446,8 @@ class QuestionPaperTestCases(unittest.TestCase): trial_quiz, self.questions_list ) self.assertEqual(trial_paper.quiz, trial_quiz) - fixed_q = FixedQuestions.objects.filter( - questionpaper=self.question_paper).values_list( - 'question_id', flat=True) + fixed_q = self.question_paper.fixed_questions.values_list( + 'id', flat=True) self.assertEqual(self.questions_list, fixed_q) diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 9d74697..aa6561a 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -10,7 +10,7 @@ from django.utils import timezone from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, has_profile, FixedQuestions + StdIOBasedTestCase, has_profile class TestProfile(TestCase): @@ -1161,8 +1161,7 @@ class TestViewAnswerPaper(TestCase): self.question_paper = QuestionPaper.objects.create(quiz=self.quiz, total_marks=1.0) - fixed_ques = FixedQuestions() - fixed_ques.add_fixed_questions(self.question_paper, self.question) + self.question_paper.fixed_questions.add(self.question) self.question_paper.save() AnswerPaper.objects.create(user_id=3, @@ -1446,8 +1445,7 @@ class TestGrader(TestCase): self.question_paper = QuestionPaper.objects.create(quiz=self.quiz, total_marks=1.0) - fixed_ques = FixedQuestions() - fixed_ques.add_fixed_questions(self.question_paper, self.question) + self.question_paper.fixed_questions.add(self.question) self.question_paper.save() self.answerpaper = AnswerPaper.objects.create(user_id=3, diff --git a/yaksh/views.py b/yaksh/views.py index 9b6fc4e..2a93aff 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,8 +25,8 @@ 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, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase,\ - FixedQuestions + has_profile, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase + from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ @@ -266,8 +266,7 @@ def show_all_questionpapers(request, questionpaper_id=None): else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz - fixed_ques = FixedQuestions() - fixed_questions = fixed_ques.get_fixed_questions(qu_papers) + fixed_questions = qu_papers.get_ordered_questions() random_questions = qu_papers.random_questions.all() context = {'quiz': quiz, 'fixed_questions': fixed_questions, 'random_questions': random_questions} @@ -359,7 +358,8 @@ def start(request, questionpaper_id=None, attempt_num=None): msg = 'Quiz not found, please contact your '\ 'instructor/administrator.' return complete(request, msg, attempt_num, questionpaper_id=None) - if not quest_paper.fixed_questions.all() and not quest_paper.random_questions.all(): + if not quest_paper.get_ordered_questions() and not \ + quest_paper.random_questions.all(): msg = 'Quiz does not have Questions, please contact your '\ 'instructor/administrator.' return complete(request, msg, attempt_num, questionpaper_id=None) @@ -807,9 +807,8 @@ def _remove_already_present(questionpaper_id, questions): if questionpaper_id is None: return questions questionpaper = QuestionPaper.objects.get(pk=questionpaper_id) - fixed_questions = FixedQuestions.objects.filter( - questionpaper=questionpaper).values_list('question_id', flat=True) - questions = questions.exclude(id__in=fixed_questions) + questions = questions.exclude( + id__in=questionpaper.fixed_questions.values_list('id', flat=True)) for random_set in questionpaper.random_questions.all(): questions = questions.exclude( id__in=random_set.questions.values_list('id', flat=True)) @@ -827,7 +826,6 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): questions = None marks = None state = None - fixed_que = FixedQuestions() if questionpaper_id is None: question_paper = QuestionPaper.objects.get_or_create(quiz_id=quiz_id)[0] else: @@ -843,14 +841,29 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): state = request.POST.get('is_active', None) if 'add-fixed' in request.POST: - question_ids = request.POST.get('checked_ques', None).split(',') - for q_id in question_ids: - que = Question.objects.get(id=q_id) - fixed_que.add_fixed_questions(question_paper, que) + question_ids = request.POST.get('checked_ques', None) + if question_paper.fixed_question_order: + ques_order = question_paper.fixed_question_order.split(",") +\ + question_ids.split(",") + questions_order = ",".join(ques_order) + else: + questions_order = question_ids + questions = Question.objects.filter(id__in=question_ids.split(',')) + question_paper.fixed_question_order = questions_order + question_paper.save() + question_paper.fixed_questions.add(*questions) if 'remove-fixed' in request.POST: question_ids = request.POST.getlist('added-questions', None) - fixed_que.remove_fixed_questions(question_ids) + que_order = question_paper.fixed_question_order.split(",") + for qid in question_ids: + que_order.remove(qid) + if que_order: + question_paper.fixed_question_order = ",".join(que_order) + else: + question_paper.fixed_question_order = "" + question_paper.save() + question_paper.fixed_questions.remove(*question_ids) if 'add-random' in request.POST: question_ids = request.POST.getlist('random_questions', None) @@ -877,7 +890,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): question_paper.update_total_marks() question_paper.save() random_sets = question_paper.random_questions.all() - fixed_questions = fixed_que.get_fixed_questions(question_paper) + fixed_questions = question_paper.get_ordered_questions() context = {'qpaper_form': qpaper_form, 'filter_form': filter_form, 'qpaper': question_paper, 'questions': questions, 'fixed_questions': fixed_questions, 'state': state, 'random_sets': random_sets} -- cgit From 97abdf46bad713e1ddab31175fbb7030e0257acb Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 9 Mar 2017 18:38:16 +0530 Subject: Add test for ordering of questions --- yaksh/test_models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'yaksh') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 80a385f..5e39ab9 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,7 +1,7 @@ import unittest from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, FileUpload, McqTestCases + StdIOBasedTestCase, FileUpload, McqTestCase import json from datetime import datetime, timedelta from django.utils import timezone @@ -324,6 +324,9 @@ class QuestionPaperTestCases(unittest.TestCase): shuffle_questions=True ) + self.question_paper.fixed_question_order = "{0}, {1}".format( + self.questions[3].id, self.questions[5].id + ) # add fixed set of questions to the question paper self.question_paper.fixed_questions.add(self.questions[3], self.questions[5] @@ -417,7 +420,6 @@ class QuestionPaperTestCases(unittest.TestCase): attempt_num) self.assertIsInstance(answerpaper, AnswerPaper) paper_questions = answerpaper.questions.all() - print (paper_questions) self.assertEqual(len(paper_questions), 7) fixed_questions = set(self.question_paper.fixed_questions.all()) self.assertTrue(fixed_questions.issubset(set(paper_questions))) @@ -450,6 +452,11 @@ class QuestionPaperTestCases(unittest.TestCase): 'id', flat=True) self.assertEqual(self.questions_list, fixed_q) + def test_fixed_order_questions(self): + fixed_ques = self.question_paper.get_ordered_questions() + actual_ques = [self.questions[3], self.questions[5]] + self.assertSequenceEqual(fixed_ques, actual_ques) + ############################################################################### class AnswerPaperTestCases(unittest.TestCase): -- cgit From ec507b9a977811cfb9909e50133120476eaa9c6c Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 10 Mar 2017 14:52:12 +0530 Subject: Fix demo course creation for selenium tests --- yaksh/models.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 068bd66..c5e73f6 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -756,6 +756,9 @@ class QuestionPaper(models.Model): questions = Question.objects.filter(active=True, summary="Yaksh Demo Question", user=user) + q_order = [str(que.id) for que in questions] + question_paper.fixed_questions_order = ",".join(q_order) + question_paper.save() # add fixed set of questions to the question paper question_paper.fixed_questions.add(*questions) -- cgit From f568762320caa8f57b83f34d02e3b3f5db3b1e45 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 10 Mar 2017 17:44:16 +0530 Subject: Fix test questions method in QuestionPaper model --- yaksh/models.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index c5e73f6..2569892 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -635,10 +635,8 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.create(quiz=trial_quiz, total_marks=10, ) - for q_id in questions_list: - que = Question.objects.get(id=q_id) - FixedQuestions.objects.add_fixed_questions( - trial_questionpaper, que) + trial_questionpaper.fixed_question_order = ",".join(questions_list) + trial_questionpaper.fixed_questions.add(*questions_list) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): @@ -647,8 +645,7 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.get(quiz=trial_quiz) else: trial_questionpaper, trial_questions = \ - self._create_trial_from_questionpaper(original_quiz_id, - ) + self._create_trial_from_questionpaper(original_quiz_id) trial_questionpaper.quiz = trial_quiz trial_questionpaper.fixed_questions\ .add(*trial_questions["fixed_questions"]) @@ -757,7 +754,7 @@ class QuestionPaper(models.Model): summary="Yaksh Demo Question", user=user) q_order = [str(que.id) for que in questions] - question_paper.fixed_questions_order = ",".join(q_order) + question_paper.fixed_question_order = ",".join(q_order) question_paper.save() # add fixed set of questions to the question paper question_paper.fixed_questions.add(*questions) -- cgit From 17c9dd17449adb19ce7734e3572ac0bc376ca5c5 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 10 Mar 2017 19:02:40 +0530 Subject: Fix test cases for trial quiz --- yaksh/test_models.py | 57 +++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'yaksh') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 5e39ab9..6812212 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -331,7 +331,6 @@ class QuestionPaperTestCases(unittest.TestCase): self.question_paper.fixed_questions.add(self.questions[3], self.questions[5] ) - # create two QuestionSet for random questions # QuestionSet 1 self.question_set_1 = QuestionSet.objects.create(marks=2, @@ -373,8 +372,8 @@ class QuestionPaperTestCases(unittest.TestCase): # For Trial case self.questions_list = [self.questions[3].id, self.questions[5].id] - trial_course = Course.objects.create_trial_course(self.user) - trial_quiz = Quiz.objects.create_trial_quiz(trial_course, self.user) + self.trial_course = Course.objects.create_trial_course(self.user) + self.trial_quiz = Quiz.objects.create_trial_quiz(self.trial_course, self.user) def test_questionpaper(self): """ Test question paper""" @@ -429,28 +428,36 @@ class QuestionPaperTestCases(unittest.TestCase): # test can_attempt_now(self): self.assertFalse(self.question_paper.can_attempt_now(self.user)) - def test_create_trial_paper_to_test_quiz(self): - trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\ - (trial_quiz, - self.question_paper.id - ) - self.assertEqual(trial_paper.quiz, trial_quiz) - self.assertEqual(trial_paper.fixed_questions.all(), - self.question_paper.fixed_questions.all() - ) - self.assertEqual(trial_paper.random_questions.all(), - self.question_paper.random_questions.all() - ) - - def test_create_trial_paper_to_test_questions(self): - trial_paper = QuestionPaper.objects.\ - create_trial_paper_to_test_questions( - trial_quiz, self.questions_list - ) - self.assertEqual(trial_paper.quiz, trial_quiz) - fixed_q = self.question_paper.fixed_questions.values_list( - 'id', flat=True) - self.assertEqual(self.questions_list, fixed_q) + def test_create_trial_paper_to_test_quiz(self): + qu_list = [str(self.questions_list[0]), str(self.questions_list[1])] + trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\ + (self.trial_quiz, + self.quiz.id + ) + trial_paper.random_questions.add(self.question_set_1) + trial_paper.random_questions.add(self.question_set_2) + trial_paper.fixed_question_order = ",".join(qu_list) + self.assertEqual(trial_paper.quiz, self.trial_quiz) + self.assertSequenceEqual(trial_paper.get_ordered_questions(), + self.question_paper.get_ordered_questions() + ) + trial_paper_ran = [q_set.id for q_set in + trial_paper.random_questions.all()] + qp_ran = [q_set.id for q_set in + self.question_paper.random_questions.all()] + + self.assertSequenceEqual(trial_paper_ran, qp_ran) + + def test_create_trial_paper_to_test_questions(self): + qu_list = [str(self.questions_list[0]), str(self.questions_list[1])] + trial_paper = QuestionPaper.objects.\ + create_trial_paper_to_test_questions( + self.trial_quiz, qu_list + ) + self.assertEqual(trial_paper.quiz, self.trial_quiz) + fixed_q = self.question_paper.fixed_questions.values_list( + 'id', flat=True) + self.assertSequenceEqual(self.questions_list, fixed_q) def test_fixed_order_questions(self): fixed_ques = self.question_paper.get_ordered_questions() -- cgit From f1eb06d3740eb21558576e5f5489972e45cab038 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 15 Mar 2017 15:38:01 +0530 Subject: Changes in Views and Models - Add new boolean field in Question model whether to check assignment upload or not - In views before uploading a assignment file, check if it already exists and delete previous file - Grade assignment file with hook code --- yaksh/models.py | 11 +++++++++-- yaksh/views.py | 32 ++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 12 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 398f508..9134663 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -66,7 +66,7 @@ test_status = ( def get_assignment_dir(instance, filename): return os.sep.join(( - instance.user.user, instance.assignmentQuestion.id, filename + str(instance.user.user), str(instance.assignmentQuestion.id), filename )) @@ -264,6 +264,9 @@ class Question(models.Model): # Does this question allow partial grading partial_grading = models.BooleanField(default=False) + # Check assignment upload based question + grade_assignment_upload = models.BooleanField(default=False) + def consolidate_answer_data(self, user_answer): question_data = {} metadata = {} @@ -280,9 +283,13 @@ class Question(models.Model): metadata['language'] = self.language metadata['partial_grading'] = self.partial_grading files = FileUpload.objects.filter(question=self) + assignment_files = AssignmentUpload.objects.filter() if files: metadata['file_paths'] = [(file.file.path, file.extract) for file in files] + if assignment_files: + metadata['assign_files'] = [(file.assignmentFile.path, False) + for file in assignment_files] question_data['metadata'] = metadata return json.dumps(question_data) @@ -1201,7 +1208,7 @@ class AnswerPaper(models.Model): ############################################################################### class AssignmentUpload(models.Model): - user = models.ForeignKey(Profile) + user = models.ForeignKey(User) assignmentQuestion = models.ForeignKey(Question) assignmentFile = models.FileField(upload_to=get_assignment_dir) diff --git a/yaksh/views.py b/yaksh/views.py index 63653e6..4cdba5c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -467,17 +467,24 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): elif current_question.type == 'mcc': user_answer = request.POST.getlist('answer') elif current_question.type == 'upload': - assign = AssignmentUpload() - assign.user = user.profile - assign.assignmentQuestion = current_question # if time-up at upload question then the form is submitted without # validation if 'assignment' in request.FILES: - assign.assignmentFile = request.FILES['assignment'] - assign.save() + assignment_filename = request.FILES.getlist('assignment') + for fname in assignment_filename: + if AssignmentUpload.objects.filter( + assignmentFile__icontains=fname, user=user).exists(): + assign_file = AssignmentUpload.objects.get( + assignmentFile__icontains=fname, user=user) + os.remove(assign_file.assignmentFile.path) + assign_file.delete() + AssignmentUpload.objects.create(user=user, + assignmentQuestion=current_question, assignmentFile=fname + ) user_answer = 'ASSIGNMENT UPLOADED' - next_q = paper.add_completed_question(current_question.id) - return show_question(request, next_q, paper) + if not current_question.grade_assignment_upload: + next_q = paper.add_completed_question(current_question.id) + return show_question(request, next_q, paper) else: user_code = request.POST.get('answer') user_answer = snippet_code + "\n" + user_code if snippet_code else user_code @@ -497,7 +504,9 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): if result.get('success'): new_answer.marks = (current_question.points * result['weight'] / current_question.get_maximum_test_case_weight()) \ - if current_question.partial_grading and current_question.type == 'code' else current_question.points + if current_question.partial_grading and \ + current_question.type == 'code' or current_question.type == 'upload' \ + else current_question.points new_answer.correct = result.get('success') error_message = None new_answer.error = json.dumps(result.get('error')) @@ -505,11 +514,14 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): else: new_answer.marks = (current_question.points * result['weight'] / current_question.get_maximum_test_case_weight()) \ - if current_question.partial_grading and current_question.type == 'code' else 0 + if current_question.partial_grading and \ + current_question.type == 'code' or current_question.type == 'upload' \ + else 0 error_message = result.get('error') if current_question.type == 'code' \ - else None + or current_question.type == 'upload' else None new_answer.error = json.dumps(result.get('error')) next_question = current_question if current_question.type == 'code' \ + or current_question.type == 'upload' \ else paper.add_completed_question(current_question.id) new_answer.save() paper.update_marks('inprogress') -- cgit From 5e500f3344a13d375d018312936280d88d47c93c Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 15 Mar 2017 17:55:34 +0530 Subject: Change templates and js - Support multiple files uploading in assignment - Create new check field to grade assignement based question - Add js changes --- yaksh/hook_evaluator.py | 7 +++++++ yaksh/static/yaksh/js/add_question.js | 36 ++++++++++++++++++++++++--------- yaksh/templates/exam.html | 2 +- yaksh/templates/yaksh/add_question.html | 1 + yaksh/templates/yaksh/question.html | 2 +- 5 files changed, 37 insertions(+), 11 deletions(-) (limited to 'yaksh') diff --git a/yaksh/hook_evaluator.py b/yaksh/hook_evaluator.py index 2cc4578..052d220 100644 --- a/yaksh/hook_evaluator.py +++ b/yaksh/hook_evaluator.py @@ -17,6 +17,7 @@ class HookEvaluator(BaseEvaluator): self.user_answer = metadata.get('user_answer') self.file_paths = metadata.get('file_paths') self.partial_grading = metadata.get('partial_grading') + self.assignment_files = metadata.get('assign_files') # Set test case data values self.hook_code = test_case_data.get('hook_code') @@ -26,6 +27,8 @@ class HookEvaluator(BaseEvaluator): # Delete the created file. if self.files: delete_files(self.files) + if self.assign_files: + delete_files(self.assign_files) def check_code(self): """ Function evaluates user answer by running a python based hook code @@ -47,6 +50,10 @@ class HookEvaluator(BaseEvaluator): Returns (False, error_msg, 0.0): If mandatory arguments are not files or if the required permissions are not given to the file(s). """ + if self.file_paths: + self.files = copy_files(self.file_paths) + if self.assignment_files: + self.assign_files = copy_files(self.assignment_files) success = False mark_fraction = 0.0 try: diff --git a/yaksh/static/yaksh/js/add_question.js b/yaksh/static/yaksh/js/add_question.js index 8ca22eb..05752b4 100644 --- a/yaksh/static/yaksh/js/add_question.js +++ b/yaksh/static/yaksh/js/add_question.js @@ -111,16 +111,34 @@ function textareaformat() }); - $('#id_type').bind('focus', function(event){ - var type = document.getElementById('id_type'); - type.style.border = '1px solid #ccc'; - }); + $('#id_type').bind('focus', function(event){ + var type = document.getElementById('id_type'); + type.style.border = '1px solid #ccc'; + }); + + $('#id_language').bind('focus', function(event){ + var language = document.getElementById('id_language'); + language.style.border = '1px solid #ccc'; + }); + document.getElementById('my').innerHTML = document.getElementById('id_description').value ; - $('#id_language').bind('focus', function(event){ - var language = document.getElementById('id_language'); - language.style.border = '1px solid #ccc'; - }); - document.getElementById('my').innerHTML = document.getElementById('id_description').value ; + + if (document.getElementById('id_grade_assignment_upload').checked || + document.getElementById('id_type').val() == 'upload'){ + $("#id_grade_assignment_upload").prop("disabled", false); + } + else{ + $("#id_grade_assignment_upload").prop("disabled", true); + } + + $('#id_type').change(function() { + if ($(this).val() == "upload"){ + $("#id_grade_assignment_upload").prop("disabled", false); + } + else{ + $("#id_grade_assignment_upload").prop("disabled", true); + } + }); } function autosubmit() diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index 02ff70a..a18a962 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -73,7 +73,7 @@ {% block main %} {% endblock %} - {% if question.type == 'code' %} + {% if question.type == 'code' or question.type == 'upload' %} {% if error_message %}
    {% for error in error_message %} diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index 75802b4..ae70774 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -25,6 +25,7 @@ Tags: {{ qform.tags }} Snippet: {{ qform.snippet }} Partial Grading: {{ qform.partial_grading }} + Grade Assignment Upload: {{ qform.grade_assignment_upload }} 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)
    diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index 9dd0de5..2a93cfb 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -188,7 +188,7 @@ function call_skip(url) {% endif %} {% if question.type == "upload" %}

    Upload assignment file for the said question

    - +


    {% endif %} {% if question.type == "code" %} -- cgit From e937c2a3a17bfc127af89a86e213e434350cd360 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 15 Mar 2017 18:12:31 +0530 Subject: Change assignment dir path and add condition for json data --- yaksh/models.py | 4 ++-- yaksh/views.py | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 9134663..4dcd2ec 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -66,7 +66,7 @@ test_status = ( def get_assignment_dir(instance, filename): return os.sep.join(( - str(instance.user.user), str(instance.assignmentQuestion.id), filename + instance.user.username, str(instance.assignmentQuestion.id), filename )) @@ -1145,7 +1145,7 @@ class AnswerPaper(models.Model): if set(user_answer) == set(expected_answers): result['success'] = True result['error'] = ['Correct answer'] - elif question.type == 'code': + elif question.type == 'code' or question.type == "upload": user_dir = self.user.profile.get_user_dir() json_result = code_server.run_code( question.language, json_data, user_dir diff --git a/yaksh/views.py b/yaksh/views.py index 4cdba5c..d4d11f5 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -473,8 +473,10 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): assignment_filename = request.FILES.getlist('assignment') for fname in assignment_filename: if AssignmentUpload.objects.filter( + assignmentQuestion=current_question, assignmentFile__icontains=fname, user=user).exists(): assign_file = AssignmentUpload.objects.get( + assignmentQuestion=current_question, assignmentFile__icontains=fname, user=user) os.remove(assign_file.assignmentFile.path) assign_file.delete() @@ -499,7 +501,8 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. json_data = current_question.consolidate_answer_data(user_answer) \ - if current_question.type == 'code' else None + if current_question.type == 'code' or \ + current_question.type == 'upload' else None result = paper.validate_answer(user_answer, current_question, json_data) if result.get('success'): new_answer.marks = (current_question.points * result['weight'] / -- cgit From c6b9b51a5c4b1c676c9784432435ccffb15335b6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 16 Mar 2017 10:47:21 +0530 Subject: Add test for assignment evaluation and Change test_models --- yaksh/evaluator_tests/test_python_evaluation.py | 56 +++++++++++++++++++++---- yaksh/test_models.py | 34 +++++++++------ 2 files changed, 70 insertions(+), 20 deletions(-) (limited to 'yaksh') diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 51f9bea..a751c40 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -19,7 +19,8 @@ class EvaluatorBaseTest(unittest.TestCase): class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): def setUp(self): - with open('/tmp/test.txt', 'wb') as f: + self.tmp_file = os.path.join(tempfile.gettempdir(), "test.txt") + with open(self.tmp_file, 'wb') as f: f.write('2'.encode('ascii')) tmp_in_dir_path = tempfile.mkdtemp() self.in_dir = tmp_in_dir_path @@ -33,7 +34,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): self.file_paths = None def tearDown(self): - os.remove('/tmp/test.txt') + os.remove(self.tmp_file) shutil.rmtree(self.in_dir) def test_correct_answer(self): @@ -343,7 +344,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): def test_file_based_assert(self): # Given self.test_case_data = [{"test_case_type": "standardtestcase", "test_case": "assert(ans()=='2')", "weight": 0.0}] - self.file_paths = [('/tmp/test.txt', False)] + self.file_paths = [(self.tmp_file, False)] user_answer = dedent(""" def ans(): with open("test.txt") as f: @@ -479,12 +480,17 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): class PythonStdIOEvaluationTestCases(EvaluatorBaseTest): def setUp(self): - with open('/tmp/test.txt', 'wb') as f: + self.tmp_file = os.path.join(tempfile.gettempdir(), "test.txt") + with open(self.tmp_file, 'wb') as f: f.write('2'.encode('ascii')) self.file_paths = None tmp_in_dir_path = tempfile.mkdtemp() self.in_dir = tmp_in_dir_path + def teardown(self): + os.remove(self.tmp_file) + shutil.rmtree(self.in_dir) + def test_correct_answer_integer(self): # Given self.test_case_data = [{"test_case_type": "stdiobasedtestcase", @@ -618,7 +624,7 @@ class PythonStdIOEvaluationTestCases(EvaluatorBaseTest): "expected_output": "2", "weight": 0.0 }] - self.file_paths = [('/tmp/test.txt', False)] + self.file_paths = [(self.tmp_file, False)] user_answer = dedent(""" with open("test.txt") as f: @@ -702,7 +708,8 @@ class PythonStdIOEvaluationTestCases(EvaluatorBaseTest): class PythonHookEvaluationTestCases(EvaluatorBaseTest): def setUp(self): - with open('/tmp/test.txt', 'wb') as f: + self.tmp_file = os.path.join(tempfile.gettempdir(), "test.txt") + with open(self.tmp_file, 'wb') as f: f.write('2'.encode('ascii')) tmp_in_dir_path = tempfile.mkdtemp() self.in_dir = tmp_in_dir_path @@ -712,7 +719,7 @@ class PythonHookEvaluationTestCases(EvaluatorBaseTest): self.file_paths = None def tearDown(self): - os.remove('/tmp/test.txt') + os.remove(self.tmp_file) shutil.rmtree(self.in_dir) def test_correct_answer(self): @@ -910,6 +917,41 @@ class PythonHookEvaluationTestCases(EvaluatorBaseTest): self.assertFalse(result.get('success')) self.assert_correct_output(self.timeout_msg, result.get('error')) + def test_assignment_upload(self): + # Given + user_answer = "Assignment Upload" + hook_code = dedent("""\ + def check_answer(user_answer): + success = False + err = "Incorrect Answer" + mark_fraction = 0.0 + with open("test.txt") as f: + data = f.read() + if data == '2': + success, err, mark_fraction = True, "", 1.0 + return success, err, mark_fraction + """ + ) + test_case_data = [{"test_case_type": "hooktestcase", + "hook_code": hook_code,"weight": 1.0 + }] + kwargs = { + 'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'assign_files': [(self.tmp_file, False)], + 'partial_grading': False, + 'language': 'python' + }, + 'test_case_data': test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + + # Then + self.assertTrue(result.get('success')) if __name__ == '__main__': unittest.main() diff --git a/yaksh/test_models.py b/yaksh/test_models.py index cd66aed..56097a7 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -57,20 +57,15 @@ def setUpModule(): description='demo quiz', pass_criteria=40, language='Python', prerequisite=quiz, course=course, instructions="Demo Instructions") - - with open('/tmp/test.txt', 'wb') as f: + tmp_file1 = os.path.join(tempfile.gettempdir(), "test.txt") + with open(tmp_file1, 'wb') as f: f.write('2'.encode('ascii')) + def tearDownModule(): User.objects.all().delete() Question.objects.all().delete() Quiz.objects.all().delete() - - que_id_list = ["25", "22", "24", "27"] - for que_id in que_id_list: - dir_path = os.path.join(os.getcwd(), "yaksh", "data","question_{0}".format(que_id)) - if os.path.exists(dir_path): - shutil.rmtree(dir_path) ############################################################################### class ProfileTestCases(unittest.TestCase): @@ -117,7 +112,7 @@ class QuestionTestCases(unittest.TestCase): self.question2.save() # create a temp directory and add files for loading questions test - file_path = "/tmp/test.txt" + file_path = os.path.join(tempfile.gettempdir(), "test.txt") self.load_tmp_path = tempfile.mkdtemp() shutil.copy(file_path, self.load_tmp_path) file1 = os.path.join(self.load_tmp_path, "test.txt") @@ -126,9 +121,11 @@ class QuestionTestCases(unittest.TestCase): self.dump_tmp_path = tempfile.mkdtemp() shutil.copy(file_path, self.dump_tmp_path) file2 = os.path.join(self.dump_tmp_path, "test.txt") - file = open(file2, "r") - django_file = File(file) - file = FileUpload.objects.create(file=django_file, question=self.question2) + upload_file = open(file2, "r") + django_file = File(upload_file) + file = FileUpload.objects.create(file=django_file, + question=self.question2 + ) self.question1.tags.add('python', 'function') self.assertion_testcase = StandardTestCase(question=self.question1, @@ -158,6 +155,15 @@ class QuestionTestCases(unittest.TestCase): def tearDown(self): shutil.rmtree(self.load_tmp_path) shutil.rmtree(self.dump_tmp_path) + uploaded_files = FileUpload.objects.all() + que_id_list = [file.question.id for file in uploaded_files] + for que_id in que_id_list: + dir_path = os.path.join(os.getcwd(), "yaksh", "data", + "question_{0}".format(que_id) + ) + if os.path.exists(dir_path): + shutil.rmtree(dir_path) + uploaded_files.delete() def test_question(self): """ Test question """ @@ -214,7 +220,9 @@ class QuestionTestCases(unittest.TestCase): self.assertTrue(question_data.active) self.assertEqual(question_data.snippet, 'def fact()') self.assertEqual(os.path.basename(file.file.path), "test.txt") - self.assertEqual([case.get_field_value() for case in test_case], self.test_case_upload_data) + self.assertEqual([case.get_field_value() for case in test_case], + self.test_case_upload_data + ) ############################################################################### -- cgit From 81a70c486823149b009627eb0194618ceeeb2369 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 16 Mar 2017 11:28:01 +0530 Subject: Change in models get_ordered_questions function - Add else condition to return fixed questions if order are not available --- yaksh/models.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 2569892..28af8f6 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -765,6 +765,8 @@ class QuestionPaper(models.Model): que_order = self.fixed_question_order.split(',') for que_id in que_order: ques.append(self.fixed_questions.get(id=que_id)) + else: + ques = self.fixed_questions.all() return ques def __str__(self): -- cgit From 5ae3841e4c432d5c0ee378ac9a4142f205b1dbb6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 17 Mar 2017 20:47:55 +0530 Subject: Change consolidate_answer_data to get assignment uploads --- yaksh/models.py | 16 ++++++++++------ yaksh/views.py | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 4dcd2ec..f6efeba 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -267,7 +267,7 @@ class Question(models.Model): # Check assignment upload based question grade_assignment_upload = models.BooleanField(default=False) - def consolidate_answer_data(self, user_answer): + def consolidate_answer_data(self, user_answer, user=None): question_data = {} metadata = {} test_case_data = [] @@ -283,13 +283,16 @@ class Question(models.Model): metadata['language'] = self.language metadata['partial_grading'] = self.partial_grading files = FileUpload.objects.filter(question=self) - assignment_files = AssignmentUpload.objects.filter() if files: metadata['file_paths'] = [(file.file.path, file.extract) for file in files] - if assignment_files: - metadata['assign_files'] = [(file.assignmentFile.path, False) - for file in assignment_files] + if self.type == "upload": + assignment_files = AssignmentUpload.objects.filter( + assignmentQuestion=self, user=user + ) + if assignment_files: + metadata['assign_files'] = [(file.assignmentFile.path, False) + for file in assignment_files] question_data['metadata'] = metadata return json.dumps(question_data) @@ -1272,7 +1275,8 @@ class HookTestCase(TestCase): success - Boolean, indicating if code was executed correctly mark_fraction - Float, indicating fraction of the weight to a test case - error - String, error message if success is false''' + error - String, error message if success is false + In case of assignment upload there will be no user answer ''' success = False err = "Incorrect Answer" # Please make this more specific mark_fraction = 0.0 diff --git a/yaksh/views.py b/yaksh/views.py index d4d11f5..bd3540d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -500,7 +500,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. - json_data = current_question.consolidate_answer_data(user_answer) \ + json_data = current_question.consolidate_answer_data(user_answer, user) \ if current_question.type == 'code' or \ current_question.type == 'upload' else None result = paper.validate_answer(user_answer, current_question, json_data) -- cgit From b4b33cc37244ed59765c705c6d882c00ddc88c62 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 17 Mar 2017 20:49:13 +0530 Subject: Intialize empty list for assignment files in hook --- yaksh/hook_evaluator.py | 1 + 1 file changed, 1 insertion(+) (limited to 'yaksh') diff --git a/yaksh/hook_evaluator.py b/yaksh/hook_evaluator.py index 052d220..0819ec9 100644 --- a/yaksh/hook_evaluator.py +++ b/yaksh/hook_evaluator.py @@ -12,6 +12,7 @@ from .grader import TimeoutException class HookEvaluator(BaseEvaluator): def __init__(self, metadata, test_case_data): self.files = [] + self.assign_files = [] # Set metadata values self.user_answer = metadata.get('user_answer') -- cgit From e0beba1dacb0d5de5ca8b59298345eb9d841d879 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 17 Mar 2017 22:17:31 +0530 Subject: Add grade_assignment_upload field to inital migrations --- yaksh/migrations/0001_initial.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/migrations/0001_initial.py b/yaksh/migrations/0001_initial.py index 8ee8c6a..8770a72 100644 --- a/yaksh/migrations/0001_initial.py +++ b/yaksh/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.5 on 2017-03-14 08:33 +# Generated by Django 1.9.5 on 2017-03-17 16:42 from __future__ import unicode_literals import datetime @@ -114,6 +114,7 @@ class Migration(migrations.Migration): ('active', models.BooleanField(default=True)), ('snippet', models.CharField(blank=True, max_length=256)), ('partial_grading', models.BooleanField(default=False)), + ('grade_assignment_upload', models.BooleanField(default=False)), ('tags', taggit.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user', to=settings.AUTH_USER_MODEL)), ], @@ -171,7 +172,7 @@ class Migration(migrations.Migration): name='HookTestCase', fields=[ ('testcase_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='yaksh.TestCase')), - ('hook_code', models.TextField(default='def check_answer(user_answer):\n \'\'\' Evaluates user answer to return -\n success - Boolean, indicating if code was executed correctly\n mark_fraction - Float, indicating fraction of the\n weight to a test case\n error - String, error message if success is false\'\'\'\n success = False\n err = "Incorrect Answer" # Please make this more specific\n mark_fraction = 0.0\n\n # write your code here\n\n return success, err, mark_fraction\n\n')), + ('hook_code', models.TextField(default='def check_answer(user_answer):\n \'\'\' Evaluates user answer to return -\n success - Boolean, indicating if code was executed correctly\n mark_fraction - Float, indicating fraction of the\n weight to a test case\n error - String, error message if success is false\n In case of assignment upload there will be no user answer \'\'\'\n success = False\n err = "Incorrect Answer" # Please make this more specific\n mark_fraction = 0.0\n\n # write your code here\n\n return success, err, mark_fraction\n\n')), ('weight', models.FloatField(default=1.0)), ], bases=('yaksh.testcase',), @@ -233,7 +234,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='assignmentupload', name='user', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='yaksh.Profile'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), ), migrations.AddField( model_name='answerpaper', -- cgit From 9b98de36b2354ce8d5414908ce62d9c665ebb89b Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 20 Mar 2017 19:09:08 +0530 Subject: Check in migration file to add field fixed_question_order to questionpaper --- .../0002_questionpaper_fixed_question_order.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 yaksh/migrations/0002_questionpaper_fixed_question_order.py (limited to 'yaksh') diff --git a/yaksh/migrations/0002_questionpaper_fixed_question_order.py b/yaksh/migrations/0002_questionpaper_fixed_question_order.py new file mode 100644 index 0000000..3cc46ed --- /dev/null +++ b/yaksh/migrations/0002_questionpaper_fixed_question_order.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.5 on 2017-03-20 13:32 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('yaksh', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='questionpaper', + name='fixed_question_order', + field=models.CharField(blank=True, max_length=255), + ), + ] -- cgit