Commit 38e7bbbe by Muhammad Shoaib

PHX-161 added the new helper methods to create/update review policy for proctored exams

parent 089e1d69
......@@ -26,6 +26,8 @@ from edx_proctoring.exceptions import (
ProctoredExamIllegalStatusTransition,
ProctoredExamPermissionDenied,
ProctoredExamNotActiveException,
ProctoredExamReviewPolicyNotFoundException,
ProctoredExamReviewPolicyAlreadyExists
)
from edx_proctoring.models import (
ProctoredExam,
......@@ -38,6 +40,7 @@ from edx_proctoring.serializers import (
ProctoredExamSerializer,
ProctoredExamStudentAttemptSerializer,
ProctoredExamStudentAllowanceSerializer,
ProctoredExamReviewPolicySerializer
)
from edx_proctoring.utils import (
humanized_time,
......@@ -96,6 +99,84 @@ def create_exam(course_id, content_id, exam_name, time_limit_mins, due_date=None
return proctored_exam.id
def create_exam_review_policy(exam_id=None, set_by_user_id=None, review_policy=None):
"""
Creates a new exam_review_policy entity, if the review_policy for exam_id do not already exist.
If already exists, then raise exception.
Returns: id (PK)
"""
exam_review_policy = ProctoredExamReviewPolicy.get_review_policy_for_exam(exam_id)
if exam_review_policy is not None:
raise ProctoredExamReviewPolicyAlreadyExists
exam_review_policy = ProctoredExamReviewPolicy.objects.create(
proctored_exam_id=exam_id,
set_by_user_id=set_by_user_id,
review_policy=review_policy
)
log_msg = (
u'Created ProctoredExamReviewPolicy ({review_policy}) with parameters: exam_id={exam_id}, '
u'set_by_user_id={set_by_user_id}'.format(
exam_id=exam_id,
review_policy=review_policy,
set_by_user_id=set_by_user_id
)
)
log.info(log_msg)
return exam_review_policy.id
def update_review_policy(exam_id=None, set_by_user_id=None, review_policy=None):
"""
Given a exam id, update the existing record, otherwise raise exception if not found.
If an argument is not passed in, then do not change it's current value.
Returns: review_policy_id
"""
log_msg = (
u'Updating exam review policy with exam_id {exam_id}'
u'set_by_user_id={set_by_user_id}, review_policy={review_policy}'
.format(
exam_id=exam_id, set_by_user_id=set_by_user_id, review_policy=review_policy,
)
)
log.info(log_msg)
exam_review_policy = ProctoredExamReviewPolicy.get_review_policy_for_exam(exam_id)
if exam_review_policy is None:
raise ProctoredExamReviewPolicyNotFoundException
exam_review_policy.set_by_user_id = set_by_user_id
exam_review_policy.proctored_exam_id = exam_id
exam_review_policy.review_policy = review_policy
exam_review_policy.save()
def get_review_policy_by_exam_id(exam_id):
"""
Looks up exam by the Primary Key. Raises exception if not found.
Returns dictionary version of the Django ORM object
e.g.
{
"id": 1
"proctored_exam": "{object}",
"user": "{object}",
"exam_review_rules": "review rules value"
}
"""
exam_review_policy = ProctoredExamReviewPolicy.get_review_policy_for_exam(exam_id)
if exam_review_policy is None:
raise ProctoredExamReviewPolicyNotFoundException
return ProctoredExamReviewPolicySerializer(exam_review_policy).data
def update_exam(exam_id, exam_name=None, time_limit_mins=None, due_date=constants.MINIMUM_TIME,
is_proctored=None, is_practice_exam=None, external_id=None, is_active=None):
"""
......
......@@ -21,6 +21,18 @@ class ProctoredExamNotFoundException(ProctoredBaseException):
"""
class ProctoredExamReviewPolicyNotFoundException(ProctoredBaseException):
"""
Raised when a look up fails.
"""
class ProctoredExamReviewPolicyAlreadyExists(ProctoredBaseException):
"""
Raised when trying to create an ProctoredExamReviewPolicy that already exists.
"""
class ProctoredExamNotActiveException(ProctoredBaseException):
"""
Raised when a look up fails.
......
......@@ -2,7 +2,12 @@
from rest_framework import serializers
from rest_framework.fields import DateTimeField
from django.contrib.auth.models import User
from edx_proctoring.models import ProctoredExam, ProctoredExamStudentAttempt, ProctoredExamStudentAllowance
from edx_proctoring.models import (
ProctoredExam,
ProctoredExamStudentAttempt,
ProctoredExamStudentAllowance,
ProctoredExamReviewPolicy
)
class ProctoredExamSerializer(serializers.ModelSerializer):
......@@ -95,3 +100,20 @@ class ProctoredExamStudentAllowanceSerializer(serializers.ModelSerializer):
fields = (
"id", "created", "modified", "user", "key", "value", "proctored_exam"
)
class ProctoredExamReviewPolicySerializer(serializers.ModelSerializer):
"""
Serializer for the ProctoredExamStudentAllowance Model.
"""
proctored_exam = ProctoredExamSerializer()
set_by_user = UserSerializer()
class Meta:
"""
Meta Class
"""
model = ProctoredExamReviewPolicy
fields = (
"id", "created", "modified", "set_by_user", "proctored_exam", "review_policy"
)
......@@ -39,6 +39,9 @@ from edx_proctoring.api import (
_check_for_attempt_timeout,
_get_ordered_prerequisites,
_are_prerequirements_satisfied,
create_exam_review_policy,
get_review_policy_by_exam_id,
update_review_policy
)
from edx_proctoring.exceptions import (
ProctoredExamAlreadyExists,
......@@ -49,7 +52,9 @@ from edx_proctoring.exceptions import (
UserNotFoundException,
ProctoredExamIllegalStatusTransition,
ProctoredExamPermissionDenied,
AllowanceValueNotAllowedException
AllowanceValueNotAllowedException,
ProctoredExamReviewPolicyAlreadyExists,
ProctoredExamReviewPolicyNotFoundException
)
from edx_proctoring.models import (
ProctoredExam,
......@@ -398,6 +403,94 @@ class ProctoredExamApiTests(LoggedInTestCase):
exams = get_all_exams_for_course(self.course_id, False)
self.assertEqual(len(exams), 4)
def test_create_exam_review_policy(self):
"""
Test to create a new exam review policy for
proctored exam and tests that it stores in the
db correctly
"""
proctored_exam = get_exam_by_id(self.proctored_exam_id)
create_exam_review_policy(
exam_id=proctored_exam['id'],
set_by_user_id=self.user_id,
review_policy=u'allow use of paper'
)
# now get the exam review policy for the proctored exam
exam_review_policy = get_review_policy_by_exam_id(proctored_exam['id'])
self.assertEqual(exam_review_policy['proctored_exam']['id'], proctored_exam['id'])
self.assertEqual(exam_review_policy['set_by_user']['id'], self.user_id)
self.assertEqual(exam_review_policy['review_policy'], u'allow use of paper')
def test_update_exam_review_policy(self):
"""
Test to update existing exam review policy for
proctored exam and tests that it stores in the
db correctly
"""
proctored_exam = get_exam_by_id(self.proctored_exam_id)
create_exam_review_policy(
exam_id=proctored_exam['id'],
set_by_user_id=self.user_id,
review_policy=u'allow use of paper'
)
# now update the exam review policy for the proctored exam
update_review_policy(
exam_id=proctored_exam['id'],
set_by_user_id=self.user_id,
review_policy=u'allow use of calculator'
)
# now get the updated exam review policy for the proctored exam
exam_review_policy = get_review_policy_by_exam_id(proctored_exam['id'])
self.assertEqual(exam_review_policy['proctored_exam']['id'], proctored_exam['id'])
self.assertEqual(exam_review_policy['set_by_user']['id'], self.user_id)
self.assertEqual(exam_review_policy['review_policy'], u'allow use of calculator')
def test_update_non_existing_exam_review_policy(self):
"""
Test to update non existing exam review policy for
proctored exam and it will raises exception
"""
# update the non existing exam review policy for the proctored exam
with self.assertRaises(ProctoredExamReviewPolicyNotFoundException):
update_review_policy(
exam_id=self.practice_exam_id,
set_by_user_id=10,
review_policy=u'allow use of calculator'
)
def test_create_exam_review_policy_with_same_exam_id(self):
"""
Test to create a same exam review policy will raise exception
"""
proctored_exam = get_exam_by_id(self.proctored_exam_id)
create_exam_review_policy(
exam_id=proctored_exam['id'],
set_by_user_id=self.user_id,
review_policy=u'allow use of paper'
)
# create the same review policy again will raise exception
with self.assertRaises(ProctoredExamReviewPolicyAlreadyExists):
create_exam_review_policy(
exam_id=proctored_exam['id'],
set_by_user_id=self.user_id,
review_policy=u'allow use of paper'
)
def test_get_non_existing_review_policy_raises_exception(self):
"""
Test to get the non-existing review policy raises exception
"""
with self.assertRaises(ProctoredExamReviewPolicyNotFoundException):
# now get the exam review policy for the proctored exam
get_review_policy_by_exam_id(self.practice_exam_id)
def test_get_timed_exam(self):
"""
test to get the exam by the exam_id and
......
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