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.

80 lines
3.1 KiB

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