Commit 718a1d39 by Eric Fischer Committed by GitHub

Merge pull request #50 from edx/efischer/annotation_serialization

Serialize annotation information with scores
parents d85fd0a0 d7500967
......@@ -33,7 +33,7 @@ def load_requirements(*requirements_paths):
setup(
name='edx-submissions',
version='1.1.2',
version='1.1.3',
author='edX',
description='An API for creating submissions and scores.',
url='http://github.com/edx/edx-submissions.git',
......
......@@ -6,7 +6,7 @@ import json
from rest_framework import serializers
from rest_framework.fields import Field, DateTimeField, IntegerField
from submissions.models import StudentItem, Submission, Score
from submissions.models import StudentItem, Submission, Score, ScoreAnnotation
class RawField(Field):
......@@ -85,11 +85,33 @@ class SubmissionSerializer(serializers.ModelSerializer):
)
class ScoreAnnotationSerializer(serializers.ModelSerializer):
class Meta:
model = ScoreAnnotation
fields = (
'creator',
'reason',
'annotation_type',
)
class ScoreSerializer(serializers.ModelSerializer):
# Ensure that the created_at datetime is not converted to a string.
created_at = DateTimeField(format=None, required=False)
annotations = serializers.SerializerMethodField()
def get_annotations(self, obj):
"""
Inspect ScoreAnnotations to attach all relevant annotations.
"""
annotations = ScoreAnnotation.objects.filter(score_id=obj.id)
return [
ScoreAnnotationSerializer(instance=annotation).data
for annotation in annotations
]
class Meta:
model = Score
fields = (
......@@ -101,4 +123,5 @@ class ScoreSerializer(serializers.ModelSerializer):
# Computed
'submission_uuid',
'annotations',
)
"""
Tests for submissions serializers.
"""
import ddt
from django.test import TestCase
from submissions.models import Score, StudentItem
from submissions.models import Score, ScoreAnnotation, StudentItem, Submission
from submissions.serializers import ScoreSerializer
@ddt.ddt
class ScoreSerializerTest(TestCase):
"""
Tests for the score serializer.
"""
def test_score_with_null_submission(self):
item = StudentItem.objects.create(
def setUp(self):
super(ScoreSerializerTest, self).setUp()
self.item = StudentItem.objects.create(
student_id="score_test_student",
course_id="score_test_course",
item_id="i4x://mycourse/special_presentation"
)
self.submission = Submission.objects.create(student_item=self.item, attempt_number=1)
self.score = Score.objects.create(
student_item=self.item,
submission=self.submission,
points_earned=2,
points_possible=6,
)
def test_score_with_null_submission(self):
# Create a score with a null submission
score = Score.objects.create(
student_item=item,
null_sub_score = Score.objects.create(
student_item=self.item,
submission=None,
points_earned=2,
points_possible=6
points_earned=3,
points_possible=8,
)
score_dict = ScoreSerializer(score).data
null_sub_score_dict = ScoreSerializer(null_sub_score).data
self.assertIs(null_sub_score_dict['submission_uuid'], None)
self.assertEqual(null_sub_score_dict['points_earned'], 3)
self.assertEqual(null_sub_score_dict['points_possible'], 8)
self.assertIs(score_dict['submission_uuid'], None)
self.assertEqual(score_dict['points_earned'], 2)
self.assertEqual(score_dict['points_possible'], 6)
@ddt.data(['test_annotation_1', 'test_annotation_2'], [])
def test_score_annotations(self, annotation_types):
"""
Ensure that annotation types are returned with serialized scores.
"""
annotation_kwargs = {
'creator': 'test_annotator',
'reason': 'tests for the test god'
}
for test_type in annotation_types:
ScoreAnnotation.objects.create(
score=self.score,
annotation_type=test_type,
**annotation_kwargs
)
score_dict = ScoreSerializer(self.score).data
self.assertEqual(
score_dict['annotations'],
[
{
'reason': annotation_kwargs['reason'],
'annotation_type': annotation_type,
'creator': annotation_kwargs['creator'],
}
for annotation_type in annotation_types
]
)
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