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.

56 lines
2.2 KiB

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