diff --git a/app/api/tests/test_api.py b/app/api/tests/test_api.py index cd76d17a..0a77bb7c 100644 --- a/app/api/tests/test_api.py +++ b/app/api/tests/test_api.py @@ -1371,24 +1371,38 @@ class TestDownloader(APITestCase): expected_status=status.HTTP_400_BAD_REQUEST) -class TestStatisticsAPI(APITestCase): +class TestStatisticsAPI(APITestCase, TestUtilsMixin): @classmethod def setUpTestData(cls): cls.super_user_name = 'super_user_name' cls.super_user_pass = 'super_user_pass' + cls.other_user_name = 'other_user_name' + cls.other_user_pass = 'other_user_pass' create_default_roles() # Todo: change super_user to project_admin. super_user = User.objects.create_superuser(username=cls.super_user_name, password=cls.super_user_pass, email='fizz@buzz.com') - main_project = mommy.make('TextClassificationProject', users=[super_user]) - doc1 = mommy.make('Document', project=main_project) - mommy.make('Document', project=main_project) + other_user = User.objects.create_user(username=cls.other_user_name, + password=cls.other_user_pass, + email='bar@buzz.com') + + cls.project = mommy.make('TextClassificationProject', users=[super_user, other_user]) + doc1 = mommy.make('Document', project=cls.project) + doc2 = mommy.make('Document', project=cls.project) mommy.make('DocumentAnnotation', document=doc1, user=super_user) - cls.url = reverse(viewname='statistics', args=[main_project.id]) - cls.doc = Document.objects.filter(project=main_project) + mommy.make('DocumentAnnotation', document=doc2, user=other_user) + cls.url = reverse(viewname='statistics', args=[cls.project.id]) + cls.doc = Document.objects.filter(project=cls.project) + + assign_user_to_role(project_member=other_user, project=cls.project, + role_name=settings.ROLE_ANNOTATOR) + + @classmethod + def doCleanups(cls): + remove_all_role_mappings() def test_returns_exact_progress(self): self.client.login(username=self.super_user_name, @@ -1397,6 +1411,15 @@ class TestStatisticsAPI(APITestCase): self.assertEqual(response.data['total'], 2) self.assertEqual(response.data['remaining'], 1) + def test_returns_exact_progress_with_collaborative_annotation(self): + self._patch_project(self.project, 'collaborative_annotation', True) + + self.client.login(username=self.other_user_name, + password=self.other_user_pass) + response = self.client.get(self.url, format='json') + self.assertEqual(response.data['total'], 2) + self.assertEqual(response.data['remaining'], 0) + def test_returns_user_count(self): self.client.login(username=self.super_user_name, password=self.super_user_pass) diff --git a/app/api/views.py b/app/api/views.py index 4b31bcd8..c0f694d3 100644 --- a/app/api/views.py +++ b/app/api/views.py @@ -2,7 +2,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django_filters.rest_framework import DjangoFilterBackend -from django.db.models import Count, F +from django.db.models import Count, F, Q from libcloud.base import DriverType, get_driver from libcloud.storage.types import ContainerDoesNotExistError, ObjectDoesNotExistError from rest_framework import generics, filters, status @@ -90,9 +90,11 @@ class StatisticsAPI(APIView): docs = project.documents annotation_class = project.get_annotation_class() total = docs.count() - done = annotation_class.objects.filter(document_id__in=docs.all(), - user_id=self.request.user).\ - aggregate(Count('document', distinct=True))['document__count'] + annotation_filter = Q(document_id__in=docs.all()) + if not project.collaborative_annotation: + annotation_filter &= Q(user_id=self.request.user) + done = annotation_class.objects.filter(annotation_filter)\ + .aggregate(Count('document', distinct=True))['document__count'] remaining = total - done return {'total': total, 'remaining': remaining}