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.

157 lines
5.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from collections import Counter
  2. from itertools import chain
  3. from django.shortcuts import get_object_or_404
  4. from django_filters.rest_framework import DjangoFilterBackend
  5. from rest_framework import viewsets, generics, filters
  6. from rest_framework.decorators import action
  7. from rest_framework.permissions import IsAuthenticated, IsAdminUser
  8. from rest_framework.response import Response
  9. from rest_framework.views import APIView
  10. from .models import Project, Label, Document
  11. from .permissions import IsAdminUserAndWriteOnly, IsProjectUser, IsOwnAnnotation
  12. from .serializers import ProjectSerializer, LabelSerializer
  13. class ProjectViewSet(viewsets.ModelViewSet):
  14. queryset = Project.objects.all()
  15. serializer_class = ProjectSerializer
  16. pagination_class = None
  17. permission_classes = (IsAuthenticated, IsAdminUserAndWriteOnly)
  18. def get_queryset(self):
  19. return self.request.user.projects
  20. @action(methods=['get'], detail=True)
  21. def progress(self, request, pk=None):
  22. project = self.get_object()
  23. return Response(project.get_progress(self.request.user))
  24. class LabelList(generics.ListCreateAPIView):
  25. queryset = Label.objects.all()
  26. serializer_class = LabelSerializer
  27. pagination_class = None
  28. permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly)
  29. def get_queryset(self):
  30. queryset = self.queryset.filter(project=self.kwargs['project_id'])
  31. return queryset
  32. def perform_create(self, serializer):
  33. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  34. serializer.save(project=project)
  35. class ProjectStatsAPI(APIView):
  36. pagination_class = None
  37. permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly)
  38. def get(self, request, *args, **kwargs):
  39. p = get_object_or_404(Project, pk=self.kwargs['project_id'])
  40. labels = [label.text for label in p.labels.all()]
  41. users = [user.username for user in p.users.all()]
  42. docs = [doc for doc in p.documents.all()]
  43. nested_labels = [[a.label.text for a in doc.get_annotations()] for doc in docs]
  44. nested_users = [[a.user.username for a in doc.get_annotations()] for doc in docs]
  45. label_count = Counter(chain(*nested_labels))
  46. label_data = [label_count[name] for name in labels]
  47. user_count = Counter(chain(*nested_users))
  48. user_data = [user_count[name] for name in users]
  49. response = {'label': {'labels': labels, 'data': label_data},
  50. 'user': {'users': users, 'data': user_data}}
  51. return Response(response)
  52. class LabelDetail(generics.RetrieveUpdateDestroyAPIView):
  53. queryset = Label.objects.all()
  54. serializer_class = LabelSerializer
  55. permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUser)
  56. def get_queryset(self):
  57. queryset = self.queryset.filter(project=self.kwargs['project_id'])
  58. return queryset
  59. def get_object(self):
  60. queryset = self.filter_queryset(self.get_queryset())
  61. obj = get_object_or_404(queryset, pk=self.kwargs['label_id'])
  62. self.check_object_permissions(self.request, obj)
  63. return obj
  64. class DocumentList(generics.ListCreateAPIView):
  65. queryset = Document.objects.all()
  66. filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
  67. search_fields = ('text', )
  68. permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly)
  69. def get_serializer_class(self):
  70. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  71. self.serializer_class = project.get_document_serializer()
  72. return self.serializer_class
  73. def get_queryset(self):
  74. queryset = self.queryset.filter(project=self.kwargs['project_id'])
  75. if not self.request.query_params.get('is_checked'):
  76. return queryset
  77. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  78. is_null = self.request.query_params.get('is_checked') == 'true'
  79. queryset = project.get_documents(is_null).distinct()
  80. return queryset
  81. class AnnotationList(generics.ListCreateAPIView):
  82. pagination_class = None
  83. permission_classes = (IsAuthenticated, IsProjectUser)
  84. def get_serializer_class(self):
  85. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  86. self.serializer_class = project.get_annotation_serializer()
  87. return self.serializer_class
  88. def get_queryset(self):
  89. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  90. document = project.documents.get(id=self.kwargs['doc_id'])
  91. self.queryset = document.get_annotations()
  92. self.queryset = self.queryset.filter(user=self.request.user)
  93. return self.queryset
  94. def perform_create(self, serializer):
  95. doc = get_object_or_404(Document, pk=self.kwargs['doc_id'])
  96. serializer.save(document=doc, user=self.request.user)
  97. class AnnotationDetail(generics.RetrieveUpdateDestroyAPIView):
  98. permission_classes = (IsAuthenticated, IsProjectUser, IsOwnAnnotation)
  99. def get_serializer_class(self):
  100. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  101. self.serializer_class = project.get_annotation_serializer()
  102. return self.serializer_class
  103. def get_queryset(self):
  104. document = get_object_or_404(Document, pk=self.kwargs['doc_id'])
  105. self.queryset = document.get_annotations()
  106. return self.queryset
  107. def get_object(self):
  108. queryset = self.filter_queryset(self.get_queryset())
  109. obj = get_object_or_404(queryset, pk=self.kwargs['annotation_id'])
  110. self.check_object_permissions(self.request, obj)
  111. return obj