def get_from_db(self, *args, **kwargs): # pylint: disable=unused-argument """ Search annotations in database. """ query = Note.objects.filter(**self.query_params).order_by('-updated') if 'text' in self.params: query = query.filter(Q(text__icontains=self.params['text']) | Q(tags__icontains=self.params['text'])) # Do not send paginated result if usage id based search. if self.search_with_usage_id: serializer = NoteSerializer(query, many=True) return Response(serializer.data, status=status.HTTP_200_OK) page = self.paginate_queryset(query) serializer = NoteSerializer(page, many=True) response = self.get_paginated_response(serializer.data) return response
def test_default_tags_value(self): """ Test that a note without explicit tags has an empty list for the tags value """ payload = self.note_dict.copy() payload.pop("tags") note = Note.create(payload) note.full_clean() note.save() self.assertEqual("[]", note.tags) self.assertEqual([], NoteSerializer(note).data["tags"])
def test_create_valid_note(self): note = Note.create(self.note_dict.copy()) note.save() serializer = NoteSerializer(note) result_note = serializer.data del result_note['id'] del result_note['created'] del result_note['updated'] self.assertEqual(result_note, self.note_dict)
def get(self, *args, **kwargs): # pylint: disable=unused-argument """ Get an existing annotation. """ note_id = self.kwargs.get('annotation_id') try: note = Note.objects.get(id=note_id) except Note.DoesNotExist: return Response('Annotation not found!', status=status.HTTP_404_NOT_FOUND) serializer = NoteSerializer(note) return Response(serializer.data)
def post(self, *args, **kwargs): # pylint: disable=unused-argument """ Create a new annotation. Returns 400 request if bad payload is sent or it was empty object. """ if not self.request.data or 'id' in self.request.data: return Response(status=status.HTTP_400_BAD_REQUEST) try: total_notes = Note.objects.filter( user_id=self.request.data['user'], course_id=self.request.data['course_id']).count() if total_notes >= settings.MAX_NOTES_PER_COURSE: raise AnnotationsLimitReachedError note = Note.create(self.request.data) note.full_clean() # Gather metrics for New Relic so we can slice data in New Relic Insights newrelic.agent.add_custom_parameter('notes.count', total_notes) except ValidationError as error: log.debug(error, exc_info=True) return Response(status=status.HTTP_400_BAD_REQUEST) except AnnotationsLimitReachedError: error_message = _( u'You can create up to {max_num_annotations_per_course} notes.' u' You must remove some notes before you can add new ones.' ).format( max_num_annotations_per_course=settings.MAX_NOTES_PER_COURSE) log.info(u'Attempted to create more than %s annotations', settings.MAX_NOTES_PER_COURSE) return Response({'error_msg': error_message}, status=status.HTTP_400_BAD_REQUEST) note.save() location = reverse('api:v1:annotations_detail', kwargs={'annotation_id': note.id}) serializer = NoteSerializer(note) return Response(serializer.data, status=status.HTTP_201_CREATED, headers={'Location': location})
def get_from_db(self, *args, **kwargs): # pylint: disable=unused-argument """ Search annotations in database. """ params = self.request.query_params.dict() query = Note.objects.filter(**{ f: v for (f, v) in params.items() if f in ('course_id', 'usage_id') }).order_by('-updated') if 'user' in params: query = query.filter(user_id=params['user']) if 'text' in params: query = query.filter( Q(text__icontains=params['text']) | Q(tags__icontains=params['text'])) page = self.paginate_queryset(query) serializer = NoteSerializer(page, many=True) response = self.get_paginated_response(serializer.data) return response
def put(self, *args, **kwargs): # pylint: disable=unused-argument """ Update an existing annotation. """ note_id = self.kwargs.get('annotation_id') try: note = Note.objects.get(id=note_id) except Note.DoesNotExist: return Response('Annotation not found! No update performed.', status=status.HTTP_404_NOT_FOUND) try: note.text = self.request.data['text'] note.tags = json.dumps(self.request.data['tags']) note.full_clean() except KeyError as error: log.debug(error, exc_info=True) return Response(status=status.HTTP_400_BAD_REQUEST) note.save() serializer = NoteSerializer(note) return Response(serializer.data)