You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.7 KiB

3 years ago
3 years ago
  1. from django.shortcuts import get_object_or_404
  2. from rest_framework import generics, status
  3. from rest_framework.exceptions import ValidationError
  4. from rest_framework.permissions import IsAuthenticated
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from ..models import Document, Project
  8. from ..permissions import (IsAnnotationApprover, IsInProjectOrAdmin,
  9. IsOwnAnnotation, IsProjectAdmin)
  10. from ..serializers import ApproverSerializer
  11. class AnnotationList(generics.ListCreateAPIView):
  12. pagination_class = None
  13. permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  14. swagger_schema = None
  15. def get_serializer_class(self):
  16. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  17. self.serializer_class = project.get_annotation_serializer()
  18. return self.serializer_class
  19. def get_queryset(self):
  20. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  21. model = project.get_annotation_class()
  22. queryset = model.objects.filter(document=self.kwargs['doc_id'])
  23. if not project.collaborative_annotation:
  24. queryset = queryset.filter(user=self.request.user)
  25. return queryset
  26. def create(self, request, *args, **kwargs):
  27. self.check_single_class_classification(self.kwargs['project_id'], self.kwargs['doc_id'], request.user)
  28. request.data['document'] = self.kwargs['doc_id']
  29. return super().create(request, args, kwargs)
  30. def perform_create(self, serializer):
  31. serializer.save(document_id=self.kwargs['doc_id'], user=self.request.user)
  32. def delete(self, request, *args, **kwargs):
  33. queryset = self.get_queryset()
  34. queryset.all().delete()
  35. return Response(status=status.HTTP_204_NO_CONTENT)
  36. @staticmethod
  37. def check_single_class_classification(project_id, doc_id, user):
  38. project = get_object_or_404(Project, pk=project_id)
  39. if not project.single_class_classification:
  40. return
  41. model = project.get_annotation_class()
  42. annotations = model.objects.filter(document_id=doc_id)
  43. if not project.collaborative_annotation:
  44. annotations = annotations.filter(user=user)
  45. if annotations.exists():
  46. raise ValidationError('requested to create duplicate annotation for single-class-classification project')
  47. class AnnotationDetail(generics.RetrieveUpdateDestroyAPIView):
  48. lookup_url_kwarg = 'annotation_id'
  49. swagger_schema = None
  50. def get_permissions(self):
  51. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  52. if project.collaborative_annotation:
  53. self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  54. else:
  55. self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin & IsOwnAnnotation]
  56. return super().get_permissions()
  57. def get_serializer_class(self):
  58. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  59. self.serializer_class = project.get_annotation_serializer()
  60. return self.serializer_class
  61. def get_queryset(self):
  62. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  63. model = project.get_annotation_class()
  64. self.queryset = model.objects.all()
  65. return self.queryset
  66. class ApproveLabelsAPI(APIView):
  67. permission_classes = [IsAuthenticated & (IsAnnotationApprover | IsProjectAdmin)]
  68. def post(self, request, *args, **kwargs):
  69. approved = self.request.data.get('approved', True)
  70. document = get_object_or_404(Document, pk=self.kwargs['doc_id'])
  71. document.annotations_approved_by = self.request.user if approved else None
  72. document.save()
  73. return Response(ApproverSerializer(document).data)