Commit 0f654d8d by Stephen Sanchez

First pass attempt at generation exceptions

parent 92ad6345
......@@ -2,42 +2,59 @@
Public interface for the submissions app.
"""
from datetime import datetime
from django.db import DatabaseError
from django.utils.encoding import force_unicode
from submissions.serializers import SubmissionSerializer
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.
Generic means by which to submit an answer for evaluation.
Args:
student_item_struct (StudentItemStruct): The student_item this submission is associated with. This is used to
determine which course, student, and location this submission belongs to.
student_item_struct (StudentItemStruct): The student_item this
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.
submitted_at (date): The date in which this submission was submitted. 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 to be overridden. If the attempt is not specified, it will be incremented by the number of
submissions associated with this student_item.
submitted_at (date): The date in which this submission was submitted.
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 to be overridden.
If the attempt is not specified, it will be incremented by the
number of submissions associated with this student_item.
Returns:
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(
student_id=student_item_struct.student_id,
course_id=student_item_struct.course_id,
item_id=student_item_struct.item_id,
item_type=student_item_struct.item_type,
)
try:
student_item_model, _ = StudentItem.objects.get_or_create(
student_item_struct._asdict())
except DatabaseError as err:
raise SubmissionAccessError(err)
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
model_kwargs = {
......@@ -48,38 +65,56 @@ def create_submission(student_item_struct, answer, submitted_at=None, attempt_nu
if 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)
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
to this location.
Returns the submissions relative to the specified student item. Exception
thrown if no submission is found relative to this location.
Args:
student_item_struct (StudentItemStruct): The location of the problem this submission is associated with, as
defined by a course, student, and item.
limit (int): Optional parameter for limiting the returned number of submissions associated with this student
item. If not specified, all associated submissions are returned.
student_item_struct (StudentItemStruct): The location of the problem
this submission is associated with, as defined by a course, student,
and item.
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:
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(
student_id=student_item_struct.student_id,
course_id=student_item_struct.course_id,
item_id=student_item_struct.item_id,
item_type=student_item_struct.item_type,
)
submission_models = Submission.objects.filter(student_item=student_item_model)
try:
student_item_model, _ = StudentItem.objects.get_or_create(
student_item_struct._asdict())
except DatabaseError as err:
raise SubmissionAccessError(err)
try:
submission_models = Submission.objects.filter(
student_item=student_item_model)
except DatabaseError:
raise SubmissionNotFoundError()
if 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):
......
......@@ -47,7 +47,8 @@ class TestApi(TestCase):
submissions = get_submissions(STUDENT_ITEM)
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.assertEqual(submission.answer, expected_answer)
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