summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/models.py40
-rw-r--r--yaksh/test_models.py79
2 files changed, 101 insertions, 18 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index fd4ae3f..f065190 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -78,7 +78,6 @@ string_check_type = (
attempts = [(i, i) for i in range(1, 6)]
attempts.append((-1, 'Infinite'))
-days_between_attempts = [(j, j) for j in range(401)]
test_status = (
('inprogress', 'Inprogress'),
@@ -297,8 +296,8 @@ class Quiz(models.Model):
# Number of attempts for the quiz
attempts_allowed = models.IntegerField(default=1, choices=attempts)
- time_between_attempts = models.IntegerField(
- "Number of Days", choices=days_between_attempts
+ time_between_attempts = models.FloatField(
+ "Time Between Quiz Attempts in hours"
)
is_trial = models.BooleanField(default=False)
@@ -338,20 +337,32 @@ class Quiz(models.Model):
return demo_quiz
def get_total_students(self, course):
+ try:
+ qp = self.questionpaper_set.get().id
+ except QuestionPaper.DoesNotExist:
+ qp = None
return AnswerPaper.objects.filter(
- question_paper=self.questionpaper_set.get().id,
+ question_paper=qp,
course=course
).values_list("user", flat=True).distinct().count()
def get_passed_students(self, course):
+ try:
+ qp = self.questionpaper_set.get().id
+ except QuestionPaper.DoesNotExist:
+ qp = None
return AnswerPaper.objects.filter(
- question_paper=self.questionpaper_set.get().id,
+ question_paper=qp,
course=course, passed=True
).values_list("user", flat=True).distinct().count()
def get_failed_students(self, course):
+ try:
+ qp = self.questionpaper_set.get().id
+ except QuestionPaper.DoesNotExist:
+ qp = None
return AnswerPaper.objects.filter(
- question_paper=self.questionpaper_set.get().id,
+ question_paper=qp,
course=course, passed=False
).values_list("user", flat=True).distinct().count()
@@ -1165,7 +1176,7 @@ class QuestionPaper(models.Model):
return all_questions
def make_answerpaper(self, user, ip, attempt_num, course_id):
- """Creates an answer paper for the user to attempt the quiz"""
+ """Creates an answer paper for the user to attempt the quiz"""
try:
ans_paper = AnswerPaper.objects.get(user=user,
attempt_number=attempt_num,
@@ -1190,14 +1201,6 @@ class QuestionPaper(models.Model):
ans_paper.questions_order = ",".join(question_ids)
ans_paper.save()
ans_paper.questions_unanswered.add(*questions)
- except AnswerPaper.MultipleObjectsReturned:
- ans_paper = AnswerPaper.objects.get(user=user,
- attempt_number=attempt_num,
- question_paper=self,
- course_id=course_id
- ).order_by('-id')
- ans_paper = ans_paper[0]
-
return ans_paper
def _is_attempt_allowed(self, user, course_id):
@@ -1212,7 +1215,7 @@ class QuestionPaper(models.Model):
user=user, questionpaper=self, course_id=course_id
)
if last_attempt:
- time_lag = (timezone.now() - last_attempt.start_time).days
+ time_lag = (timezone.now() - last_attempt.start_time).total_seconds() / 3600
return time_lag >= self.quiz.time_between_attempts
else:
return True
@@ -1507,6 +1510,11 @@ class AnswerPaper(models.Model):
objects = AnswerPaperManager()
+ class Meta:
+ unique_together = ('user', 'question_paper',
+ 'attempt_number', "course"
+ )
+
def get_per_question_score(self, question_id):
if question_id not in self.get_questions().values_list('id', flat=True):
return 'NA'
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index 8205b88..cd4279b 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -12,6 +12,7 @@ from datetime import datetime, timedelta
from django.utils import timezone
import pytz
from django.contrib.auth.models import Group
+from django.db import IntegrityError
from django.core.files import File
from django.forms.models import model_to_dict
from textwrap import dedent
@@ -475,10 +476,73 @@ class QuizTestCases(unittest.TestCase):
self.course = Course.objects.get(name="Python Course")
self.creator = User.objects.get(username="creator")
self.teacher = User.objects.get(username="demo_user2")
+ self.student1 = User.objects.get(username='demo_user3')
+ self.student2 = User.objects.get(username='demo_user4')
self.quiz1 = Quiz.objects.get(description='demo quiz 1')
self.quiz2 = Quiz.objects.get(description='demo quiz 2')
+ self.quiz3 = Quiz.objects.create(
+ start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ duration=30, active=True,
+ attempts_allowed=1, time_between_attempts=0,
+ description='demo quiz 3', pass_criteria=0,
+ instructions="Demo Instructions"
+ )
+ self.question_paper3 = QuestionPaper.objects.create(quiz=self.quiz3)
+ self.quiz4 = Quiz.objects.create(
+ start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ duration=30, active=True,
+ attempts_allowed=1, time_between_attempts=0,
+ description='demo quiz 4', pass_criteria=0,
+ instructions="Demo Instructions"
+ )
+ self.answerpaper1 = AnswerPaper.objects.create(
+ user=self.student1,
+ question_paper=self.question_paper3,
+ course=self.course,
+ attempt_number=1,
+ start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc),
+ passed=True
+ )
+ self.answerpaper2 = AnswerPaper.objects.create(
+ user=self.student2,
+ question_paper=self.question_paper3,
+ course=self.course,
+ attempt_number=1,
+ start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc),
+ passed=False
+ )
self.trial_course = Course.objects.create_trial_course(self.creator)
+ def tearDown(self):
+ self.answerpaper1.delete()
+ self.answerpaper2.delete()
+ self.trial_course.delete()
+ self.quiz3.delete()
+ self.quiz4.delete()
+ self.question_paper3.delete()
+
+ def test_get_total_students(self):
+ self.assertEqual(self.quiz3.get_total_students(self.course), 2)
+
+ def test_get_total_students_without_questionpaper(self):
+ self.assertEqual(self.quiz4.get_total_students(self.course), 0)
+
+ def test_get_passed_students(self):
+ self.assertEqual(self.quiz3.get_passed_students(self.course), 1)
+
+ def test_get_passed_students_without_questionpaper(self):
+ self.assertEqual(self.quiz4.get_passed_students(self.course), 0)
+
+ def test_get_failed_students(self):
+ self.assertEqual(self.quiz3.get_failed_students(self.course), 1)
+
+ def test_get_failed_students_without_questionpaper(self):
+ self.assertEqual(self.quiz4.get_failed_students(self.course), 0)
+
def test_quiz(self):
""" Test Quiz"""
self.assertEqual((self.quiz1.start_date_time).strftime('%Y-%m-%d'),
@@ -556,7 +620,6 @@ class QuizTestCases(unittest.TestCase):
self.assertTrue(self.quiz1.view_answerpaper)
-
###############################################################################
class QuestionPaperTestCases(unittest.TestCase):
@classmethod
@@ -799,7 +862,8 @@ class AnswerPaperTestCases(unittest.TestCase):
question_paper=self.question_paper,
start_time=self.start_time,
end_time=self.end_time,
- user_ip=self.ip
+ user_ip=self.ip,
+ course=self.course
)
self.attempted_papers = AnswerPaper.objects.filter(
question_paper=self.question_paper,
@@ -1339,6 +1403,17 @@ class AnswerPaperTestCases(unittest.TestCase):
self.assertEqual(self.user2_answerpaper2.current_question(),
self.question1)
+ def test_duplicate_attempt_answerpaper(self):
+ with self.assertRaises(IntegrityError):
+ new_answerpaper = AnswerPaper.objects.create(
+ user=self.answerpaper.user,
+ question_paper=self.answerpaper.question_paper,
+ attempt_number=self.answerpaper.attempt_number,
+ start_time=self.answerpaper.start_time,
+ end_time=self.answerpaper.end_time,
+ course=self.answerpaper.course
+ )
+
###############################################################################
class CourseTestCases(unittest.TestCase):