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.

152 lines
5.5 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, DocumentSerializer
  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. serializer_class = DocumentSerializer
  67. filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
  68. search_fields = ('text', )
  69. permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly)
  70. def get_queryset(self):
  71. queryset = self.queryset.filter(project=self.kwargs['project_id'])
  72. if not self.request.query_params.get('is_checked'):
  73. return queryset
  74. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  75. is_null = self.request.query_params.get('is_checked') == 'true'
  76. queryset = project.get_documents(is_null).distinct()
  77. return queryset
  78. class AnnotationList(generics.ListCreateAPIView):
  79. pagination_class = None
  80. permission_classes = (IsAuthenticated, IsProjectUser)
  81. def get_serializer_class(self):
  82. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  83. self.serializer_class = project.get_annotation_serializer()
  84. return self.serializer_class
  85. def get_queryset(self):
  86. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  87. document = project.documents.get(id=self.kwargs['doc_id'])
  88. self.queryset = document.get_annotations()
  89. self.queryset = self.queryset.filter(user=self.request.user)
  90. return self.queryset
  91. def perform_create(self, serializer):
  92. doc = get_object_or_404(Document, pk=self.kwargs['doc_id'])
  93. serializer.save(document=doc, user=self.request.user)
  94. class AnnotationDetail(generics.RetrieveUpdateDestroyAPIView):
  95. permission_classes = (IsAuthenticated, IsProjectUser, IsOwnAnnotation)
  96. def get_serializer_class(self):
  97. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  98. self.serializer_class = project.get_annotation_serializer()
  99. return self.serializer_class
  100. def get_queryset(self):
  101. document = get_object_or_404(Document, pk=self.kwargs['doc_id'])
  102. self.queryset = document.get_annotations()
  103. return self.queryset
  104. def get_object(self):
  105. queryset = self.filter_queryset(self.get_queryset())
  106. obj = get_object_or_404(queryset, pk=self.kwargs['annotation_id'])
  107. self.check_object_permissions(self.request, obj)
  108. return obj