diff options
author | maheshgudi | 2016-05-27 17:00:34 +0530 |
---|---|---|
committer | maheshgudi | 2016-05-27 17:00:34 +0530 |
commit | b7b4a4c4cbcd29c3f5f02216705b04c635439b8a (patch) | |
tree | 3e5fc4abe999d7a08f365f6ef0295eb1b4dc84f1 | |
parent | bfea715def3401af2f984d133e2d6dfdc72964ba (diff) | |
download | online_test-b7b4a4c4cbcd29c3f5f02216705b04c635439b8a.tar.gz online_test-b7b4a4c4cbcd29c3f5f02216705b04c635439b8a.tar.bz2 online_test-b7b4a4c4cbcd29c3f5f02216705b04c635439b8a.zip |
modified to make code pep8 compliant
-rw-r--r-- | yaksh/models.py | 56 | ||||
-rw-r--r-- | yaksh/tests.py | 56 | ||||
-rw-r--r-- | yaksh/views.py | 67 |
3 files changed, 97 insertions, 82 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 3a2506c..ec9a84b 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -7,7 +7,7 @@ from django.db import models from django.db.models import Q from django.contrib.auth.models import User from django.forms.models import model_to_dict -from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone import pytz @@ -62,21 +62,19 @@ def get_model_class(model): ############################################################################### class CourseManager(models.Manager): - def create_trial_course(self,user): + def create_trial_course(self, user): """Creates a trial course for testing questions""" - trial_course = self.create(name="trial_course", - enrollment="open", - creator=user, - is_trial=True - ) - trial_course.enroll(False,user) + trial_course = self.create(name="trial_course", enrollment="open", + creator=user, is_trial=True) + trial_course.enroll(False, user) return trial_course def delete_all_trial_courses(self, user): """Deletes all trial course for a user.""" - trial_course = self.filter(creator=user,is_trial=True) + trial_course = self.filter(creator=user, is_trial=True) trial_course.delete() + ############################################################################### class Course(models.Model): """ Course for students""" @@ -300,19 +298,20 @@ class Answer(models.Model): def __unicode__(self): return self.answer + ############################################################################### class QuizManager(models.Manager): def get_active_quizzes(self): - return self.filter(active=True,is_trial=False) + return self.filter(active=True, is_trial=False) - def create_trial_quiz(self,trial_course,user): + def create_trial_quiz(self, trial_course, user): """Creates a trial quiz for testing questions""" trial_quiz = self.create(course=trial_course, duration=1000, description="trial_quiz", is_trial=True, time_between_attempts=0 - ) + ) return trial_quiz def create_trial_from_quiz(self, original_quiz_id, user, mode): @@ -333,9 +332,9 @@ class QuizManager(models.Manager): return trial_quiz def delete_all_trial_quizzes(self, user): - trial_quiz = self.filter(Q(course__creator=user)|Q(course__teachers=user), - is_trial=True - ) + trial_quiz = self.filter(Q(course__creator=user) | + Q(course__teachers=user), is_trial=True + ) trial_quiz.delete() @@ -409,38 +408,39 @@ class QuestionPaperManager(models.Manager): 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": trial_questionpaper\ - .fixed_questions.all(), - "random_questions": trial_questionpaper\ - .random_questions.all() - } + trial_questionpaper = self.get(quiz_id=original_quiz_id) + trial_questions = {"fixed_questions": trial_questionpaper + .fixed_questions.all(), + "random_questions": trial_questionpaper + .random_questions.all() + } trial_questionpaper.pk = None trial_questionpaper.save() return trial_questionpaper, trial_questions - + def create_trial_paper_to_test_questions(self, trial_quiz, questions_list): """Creates a trial question paper to test selected questions""" if questions_list is not None: - trial_questionpaper=self.create(quiz=trial_quiz, - total_marks=10, - ) + trial_questionpaper = self.create(quiz=trial_quiz, + total_marks=10, + ) trial_questionpaper.fixed_questions.add(*questions_list) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): """Creates a trial question paper to test quiz.""" trial_questionpaper, trial_questions = self._create_trial_from_questionpaper\ - (original_quiz_id) + (original_quiz_id) trial_questionpaper.quiz = trial_quiz trial_questionpaper.fixed_questions\ - .add(*trial_questions["fixed_questions"]) + .add(*trial_questions["fixed_questions"]) trial_questionpaper.random_questions\ - .add(*trial_questions["random_questions"]) + .add(*trial_questions["random_questions"]) trial_questionpaper.save() return trial_questionpaper + ############################################################################### class QuestionPaper(models.Model): """Question paper stores the detail of the questions.""" diff --git a/yaksh/tests.py b/yaksh/tests.py index 5dc551f..71da672 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -151,6 +151,7 @@ class QuestionTestCases(unittest.TestCase): self.assertTrue(question_data.active) self.assertEqual(question_data.snippet, 'def fact()') + ############################################################################### class QuizTestCases(unittest.TestCase): def setUp(self): @@ -159,7 +160,7 @@ class QuizTestCases(unittest.TestCase): self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) self.trial_course = Course.objects.create_trial_course(self.creator) - + def test_quiz(self): """ Test Quiz""" self.assertEqual((self.quiz1.start_date_time).strftime('%Y-%m-%d'), @@ -189,14 +190,14 @@ class QuizTestCases(unittest.TestCase): def test_create_trial_quiz(self): """Test to check if trial quiz is created""" trial_quiz = Quiz.objects.create_trial_quiz(self.trial_course, - self.creator - ) + self.creator + ) self.assertEqual(trial_quiz.course, self.trial_course) self.assertEqual(trial_quiz.duration, 1000) self.assertEqual(trial_quiz.description, "trial_quiz") self.assertTrue(trial_quiz.is_trial) self.assertEqual(trial_quiz.time_between_attempts, 0) - + def test_create_trial_from_quiz_godmode(self): """Test to check if a copy of original quiz is created in godmode""" trial_quiz = Quiz.objects.create_trial_from_quiz(self.quiz1.id, @@ -232,23 +233,25 @@ class QuizTestCases(unittest.TestCase): def test_delete_all_trial_quizzes_creator(self): Quiz.objects.create_trial_from_quiz(self.quiz1.id, - self.creator, - "godmode" - ) + self.creator, + "godmode" + ) Quiz.objects.delete_all_trial_quizzes(self.creator) self.assertFalse(Quiz.objects.filter(course__creator=self.creator, - is_trial=True).exists() - ) + is_trial=True).exists() + ) + def test_delete_all_trial_quizzes_added_teacher(self): self.trial_course.add_teachers(self.teacher) Quiz.objects.create_trial_from_quiz(self.quiz1.id, - self.creator, - "godmode" - ) + self.creator, + "godmode" + ) Quiz.objects.delete_all_trial_quizzes(self.teacher) self.assertFalse(Quiz.objects.filter(course__teachers=self.teacher, is_trial=True).exists() - ) + ) + ############################################################################### class QuestionPaperTestCases(unittest.TestCase): @@ -306,8 +309,8 @@ class QuestionPaperTestCases(unittest.TestCase): user=self.user ) - ### For Test case - self.questions_list=[self.questions[3].id, self.questions[5].id] + # 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) @@ -365,15 +368,15 @@ class QuestionPaperTestCases(unittest.TestCase): 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 + (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.question_paper.fixed_questions.all() ) self.assertEqual(trial_paper.random_questions.all(), - self.question_paper.random_questions.all() + self.question_paper.random_questions.all() ) def test_create_trial_paper_to_test_questions(self): @@ -383,9 +386,11 @@ class QuestionPaperTestCases(unittest.TestCase): ) self.assertEqual(trial_paper.quiz, trial_quiz) self.assertEqual(self.questions_list, - self.question_paper.fixed_questions\ - .values_list("id", flat=True) + self.question_paper.fixed_questions + .values_list("id", flat=True) ) + + ############################################################################### class AnswerPaperTestCases(unittest.TestCase): @classmethod @@ -598,16 +603,19 @@ class CourseTestCases(unittest.TestCase): """Test to check if trial course is created""" # Test for manager method create_trial_course trial_course = Course.objects.create_trial_course(self.creator) - self.assertEqual(trial_course.name,"trial_course") + self.assertEqual(trial_course.name, "trial_course") self.assertEqual(trial_course.enrollment, "open") self.assertTrue(trial_course.active) - self.assertEqual(trial_course.students.get(user=self.creator.id), self.creator) + self.assertEqual(trial_course.students.get(user=self.creator.id), + self.creator + ) self.assertTrue(trial_course.is_trial) def test_delete_all_trial_courses(self): Course.objects.create_trial_course(self.creator) Course.objects.delete_all_trial_courses(self.creator) - self.assertFalse(Course.objects.filter(creator=self.creator, is_trial=True).exists()) + self.assertFalse(Course.objects.filter(creator=self.creator, + is_trial=True).exists()) ############################################################################### diff --git a/yaksh/views.py b/yaksh/views.py index 23024b2..56e66e0 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -236,19 +236,19 @@ def add_quiz(request, quiz_id=None): form = QuizForm(request.POST, user=user, instance=quiz) if form.is_valid(): form.save() - context["quiz_id"]=quiz_id + context["quiz_id"] = quiz_id return my_redirect("/exam/manage/") - + context["form"] = form return my_render_to_response('yaksh/add_quiz.html', context, - context_instance=ci) + context_instance=ci) else: if quiz_id is None: form = QuizForm(user=user) else: quiz = Quiz.objects.get(id=quiz_id) form = QuizForm(user=user, instance=quiz) - context["quiz_id"]=quiz_id + context["quiz_id"] = quiz_id context["form"] = form return my_render_to_response('yaksh/add_quiz.html', context, @@ -268,7 +268,9 @@ def show_all_questionpapers(request, questionpaper_id=None): return my_render_to_response('yaksh/showquestionpapers.html', context, context_instance=ci) else: - qu_papers = QuestionPaper.objects.get(id=questionpaper_id,is_trial=False) + qu_papers = QuestionPaper.objects.get(id=questionpaper_id, + is_trial=False + ) quiz = qu_papers.quiz fixed_questions = qu_papers.fixed_questions.all() random_questions = qu_papers.random_questions.all() @@ -285,8 +287,8 @@ rights/permissions and log in.""" user = request.user if user.is_authenticated() and is_moderator(user): question_papers = QuestionPaper.objects.filter(quiz__course__creator=user, - quiz__is_trial=False - ) + quiz__is_trial=False + ) trial_course = Course.objects.delete_all_trial_courses(user) trial_quiz = Quiz.objects.delete_all_trial_quizzes(user) users_per_paper = [] @@ -346,14 +348,14 @@ def start(request, questionpaper_id=None, attempt_num=None): msg = 'Quiz not found, please contact your '\ 'instructor/administrator. Please login again thereafter.' return complete(request, msg, attempt_num, questionpaper_id=None) - if not quest_paper.quiz.course.is_enrolled(user) : + if not quest_paper.quiz.course.is_enrolled(user): raise Http404('You are not allowed to view this page!') # prerequisite check and passing criteria if quest_paper.quiz.is_expired(): if is_moderator(user): return redirect("/exam/manage") return redirect("/exam/quizzes") - if quest_paper.quiz.has_prerequisite() and not quest_paper.is_prerequisite_passed(user): + if quest_paper.quiz.has_prerequisite() and not quest_paper.is_prerequisite_passed(user): return redirect("/exam/quizzes") # if any previous attempt last_attempt = AnswerPaper.objects.get_user_last_attempt( @@ -736,10 +738,10 @@ def monitor(request, questionpaper_id=None): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: - q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) - | Q(quiz__course__teachers=user), - quiz__course__is_trial=False - ).distinct() + q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) | + Q(quiz__course__teachers=user), + quiz__course__is_trial=False + ).distinct() context = {'papers': [], 'quiz': None, 'quizzes': q_paper} @@ -747,9 +749,10 @@ def monitor(request, questionpaper_id=None): context_instance=ci) # quiz_id is not None. try: - q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user)| - Q(quiz__course__teachers=user), quiz__course__is_trial=False, - id=questionpaper_id).distinct() + q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) | + Q(quiz__course__teachers=user), + quiz__course__is_trial=False, + id=questionpaper_id).distinct() except QuestionPaper.DoesNotExist: papers = [] q_paper = None @@ -849,7 +852,7 @@ def show_all_questions(request): else: msg = "Please select atleast one question" context['msg'] = msg - + if request.POST.get('test') == 'test': question_ids = request.POST.getlist("question") trial_paper = test_mode(user, "test_questions", question_ids, None) @@ -857,7 +860,6 @@ def show_all_questions(request): trial_paper.save() return my_redirect("/exam/start/1/{0}".format(trial_paper.id)) - questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) upload_form = UploadFileForm() @@ -939,14 +941,15 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): ci = RequestContext(request) if not current_user.is_authenticated() or not is_moderator(current_user): raise Http404('You are not allowed to view this page!') - course_details = Course.objects.filter(Q(creator=current_user)| - Q(teachers=current_user), is_trial=False).distinct() + course_details = Course.objects.filter(Q(creator=current_user) | + Q(teachers=current_user), + is_trial=False).distinct() context = {"course_details": course_details} if quiz_id is not None: questionpaper_id = QuestionPaper.objects.filter(quiz_id=quiz_id)\ .values("id") - user_details = AnswerPaper.objects.get_users_for_questionpaper\ - (questionpaper_id) + user_details = AnswerPaper.objects\ + .get_users_for_questionpaper(questionpaper_id) context = {"users": user_details, "quiz_id": quiz_id} if user_id is not None: @@ -1193,33 +1196,37 @@ def remove_teachers(request, course_id): if not is_moderator(user): raise Http404('You are not allowed to view this page!') - course = get_object_or_404(Course, creator=user, pk=course_id, is_trial=False) + course = get_object_or_404(Course, creator=user, + pk=course_id, is_trial=False + ) if request.method == "POST": teacher_ids = request.POST.getlist('remove') teachers = User.objects.filter(id__in=teacher_ids) course.remove_teachers(*teachers) return my_redirect('/exam/manage/courses') + def test_mode(user, mode, questions_list=None, quiz_id=None): """creates a trial question paper for the moderators""" if questions_list is not None and mode == "test_questions": trial_course = Course.objects.create_trial_course(user) - trial_quiz = Quiz.objects.create_trial_quiz(trial_course,user) + trial_quiz = Quiz.objects.create_trial_quiz(trial_course, user) trial_questionpaper = QuestionPaper.objects\ - .create_trial_paper_to_test_questions\ - (trial_quiz, questions_list) + .create_trial_paper_to_test_questions\ + (trial_quiz, questions_list) else: trial_quiz = Quiz.objects.create_trial_from_quiz(quiz_id, user, mode) trial_questionpaper = QuestionPaper.objects\ - .create_trial_paper_to_test_quiz\ - (trial_quiz, quiz_id) + .create_trial_paper_to_test_quiz\ + (trial_quiz, quiz_id) return trial_questionpaper + @login_required -def test_quiz(request, mode,quiz_id): +def test_quiz(request, mode, quiz_id): """creates a trial quiz for the moderators""" current_user = request.user trial_questionpaper = test_mode(current_user, mode, None, quiz_id) - return my_redirect("/exam/start/{0}".format(trial_questionpaper.id))
\ No newline at end of file + return my_redirect("/exam/start/{0}".format(trial_questionpaper.id)) |