Commit 7f1a0a5d by Afzal Wali

Added humanized time.

parent 4437e5e4
...@@ -19,6 +19,7 @@ from edx_proctoring.models import ( ...@@ -19,6 +19,7 @@ from edx_proctoring.models import (
) )
from edx_proctoring.serializers import ProctoredExamSerializer, ProctoredExamStudentAttemptSerializer, \ from edx_proctoring.serializers import ProctoredExamSerializer, ProctoredExamStudentAttemptSerializer, \
ProctoredExamStudentAllowanceSerializer ProctoredExamStudentAllowanceSerializer
from edx_proctoring.utils import humanized_time
def create_exam(course_id, content_id, exam_name, time_limit_mins, def create_exam(course_id, content_id, exam_name, time_limit_mins,
...@@ -252,7 +253,7 @@ def get_student_view(user_id, course_id, content_id, context): ...@@ -252,7 +253,7 @@ def get_student_view(user_id, course_id, content_id, context):
if student_view_template: if student_view_template:
template = loader.get_template(student_view_template) template = loader.get_template(student_view_template)
django_context = Context(context) django_context = Context(context)
total_time = str(timedelta(seconds=60 * context['default_time_limit_mins'])) total_time = humanized_time(context['default_time_limit_mins'])
django_context.update({ django_context.update({
'total_time': total_time, 'total_time': total_time,
'exam_id': exam_id, 'exam_id': exam_id,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<div class="sequence" data-exam-id="{{exam_id}}"> <div class="sequence" data-exam-id="{{exam_id}}">
<h3> <h3>
{% blocktrans %} {% blocktrans %}
{{ display_name }} is a Timed Exam ( {{total_time}} ) {{ display_name }} is a Timed Exam ({{total_time}})
{% endblocktrans %} {% endblocktrans %}
</h3> </h3>
<p> <p>
......
"""
File that contains tests for the util methods.
"""
import unittest
from edx_proctoring.utils import humanized_time
class TestHumanizedTime(unittest.TestCase):
"""
Class to test the humanized_time utility function
"""
def test_humanized_time(self):
"""
tests the humanized_time utility function against different values.
"""
human_time = humanized_time(0)
self.assertEqual(human_time, "0 Minutes")
human_time = humanized_time(1)
self.assertEqual(human_time, "1 Minute")
human_time = humanized_time(10)
self.assertEqual(human_time, "10 Minutes")
human_time = humanized_time(60)
self.assertEqual(human_time, "1 Hour")
human_time = humanized_time(61)
self.assertEqual(human_time, "1 Hour and 1 Minute")
human_time = humanized_time(62)
self.assertEqual(human_time, "1 Hour and 2 Minutes")
human_time = humanized_time(120)
self.assertEqual(human_time, "2 Hours")
human_time = humanized_time(121)
self.assertEqual(human_time, "2 Hours and 1 Minute")
human_time = humanized_time(150)
self.assertEqual(human_time, "2 Hours and 30 Minutes")
human_time = humanized_time(180)
self.assertEqual(human_time, "3 Hours")
...@@ -49,26 +49,6 @@ class ProctoredExamsApiTests(LoggedInTestCase): ...@@ -49,26 +49,6 @@ class ProctoredExamsApiTests(LoggedInTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
class StudentProctoredExamAttempt(LoggedInTestCase):
"""
Tests for StudentProctoredExamAttempt
"""
def setUp(self):
super(StudentProctoredExamAttempt, self).setUp()
self.user.is_staff = True
self.user.save()
self.client.login_user(self.user)
def test_get_exam_attempt(self):
"""
Test Case for retrieving student proctored exam attempt status.
"""
response = self.client.get(
reverse('edx_proctoring.proctored_exam.attempt')
)
self.assertEqual(response.status_code, 200)
class ProctoredExamViewTests(LoggedInTestCase): class ProctoredExamViewTests(LoggedInTestCase):
""" """
Tests for the ProctoredExamView Tests for the ProctoredExamView
...@@ -476,6 +456,39 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase): ...@@ -476,6 +456,39 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase):
response_data = json.loads(response.content) response_data = json.loads(response.content)
self.assertEqual(response_data['detail'], 'Error. Trying to stop an exam that is not in progress.') self.assertEqual(response_data['detail'], 'Error. Trying to stop an exam that is not in progress.')
def test_get_exam_attempt(self):
"""
Test Case for retrieving student proctored exam attempt status.
"""
# Create an exam.
proctored_exam = ProctoredExam.objects.create(
course_id='a/b/c',
content_id='test_content',
exam_name='Test Exam',
external_id='123aXqe3',
time_limit_mins=90
)
response = self.client.get(
reverse('edx_proctoring.proctored_exam.attempt')
)
self.assertEqual(response.status_code, 200)
attempt_data = {
'exam_id': proctored_exam.id,
'user_id': self.student_taking_exam.id,
'external_id': proctored_exam.external_id
}
response = self.client.post(
reverse('edx_proctoring.proctored_exam.attempt'),
attempt_data
)
self.assertEqual(response.status_code, 200)
response = self.client.get(
reverse('edx_proctoring.proctored_exam.attempt')
)
self.assertEqual(response.status_code, 200)
class TestExamAllowanceView(LoggedInTestCase): class TestExamAllowanceView(LoggedInTestCase):
""" """
......
...@@ -13,3 +13,45 @@ class AuthenticatedAPIView(APIView): ...@@ -13,3 +13,45 @@ class AuthenticatedAPIView(APIView):
""" """
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)
def humanized_time(time_in_minutes):
"""
Converts the given value in minutes to a more human readable format
1 -> 1 Minute
2 -> 2 Minutes
60 -> 1 hour
90 -> 1 hour and 30 Minutes
120 -> 2 hours
"""
hours = int(time_in_minutes / 60)
minutes = time_in_minutes % 60
template = ""
hours_present = False
if hours == 0:
hours_present = False
elif hours == 1:
template = "{num_of_hours} Hour"
hours_present = True
elif hours >= 2:
template = "{num_of_hours} Hours"
hours_present = True
if minutes == 0:
if not hours_present:
template = "{num_of_minutes} Minutes"
elif minutes == 1:
if hours_present:
template += " and {num_of_minutes} Minute"
else:
template += "{num_of_minutes} Minute"
elif minutes >= 2:
if hours_present:
template += " and {num_of_minutes} Minutes"
else:
template += "{num_of_minutes} Minutes"
human_time = template.format(num_of_hours=hours, num_of_minutes=minutes)
return human_time
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment