Commit 915f2261 by Chris Dodge

add management command to set status

parent 66eedd50
"""
This is a python module
"""
"""
Django management command to manually set the attempt status for a user in a proctored exam
"""
from optparse import make_option
from django.core.management.base import BaseCommand
from edx_proctoring.api import (
update_attempt_status,
get_exam_by_id
)
from edx_proctoring.models import ProctoredExamStudentAttemptStatus
class Command(BaseCommand):
"""
Django Management command to force a background check of all possible notifications
"""
option_list = BaseCommand.option_list + (
make_option('-e', '--exam',
metavar='EXAM_ID',
dest='exam_id',
help='exam_id to change'),
make_option('-u', '--user',
metavar='USER',
dest='user',
help="user_id of user to affect"),
make_option('-t', '--to',
metavar='TO_STATUS',
dest='to_status',
help='the status to set'),
)
def handle(self, *args, **options):
"""
Management command entry point, simply call into the signal firiing
"""
exam_id = options['exam_id']
user_id = options['user_id']
to_status = options['to_status']
msg = (
'Running management command to update user {user_id} '
'attempt status on exam_id {exam_id} to {to_status}'.format(
user_id=user_id,
exam_id=exam_id,
to_status=to_status
)
)
print msg
if not ProctoredExamStudentAttemptStatus.is_valid_status(to_status):
raise Exception('{to_status} is not a valid attempt status!'.format(to_status=to_status))
# get exam, this will throw exception if does not exist, so let it bomb out
get_exam_by_id(exam_id)
update_attempt_status(exam_id, user_id, to_status)
print 'Completed!'
"""
Tests for the set_attempt_status management command
"""
from datetime import datetime
import pytz
from edx_proctoring.tests.utils import LoggedInTestCase
from edx_proctoring.api import create_exam, get_exam_attempt
from edx_proctoring.management.commands import set_attempt_status
from edx_proctoring.models import ProctoredExamStudentAttemptStatus, ProctoredExamStudentAttempt
from edx_proctoring.tests.test_services import (
MockCreditService,
)
from edx_proctoring.runtime import set_runtime_service
class SetAttemptStatusTests(LoggedInTestCase):
"""
Coverage of the set_attempt_status.py file
"""
def setUp(self):
"""
Build up test data
"""
super(SetAttemptStatusTests, self).setUp()
set_runtime_service('credit', MockCreditService())
self.exam_id = create_exam(
course_id='foo',
content_id='bar',
exam_name='Test Exam',
time_limit_mins=90
)
ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=self.exam_id,
user_id=self.user.id,
external_id='foo',
started_at=datetime.now(pytz.UTC),
status=ProctoredExamStudentAttemptStatus.started,
allowed_time_limit_mins=10,
taking_as_proctored=True,
is_sample_attempt=False
)
def test_run_comand(self):
"""
Run the management command
"""
set_attempt_status.Command().handle(
exam_id=self.exam_id,
user_id=self.user.id,
to_status=ProctoredExamStudentAttemptStatus.rejected
)
attempt = get_exam_attempt(self.exam_id, self.user.id)
self.assertEqual(attempt['status'], ProctoredExamStudentAttemptStatus.rejected)
set_attempt_status.Command().handle(
exam_id=self.exam_id,
user_id=self.user.id,
to_status=ProctoredExamStudentAttemptStatus.verified
)
attempt = get_exam_attempt(self.exam_id, self.user.id)
self.assertEqual(attempt['status'], ProctoredExamStudentAttemptStatus.verified)
def test_bad_status(self):
"""
Try passing a bad status
"""
with self.assertRaises(Exception):
set_attempt_status.Command().handle(
exam_id=self.exam_id,
user_id=self.user.id,
to_status='bad'
)
...@@ -200,6 +200,13 @@ class ProctoredExamStudentAttemptStatus(object): ...@@ -200,6 +200,13 @@ class ProctoredExamStudentAttemptStatus(object):
return cls.status_alias_mapping.get(status, '') return cls.status_alias_mapping.get(status, '')
@classmethod
def is_valid_status(cls, status):
"""
Makes sure that passed in status string is valid
"""
return cls.is_completed_status(status) or cls.is_incomplete_status(status)
class ProctoredExamStudentAttemptManager(models.Manager): class ProctoredExamStudentAttemptManager(models.Manager):
""" """
......
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