Commit 4627cf17 by Oleg Marshev

List all from db.

parent 2b66897a
...@@ -445,9 +445,10 @@ class AnnotationViewTests(BaseAnnotationViewTests): ...@@ -445,9 +445,10 @@ class AnnotationViewTests(BaseAnnotationViewTests):
def test_read_all_no_annotations(self): def test_read_all_no_annotations(self):
""" """
Tests list all annotations endpoint when no annotations are present in elasticsearch. Tests list all annotations endpoint when no annotations are present in database.
""" """
url = reverse('api:v1:annotations') url = reverse('api:v1:annotations')
self.headers["course_id"] = "a/b/c"
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.assertEqual(len(response.data), 0, "no annotation should be returned in response") self.assertEqual(len(response.data), 0, "no annotation should be returned in response")
...@@ -457,10 +458,13 @@ class AnnotationViewTests(BaseAnnotationViewTests): ...@@ -457,10 +458,13 @@ class AnnotationViewTests(BaseAnnotationViewTests):
Tests list all annotations. Tests list all annotations.
""" """
for i in xrange(5): for i in xrange(5):
kwargs = {'text': 'Foo_{}'.format(i)} kwargs = {'text': 'Foo_{}'.format(i),
}
self._create_annotation(**kwargs) self._create_annotation(**kwargs)
url = reverse('api:v1:annotations') url = reverse('api:v1:annotations')
self.headers["course_id"] = "test-course-id"
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.assertEqual(len(response.data), 5, "five annotations should be returned in response") self.assertEqual(len(response.data), 5, "five annotations should be returned in response")
...@@ -505,6 +509,7 @@ class TokenTests(BaseAnnotationViewTests): ...@@ -505,6 +509,7 @@ class TokenTests(BaseAnnotationViewTests):
""" """
Ensure we can read list of annotations Ensure we can read list of annotations
""" """
self.headers["course_id"] = "test-course-id"
response = self.client.get(self.url, self.headers) response = self.client.get(self.url, self.headers)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
......
...@@ -57,11 +57,16 @@ class AnnotationListView(APIView): ...@@ -57,11 +57,16 @@ class AnnotationListView(APIView):
Get a list of all annotations. Get a list of all annotations.
""" """
params = self.request.QUERY_PARAMS.dict() params = self.request.QUERY_PARAMS.dict()
results = NoteMappingType.process_result(
list(note_searcher.filter(**params).values_dict("_source"))
)
return Response(results) if 'course_id' and 'user' not in params:
return Response(status=status.HTTP_400_BAD_REQUEST)
try:
results = Note.objects.filter(course_id=params['course_id'], user_id=params['user'])
except Note.DoesNotExist:
pass
return Response([result.as_dict() for result in results])
def post(self, *args, **kwargs): # pylint: disable=unused-argument def post(self, *args, **kwargs): # pylint: disable=unused-argument
""" """
...@@ -98,12 +103,13 @@ class AnnotationDetailView(APIView): ...@@ -98,12 +103,13 @@ class AnnotationDetailView(APIView):
Get an existing annotation. Get an existing annotation.
""" """
note_id = self.kwargs.get('annotation_id') note_id = self.kwargs.get('annotation_id')
if not note_searcher.filter(id=note_id).count():
return Response(False, status=status.HTTP_404_NOT_FOUND) try:
results = NoteMappingType.process_result( note = Note.objects.get(id=note_id)
list(note_searcher.filter(id=note_id).values_dict("_source")) except Note.DoesNotExist:
) return Response('Annotation not found!', status=status.HTTP_404_NOT_FOUND)
return Response(results[0])
return Response(note.as_dict())
def put(self, *args, **kwargs): # pylint: disable=unused-argument def put(self, *args, **kwargs): # pylint: disable=unused-argument
""" """
......
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