Commit 92ad6345 by Stephen Sanchez

More updates based on feedback.

parent 2cf3c4f4
...@@ -4,39 +4,75 @@ Public interface for the submissions app. ...@@ -4,39 +4,75 @@ Public interface for the submissions app.
""" """
from datetime import datetime from datetime import datetime
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, answer, submitted_at=None, attempt_number=None): 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.
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.
Returns:
SubmissionStruct: A representation of the created Submission.
"""
student_item_model, _ = StudentItem.objects.get_or_create( student_item_model, _ = StudentItem.objects.get_or_create(
student_id=student_item.student_id, student_id=student_item_struct.student_id,
course_id=student_item.course_id, course_id=student_item_struct.course_id,
item_id=student_item.item_id, item_id=student_item_struct.item_id,
item_type=student_item.item_type, item_type=student_item_struct.item_type,
) )
submitted_at = submitted_at if submitted_at else datetime.now()
submissions = Submission.objects.filter(student_item=student_item_model).order_by("-submitted_at")[:0]
if attempt_number is None: if attempt_number is None:
submissions = Submission.objects.filter(student_item=student_item_model)[:0]
attempt_number = submissions[0].attempt_number + 1 if submissions else 1 attempt_number = submissions[0].attempt_number + 1 if submissions else 1
submission = Submission.objects.create( model_kwargs = {
student_item=student_item_model, "student_item": student_item_model,
submitted_at=submitted_at, "answer": force_unicode(answer),
answer=answer, "attempt_number": attempt_number,
attempt_number=attempt_number, }
) if submitted_at:
model_kwargs["submitted_at"] = submitted_at
submission = Submission.objects.create(**model_kwargs)
return SubmissionStruct(**SubmissionSerializer(submission).data) return SubmissionStruct(**SubmissionSerializer(submission).data)
def get_submissions(student_item, limit=None): def get_submissions(student_item_struct, limit=None):
"""Retrieves the specified submission.
Returns the requested submission relative to the 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.
Returns:
List SubmissionStruct: A list of SubmissionStruct for the associated student item.
"""
student_item_model, _ = StudentItem.objects.get_or_create( student_item_model, _ = StudentItem.objects.get_or_create(
student_id=student_item.student_id, student_id=student_item_struct.student_id,
course_id=student_item.course_id, course_id=student_item_struct.course_id,
item_id=student_item.item_id, item_id=student_item_struct.item_id,
item_type=student_item.item_type, item_type=student_item_struct.item_type,
) )
submission_models = Submission.objects.filter(student_item=student_item_model) submission_models = Submission.objects.filter(student_item=student_item_model)
......
...@@ -22,6 +22,7 @@ from django.utils.timezone import now ...@@ -22,6 +22,7 @@ from django.utils.timezone import now
StudentItemStruct = namedtuple("StudentItemStruct", "student_id course_id item_id item_type") StudentItemStruct = namedtuple("StudentItemStruct", "student_id course_id item_id item_type")
class StudentItem(models.Model): class StudentItem(models.Model):
"""Represents a single item for a single course for a single user. """Represents a single item for a single course for a single user.
...@@ -73,6 +74,8 @@ class Submission(models.Model): ...@@ -73,6 +74,8 @@ class Submission(models.Model):
# The actual answer, assumed to be a JSON string # The actual answer, assumed to be a JSON string
answer = models.TextField(blank=True) answer = models.TextField(blank=True)
class Meta:
ordering = ["-submitted_at"]
class Score(models.Model): class Score(models.Model):
"""What the user scored for a given StudentItem. """What the user scored for a given StudentItem.
......
"""
Serializers are created to ensure models do not have to be accessed outside the scope of the Tim APIs.
"""
from rest_framework import serializers from rest_framework import serializers
from submissions.models import StudentItem, Submission, Score from submissions.models import StudentItem, Submission, Score
......
import datetime import datetime
from django.test import TestCase from django.test import TestCase
from submissions.models import StudentItem, StudentItemStruct from submissions.models import StudentItem, StudentItemStruct
from submissions.api import create_submission, get_submissions from submissions.api import create_submission, get_submissions
...@@ -25,8 +27,8 @@ class TestApi(TestCase): ...@@ -25,8 +27,8 @@ class TestApi(TestCase):
create_submission(STUDENT_ITEM, ANSWER_TWO) create_submission(STUDENT_ITEM, ANSWER_TWO)
submissions = get_submissions(STUDENT_ITEM) submissions = get_submissions(STUDENT_ITEM)
self._assert_submission(submissions[0],ANSWER_ONE, 1, 1) self._assert_submission(submissions[1], ANSWER_ONE, 1, 1)
self._assert_submission(submissions[1], ANSWER_TWO, 1, 1) self._assert_submission(submissions[0], ANSWER_TWO, 1, 1)
def test_get_latest_submission(self): def test_get_latest_submission(self):
past_date = datetime.date(2007, 11, 23) past_date = datetime.date(2007, 11, 23)
...@@ -42,8 +44,8 @@ class TestApi(TestCase): ...@@ -42,8 +44,8 @@ class TestApi(TestCase):
def test_set_attempt_number(self): def test_set_attempt_number(self):
create_submission(STUDENT_ITEM, ANSWER_ONE, None, 2) create_submission(STUDENT_ITEM, ANSWER_ONE, None, 2)
submission = get_submissions(STUDENT_ITEM) submissions = get_submissions(STUDENT_ITEM)
self._assert_submission(submission, 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)
......
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