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.0 KiB

3 years ago
3 years ago
3 years ago
  1. import abc
  2. from rest_framework import status
  3. from rest_framework.permissions import IsAuthenticated
  4. from rest_framework.response import Response
  5. from rest_framework.views import APIView
  6. from api.models import Example, ExampleState, Annotation, Label, Category, CategoryType, Span, SpanType
  7. from members.models import Member
  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. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  20. members = Member.objects.filter(project=self.kwargs['project_id'])
  21. data = ExampleState.objects.measure_member_progress(examples, members)
  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. labels = self.label_type.objects.filter(project=self.kwargs['project_id'])
  29. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  30. members = Member.objects.filter(project=self.kwargs['project_id'])
  31. data = self.model.objects.calc_label_distribution(examples, members, 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