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.

81 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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 Example, Project
  7. from ..permissions import (IsAnnotationApprover, IsInProjectOrAdmin,
  8. IsOwnAnnotation, IsProjectAdmin)
  9. from ..serializers import ApproverSerializer, get_annotation_serializer
  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 = get_annotation_serializer(task=self.project.project_type)
  19. return self.serializer_class
  20. def get_queryset(self):
  21. model = self.project.get_annotation_class()
  22. queryset = model.objects.filter(example=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['example'] = self.kwargs['doc_id']
  30. return super().create(request, args, kwargs)
  31. def perform_create(self, serializer):
  32. serializer.save(example_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. @property
  41. def project(self):
  42. return get_object_or_404(Project, pk=self.kwargs['project_id'])
  43. def get_permissions(self):
  44. if self.project.collaborative_annotation:
  45. self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin]
  46. else:
  47. self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin & IsOwnAnnotation]
  48. return super().get_permissions()
  49. def get_serializer_class(self):
  50. self.serializer_class = get_annotation_serializer(task=self.project.project_type)
  51. return self.serializer_class
  52. def get_queryset(self):
  53. model = self.project.get_annotation_class()
  54. self.queryset = model.objects.all()
  55. return self.queryset
  56. class ApprovalAPI(APIView):
  57. permission_classes = [IsAuthenticated & (IsAnnotationApprover | IsProjectAdmin)]
  58. def post(self, request, *args, **kwargs):
  59. approved = self.request.data.get('approved', True)
  60. example = get_object_or_404(Example, pk=self.kwargs['data_id'])
  61. example.annotations_approved_by = self.request.user if approved else None
  62. example.save()
  63. return Response(ApproverSerializer(example).data)