diff options
author | adityacp | 2017-03-08 15:31:03 +0530 |
---|---|---|
committer | adityacp | 2017-03-10 15:41:07 +0530 |
commit | ca3c488193a7321b0ed0986856c4e74feadc199c (patch) | |
tree | a24478e4953c4acf1d37db088b8143ff0a52226a | |
parent | b8d6116cd59f68888aca8f2acceecc9b85ebf7de (diff) | |
download | online_test-ca3c488193a7321b0ed0986856c4e74feadc199c.tar.gz online_test-ca3c488193a7321b0ed0986856c4e74feadc199c.tar.bz2 online_test-ca3c488193a7321b0ed0986856c4e74feadc199c.zip |
Change views and models
- Remove Fixed Question Model
- Create a fixed question order attribute in models
-rw-r--r-- | yaksh/models.py | 60 | ||||
-rw-r--r-- | yaksh/test_models.py | 27 | ||||
-rw-r--r-- | yaksh/test_views.py | 8 | ||||
-rw-r--r-- | yaksh/views.py | 43 |
4 files changed, 68 insertions, 70 deletions
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,33 +757,21 @@ 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 will be selected for the quiz. 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} |