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.

66 lines
2.6 KiB

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