Commit 34ada8a2 by Tim Babych Committed by Oleg Marshev

support multiple ranges

parent 78eb4d29
...@@ -12,10 +12,7 @@ class Note(models.Model): ...@@ -12,10 +12,7 @@ class Note(models.Model):
usage_id = models.CharField(max_length=255) usage_id = models.CharField(max_length=255)
text = models.TextField(default="") text = models.TextField(default="")
quote = models.TextField(default="") quote = models.TextField(default="")
range_start = models.CharField(max_length=2048) ranges = models.TextField(default="")
range_start_offset = models.IntegerField()
range_end = models.CharField(max_length=2048)
range_end_offset = models.IntegerField()
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
...@@ -42,13 +39,10 @@ class Note(models.Model): ...@@ -42,13 +39,10 @@ class Note(models.Model):
raise ValidationError('Note must have a course_id and usage_id and user_id.') raise ValidationError('Note must have a course_id and usage_id and user_id.')
ranges = note.get('ranges') ranges = note.get('ranges')
if ranges is None or len(ranges) != 1: if ranges is None:
raise ValidationError('Note must contain exactly one range.') raise ValidationError('Note must contain at least one range.')
self.range_start = ranges[0]['start'] self.ranges = json.dumps(ranges)
self.range_start_offset = ranges[0]['startOffset']
self.range_end = ranges[0]['end']
self.range_end_offset = ranges[0]['endOffset']
def as_dict(self): def as_dict(self):
""" """
...@@ -64,12 +58,7 @@ class Note(models.Model): ...@@ -64,12 +58,7 @@ class Note(models.Model):
'usage_id': self.usage_id, 'usage_id': self.usage_id,
'text': self.text, 'text': self.text,
'quote': self.quote, 'quote': self.quote,
'ranges': [{ 'ranges': json.loads(self.ranges),
'start': self.range_start,
'startOffset': self.range_start_offset,
'end': self.range_end,
'endOffset': self.range_end_offset
}],
'created': created, 'created': created,
'updated': updated, 'updated': updated,
} }
......
...@@ -49,16 +49,6 @@ class NoteTest(TestCase): ...@@ -49,16 +49,6 @@ class NoteTest(TestCase):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
note.clean(payload) note.clean(payload)
def test_clean_many_ranges(self):
note = Note()
with self.assertRaises(ValidationError):
note.clean({
'text': 'foo',
'quote': 'bar',
'ranges': [{} for i in range(10)] # Too many ranges.
})
def test_save(self): def test_save(self):
note = Note() note = Note()
note.clean(self.note) note.clean(self.note)
......
...@@ -45,24 +45,6 @@ class BaseAnnotationViewTests(APITestCase): ...@@ -45,24 +45,6 @@ class BaseAnnotationViewTests(APITestCase):
], ],
} }
self.expected_note = {
# "created": "2014-11-26T00:00:00+00:00",
# "updated": "2014-11-26T00:00:00+00:00",
"user": TEST_USER,
"usage_id": "test-usage-id",
"course_id": "test-course-id",
"text": "test note text",
"quote": "test note quote",
"ranges": [
{
"start": "/p[1]",
"end": "/p[1]",
"startOffset": 0,
"endOffset": 10,
}
],
}
def tearDown(self): def tearDown(self):
for note_id in note_searcher.all().values_list('id'): for note_id in note_searcher.all().values_list('id'):
get_es().delete( get_es().delete(
...@@ -140,7 +122,7 @@ class AnnotationViewTests(BaseAnnotationViewTests): ...@@ -140,7 +122,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
del annotation['updated'] del annotation['updated']
del annotation['created'] del annotation['created']
self.assertEqual(annotation, self.expected_note) self.assertEqual(annotation, self.payload)
expected_location = '/api/v1/annotations/{0}'.format(response.data['id']) expected_location = '/api/v1/annotations/{0}'.format(response.data['id'])
self.assertTrue( self.assertTrue(
...@@ -201,11 +183,41 @@ class AnnotationViewTests(BaseAnnotationViewTests): ...@@ -201,11 +183,41 @@ class AnnotationViewTests(BaseAnnotationViewTests):
response = self.client.get(url, self.headers) response = self.client.get(url, self.headers)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.expected_note['id'] = note_id
annotation = response.data annotation = response.data
del annotation['id']
del annotation['updated']
del annotation['created']
self.assertEqual(annotation, self.payload)
def test_create_multirange(self):
"""
Create a note that has several ranges and read it
"""
note = self.payload.copy()
ranges = [{
"start": "/p[1]",
"end": "/p[1]",
"startOffset": 0,
"endOffset": 10,
}, {
"start": "/p[2]",
"end": "/p[2]",
"startOffset": 20,
"endOffset": 22,
}
]
note['ranges'] = ranges
note_id = self._create_annotation(**note)['id']
url = reverse('api:v1:annotations_detail', kwargs={'annotation_id': note_id})
response = self.client.get(url, self.headers)
self.assertEqual(response.status_code, status.HTTP_200_OK)
annotation = response.data
del annotation['id']
del annotation['updated'] del annotation['updated']
del annotation['created'] del annotation['created']
self.assertEqual(annotation, self.expected_note) self.assertEqual(annotation, note)
def test_read_notfound(self): def test_read_notfound(self):
""" """
......
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