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.

55 lines
2.1 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
  7. from label_types.models import LabelType, CategoryType, SpanType
  8. from labels.models import Label, Category, Span
  9. from members.models import Member
  10. from members.permissions import IsInProjectReadOnlyOrAdmin
  11. class ProgressAPI(APIView):
  12. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  13. def get(self, request, *args, **kwargs):
  14. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  15. total = examples.count()
  16. done = ExampleState.objects.count_done(examples, user=self.request.user)
  17. return {'total': total, 'remaining': total - done}
  18. class MemberProgressAPI(APIView):
  19. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  20. def get(self, request, *args, **kwargs):
  21. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  22. members = Member.objects.filter(project=self.kwargs['project_id'])
  23. data = ExampleState.objects.measure_member_progress(examples, members)
  24. return Response(data=data, status=status.HTTP_200_OK)
  25. class LabelDistribution(abc.ABC, APIView):
  26. permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]
  27. model = Label
  28. label_type = LabelType
  29. def get(self, request, *args, **kwargs):
  30. labels = self.label_type.objects.filter(project=self.kwargs['project_id'])
  31. examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
  32. members = Member.objects.filter(project=self.kwargs['project_id'])
  33. data = self.model.objects.calc_label_distribution(examples, members, labels)
  34. return Response(data=data, status=status.HTTP_200_OK)
  35. class CategoryTypeDistribution(LabelDistribution):
  36. model = Category
  37. label_type = CategoryType
  38. class SpanTypeDistribution(LabelDistribution):
  39. model = Span
  40. label_type = SpanType