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.

123 lines
4.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from functools import partial
  2. from django.core.exceptions import ValidationError
  3. from django.shortcuts import get_object_or_404
  4. from rest_framework import generics, status
  5. from rest_framework.permissions import IsAuthenticated
  6. from rest_framework.response import Response
  7. from projects.models import Project
  8. from labels.models import Category, Span, TextLabel, Relation
  9. from projects.permissions import IsProjectMember
  10. from .permissions import CanEditLabel
  11. from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, RelationSerializer
  12. class BaseListAPI(generics.ListCreateAPIView):
  13. label_class = None
  14. pagination_class = None
  15. permission_classes = [IsAuthenticated & IsProjectMember]
  16. swagger_schema = None
  17. @property
  18. def project(self):
  19. return get_object_or_404(Project, pk=self.kwargs["project_id"])
  20. def get_queryset(self):
  21. queryset = self.label_class.objects.filter(example=self.kwargs["example_id"])
  22. if not self.project.collaborative_annotation:
  23. queryset = queryset.filter(user=self.request.user)
  24. return queryset
  25. def create(self, request, *args, **kwargs):
  26. request.data["example"] = self.kwargs["example_id"]
  27. try:
  28. response = super().create(request, args, kwargs)
  29. except ValidationError as err:
  30. response = Response({"detail": err.messages}, status=status.HTTP_400_BAD_REQUEST)
  31. return response
  32. def perform_create(self, serializer):
  33. serializer.save(example_id=self.kwargs["example_id"], user=self.request.user)
  34. def delete(self, request, *args, **kwargs):
  35. queryset = self.get_queryset()
  36. queryset.all().delete()
  37. return Response(status=status.HTTP_204_NO_CONTENT)
  38. class BaseDetailAPI(generics.RetrieveUpdateDestroyAPIView):
  39. lookup_url_kwarg = "annotation_id"
  40. swagger_schema = None
  41. @property
  42. def project(self):
  43. return get_object_or_404(Project, pk=self.kwargs["project_id"])
  44. def get_permissions(self):
  45. if self.project.collaborative_annotation:
  46. self.permission_classes = [IsAuthenticated & IsProjectMember]
  47. else:
  48. self.permission_classes = [IsAuthenticated & IsProjectMember & partial(CanEditLabel, self.queryset)]
  49. return super().get_permissions()
  50. class CategoryListAPI(BaseListAPI):
  51. label_class = Category
  52. serializer_class = CategorySerializer
  53. def create(self, request, *args, **kwargs):
  54. if self.project.single_class_classification:
  55. self.get_queryset().delete()
  56. return super().create(request, args, kwargs)
  57. class CategoryDetailAPI(BaseDetailAPI):
  58. queryset = Category.objects.all()
  59. serializer_class = CategorySerializer
  60. class SpanListAPI(BaseListAPI):
  61. label_class = Span
  62. serializer_class = SpanSerializer
  63. class SpanDetailAPI(BaseDetailAPI):
  64. queryset = Span.objects.all()
  65. serializer_class = SpanSerializer
  66. class TextLabelListAPI(BaseListAPI):
  67. label_class = TextLabel
  68. serializer_class = TextLabelSerializer
  69. class TextLabelDetailAPI(BaseDetailAPI):
  70. queryset = TextLabel.objects.all()
  71. serializer_class = TextLabelSerializer
  72. class RelationList(generics.ListCreateAPIView):
  73. serializer_class = RelationSerializer
  74. pagination_class = None
  75. permission_classes = [IsAuthenticated & IsProjectMember]
  76. def get_queryset(self):
  77. project = get_object_or_404(Project, pk=self.kwargs["project_id"])
  78. return project.annotation_relations
  79. def perform_create(self, serializer):
  80. project = get_object_or_404(Project, pk=self.kwargs["project_id"])
  81. serializer.save(project=project)
  82. def delete(self, request, *args, **kwargs):
  83. delete_ids = request.data["ids"]
  84. Relation.objects.filter(pk__in=delete_ids).delete()
  85. return Response(status=status.HTTP_204_NO_CONTENT)
  86. class RelationDetail(generics.RetrieveUpdateDestroyAPIView):
  87. queryset = Relation.objects.all()
  88. serializer_class = RelationSerializer
  89. lookup_url_kwarg = "annotation_id"
  90. permission_classes = [IsAuthenticated & IsProjectMember]