diff options
-rw-r--r-- | yaksh/models.py | 16 | ||||
-rw-r--r-- | yaksh/tests.py | 18 | ||||
-rw-r--r-- | yaksh/views.py | 6 |
3 files changed, 25 insertions, 15 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 6ee02e1..87f7c13 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -299,10 +299,12 @@ class Quiz(models.Model): # The end date and time of the quiz end_date_time = models.DateTimeField("End Date and Time of the quiz", - default=datetime(2199, 1, 1, - tzinfo=pytz.timezone\ - (timezone.get_current_timezone_name())), - null=True) + default=datetime(2199, 1, 1, + tzinfo=pytz.timezone + (timezone. + get_current_timezone_name()) + ), + null=True) # This is always in minutes. duration = models.IntegerField("Duration of quiz in minutes", default=20) @@ -390,7 +392,7 @@ class QuestionPaper(models.Model): user_ip=ip, attempt_number=attempt_num ) - ans_paper.start_time = datetime.now() + ans_paper.start_time = timezone.now() ans_paper.end_time = ans_paper.start_time + \ timedelta(minutes=self.quiz.duration) ans_paper.question_paper = self @@ -414,7 +416,7 @@ class QuestionPaper(models.Model): last_attempt = AnswerPaper.objects.get_user_last_attempt(user=user, questionpaper=self) if last_attempt: - time_lag = (datetime.today() - last_attempt.start_time.replace(tzinfo=None)).days + time_lag = (timezone.now() - last_attempt.start_time).days return time_lag >= self.quiz.time_between_attempts else: return True @@ -660,7 +662,7 @@ class AnswerPaper(models.Model): def time_left(self): """Return the time remaining for the user in seconds.""" - dt = datetime.now() - self.start_time.replace(tzinfo=None) + dt = timezone.now() - self.start_time try: secs = dt.total_seconds() except AttributeError: diff --git a/yaksh/tests.py b/yaksh/tests.py index f2083dd..e71a78f 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -4,6 +4,8 @@ from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ StdoutBasedTestCase import json from datetime import datetime, timedelta +from django.utils import timezone +import pytz from django.contrib.auth.models import Group def setUpModule(): @@ -31,16 +33,20 @@ def setUpModule(): Question.objects.create(summary='Q%d' % (i), points=1, type='code', user=user) # create a quiz - quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), - end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0), + quiz = 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', pass_criteria=0, language='Python', prerequisite=None, course=course) - Quiz.objects.create(start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0), - end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), + Quiz.objects.create(start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), + end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), duration=30, active=False, attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, @@ -303,7 +309,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1,2,3]) - self.start_time = datetime.now() + self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) # create answerpaper @@ -386,7 +392,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertFalse(self.answerpaper.is_attempt_inprogress()) def test_set_end_time(self): - current_time = datetime.now() + current_time = timezone.now() self.answerpaper.set_end_time(current_time) self.assertEqual(self.answerpaper.end_time,current_time) diff --git a/yaksh/views.py b/yaksh/views.py index 2a3adbf..db2f9bf 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -16,6 +16,8 @@ from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required from django.contrib.auth.models import Group from django.forms.models import inlineformset_factory +from django.utils import timezone +import pytz from taggit.models import Tag from itertools import chain import json @@ -464,7 +466,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): new_answer.error = result.get('error') new_answer.save() paper.update_marks('inprogress') - paper.set_end_time(datetime.now()) + paper.set_end_time(timezone.now()) if not result.get('success'): # Should only happen for non-mcq questions. new_answer.answer = user_code new_answer.save() @@ -542,7 +544,7 @@ def complete(request, reason=None, attempt_num=None, questionpaper_id=None): paper = AnswerPaper.objects.get(user=user, question_paper=q_paper, attempt_number=attempt_num) paper.update_marks() - paper.set_end_time(datetime.now()) + paper.set_end_time(timezone.now()) if paper.percent == 100: message = "You answered all the questions correctly.\ You have been logged out successfully,\ |