Commit 0f654d8d by Stephen Sanchez

First pass attempt at generation exceptions

parent 92ad6345
...@@ -2,42 +2,59 @@ ...@@ -2,42 +2,59 @@
Public interface for the submissions app. Public interface for the submissions app.
""" """
from datetime import datetime from django.db import DatabaseError
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from submissions.serializers import SubmissionSerializer from submissions.serializers import SubmissionSerializer
from submissions.models import Submission, StudentItem, SubmissionStruct from submissions.models import Submission, StudentItem, SubmissionStruct
def create_submission(student_item_struct, answer, submitted_at=None, attempt_number=None): class SubmissionAccessError(Exception):
pass
class SubmissionNotFoundError(Exception):
pass
def create_submission(student_item_struct, answer, submitted_at=None,
attempt_number=None):
"""Creates a submission for evaluation. """Creates a submission for evaluation.
Generic means by which to submit an answer for evaluation. Generic means by which to submit an answer for evaluation.
Args: Args:
student_item_struct (StudentItemStruct): The student_item this submission is associated with. This is used to student_item_struct (StudentItemStruct): The student_item this
determine which course, student, and location this submission belongs to. submission is associated with. This is used to determine which
course, student, and location this submission belongs to.
answer (str): The answer given by the student to be evaluated. answer (str): The answer given by the student to be evaluated.
submitted_at (date): The date in which this submission was submitted. If not specified, defaults to the current submitted_at (date): The date in which this submission was submitted.
date. If not specified, defaults to the current date.
attempt_number (int): A student may be able to submit multiple attempts per question. This allows the designated attempt_number (int): A student may be able to submit multiple attempts
attempt to be overridden. If the attempt is not specified, it will be incremented by the number of per question. This allows the designated attempt to be overridden.
submissions associated with this student_item. If the attempt is not specified, it will be incremented by the
number of submissions associated with this student_item.
Returns: Returns:
SubmissionStruct: A representation of the created Submission. SubmissionStruct: A representation of the created Submission.
Raises:
SubmissionAccessError: Raised when information regarding the student
item cannot be accessed or the submission cannot be saved.
""" """
student_item_model, _ = StudentItem.objects.get_or_create( try:
student_id=student_item_struct.student_id, student_item_model, _ = StudentItem.objects.get_or_create(
course_id=student_item_struct.course_id, student_item_struct._asdict())
item_id=student_item_struct.item_id, except DatabaseError as err:
item_type=student_item_struct.item_type, raise SubmissionAccessError(err)
)
if attempt_number is None: if attempt_number is None:
submissions = Submission.objects.filter(student_item=student_item_model)[:0] try:
submissions = Submission.objects.filter(
student_item=student_item_model)[:0]
except DatabaseError as err:
raise SubmissionAccessError(err)
attempt_number = submissions[0].attempt_number + 1 if submissions else 1 attempt_number = submissions[0].attempt_number + 1 if submissions else 1
model_kwargs = { model_kwargs = {
...@@ -48,38 +65,56 @@ def create_submission(student_item_struct, answer, submitted_at=None, attempt_nu ...@@ -48,38 +65,56 @@ def create_submission(student_item_struct, answer, submitted_at=None, attempt_nu
if submitted_at: if submitted_at:
model_kwargs["submitted_at"] = submitted_at model_kwargs["submitted_at"] = submitted_at
submission = Submission.objects.create(**model_kwargs) try:
submission = Submission.objects.create(**model_kwargs)
except DatabaseError as err:
raise SubmissionAccessError(err)
return SubmissionStruct(**SubmissionSerializer(submission).data) return SubmissionStruct(**SubmissionSerializer(submission).data)
def get_submissions(student_item_struct, limit=None): def get_submissions(student_item_struct, limit=None):
"""Retrieves the specified submission. """Retrieves the submissions for the specified student item,
ordered by most recent submitted date.
Returns the requested submission relative to the student item. Exception thrown if no submission is found relative Returns the submissions relative to the specified student item. Exception
to this location. thrown if no submission is found relative to this location.
Args: Args:
student_item_struct (StudentItemStruct): The location of the problem this submission is associated with, as student_item_struct (StudentItemStruct): The location of the problem
defined by a course, student, and item. this submission is associated with, as defined by a course, student,
limit (int): Optional parameter for limiting the returned number of submissions associated with this student and item.
item. If not specified, all associated submissions are returned. limit (int): Optional parameter for limiting the returned number of
submissions associated with this student item. If not specified, all
associated submissions are returned.
Returns: Returns:
List SubmissionStruct: A list of SubmissionStruct for the associated student item. List SubmissionStruct: A list of SubmissionStruct for the associated
student item.
Raises:
SubmissionAccessError: Raised when the associated student item cannot
be accessed.
SubmissionNotFoundError: Raised when a submission cannot be found for
the associated student item.
""" """
student_item_model, _ = StudentItem.objects.get_or_create( try:
student_id=student_item_struct.student_id, student_item_model, _ = StudentItem.objects.get_or_create(
course_id=student_item_struct.course_id, student_item_struct._asdict())
item_id=student_item_struct.item_id, except DatabaseError as err:
item_type=student_item_struct.item_type, raise SubmissionAccessError(err)
)
try:
submission_models = Submission.objects.filter(student_item=student_item_model) submission_models = Submission.objects.filter(
student_item=student_item_model)
except DatabaseError:
raise SubmissionNotFoundError()
if limit: if limit:
submission_models = submission_models[:limit] submission_models = submission_models[:limit]
return [SubmissionStruct(**SubmissionSerializer(submission).data) for submission in submission_models] return [SubmissionStruct(**SubmissionSerializer(submission).data) for
submission in submission_models]
def get_score(student_item): def get_score(student_item):
......
...@@ -47,7 +47,8 @@ class TestApi(TestCase): ...@@ -47,7 +47,8 @@ class TestApi(TestCase):
submissions = get_submissions(STUDENT_ITEM) submissions = get_submissions(STUDENT_ITEM)
self._assert_submission(submissions[0], ANSWER_ONE, 1, 2) self._assert_submission(submissions[0], ANSWER_ONE, 1, 2)
def _assert_submission(self, submission, expected_answer, expected_item, expected_attempt): def _assert_submission(self, submission, expected_answer, expected_item,
expected_attempt):
self.assertIsNotNone(submission) self.assertIsNotNone(submission)
self.assertEqual(submission.answer, expected_answer) self.assertEqual(submission.answer, expected_answer)
self.assertEqual(submission.student_item, expected_item) self.assertEqual(submission.student_item, expected_item)
......
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