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.

53 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
  1. import abc
  2. from django.shortcuts import get_object_or_404
  3. from rest_framework import status
  4. from rest_framework.permissions import IsAuthenticated
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from api.models import Example, ExampleState, Project, Annotation, Label, Category, CategoryType, Span, SpanType
  8. from members.permissions import IsInProjectReadOnlyOrAdmin
  9. class ProgressAPI(APIView):
  10. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  11. def get(self, request, *args, **kwargs):
  12. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  13. total = examples.count()
  14. done = ExampleState.objects.count_done(examples, user=self.request.user)
  15. return {'total': total, 'remaining': total - done}
  16. class MemberProgressAPI(APIView):
  17. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  18. def get(self, request, *args, **kwargs):
  19. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  20. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  21. data = ExampleState.objects.measure_member_progress(examples, project.users.all())
  22. return Response(data=data, status=status.HTTP_200_OK)
  23. class LabelDistribution(abc.ABC, APIView):
  24. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  25. model = Annotation
  26. label_type = Label
  27. def get(self, request, *args, **kwargs):
  28. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  29. labels = self.label_type.objects.filter(project=self.kwargs['project_id'])
  30. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  31. data = self.model.objects.calc_label_distribution(examples, project.users.all(), labels)
  32. return Response(data=data, status=status.HTTP_200_OK)
  33. class CategoryTypeDistribution(LabelDistribution):
  34. model = Category
  35. label_type = CategoryType
  36. class SpanTypeDistribution(LabelDistribution):
  37. model = Span
  38. label_type = SpanType