Skip to content

Commit 290e2ca

Browse files
committed
Make statistics respect collab flag
1 parent e5d0494 commit 290e2ca

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

app/api/tests/test_api.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -1371,24 +1371,38 @@ def test_can_download_plain_text(self):
13711371
expected_status=status.HTTP_400_BAD_REQUEST)
13721372

13731373

1374-
class TestStatisticsAPI(APITestCase):
1374+
class TestStatisticsAPI(APITestCase, TestUtilsMixin):
13751375

13761376
@classmethod
13771377
def setUpTestData(cls):
13781378
cls.super_user_name = 'super_user_name'
13791379
cls.super_user_pass = 'super_user_pass'
1380+
cls.other_user_name = 'other_user_name'
1381+
cls.other_user_pass = 'other_user_pass'
13801382
create_default_roles()
13811383
# Todo: change super_user to project_admin.
13821384
super_user = User.objects.create_superuser(username=cls.super_user_name,
13831385
password=cls.super_user_pass,
13841386
13851387

1386-
main_project = mommy.make('TextClassificationProject', users=[super_user])
1387-
doc1 = mommy.make('Document', project=main_project)
1388-
mommy.make('Document', project=main_project)
1388+
other_user = User.objects.create_user(username=cls.other_user_name,
1389+
password=cls.other_user_pass,
1390+
1391+
1392+
cls.project = mommy.make('TextClassificationProject', users=[super_user, other_user])
1393+
doc1 = mommy.make('Document', project=cls.project)
1394+
doc2 = mommy.make('Document', project=cls.project)
13891395
mommy.make('DocumentAnnotation', document=doc1, user=super_user)
1390-
cls.url = reverse(viewname='statistics', args=[main_project.id])
1391-
cls.doc = Document.objects.filter(project=main_project)
1396+
mommy.make('DocumentAnnotation', document=doc2, user=other_user)
1397+
cls.url = reverse(viewname='statistics', args=[cls.project.id])
1398+
cls.doc = Document.objects.filter(project=cls.project)
1399+
1400+
assign_user_to_role(project_member=other_user, project=cls.project,
1401+
role_name=settings.ROLE_ANNOTATOR)
1402+
1403+
@classmethod
1404+
def doCleanups(cls):
1405+
remove_all_role_mappings()
13921406

13931407
def test_returns_exact_progress(self):
13941408
self.client.login(username=self.super_user_name,
@@ -1397,6 +1411,15 @@ def test_returns_exact_progress(self):
13971411
self.assertEqual(response.data['total'], 2)
13981412
self.assertEqual(response.data['remaining'], 1)
13991413

1414+
def test_returns_exact_progress_with_collaborative_annotation(self):
1415+
self._patch_project(self.project, 'collaborative_annotation', True)
1416+
1417+
self.client.login(username=self.other_user_name,
1418+
password=self.other_user_pass)
1419+
response = self.client.get(self.url, format='json')
1420+
self.assertEqual(response.data['total'], 2)
1421+
self.assertEqual(response.data['remaining'], 0)
1422+
14001423
def test_returns_user_count(self):
14011424
self.client.login(username=self.super_user_name,
14021425
password=self.super_user_pass)

app/api/views.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.contrib.auth.models import User
33
from django.shortcuts import get_object_or_404, redirect
44
from django_filters.rest_framework import DjangoFilterBackend
5-
from django.db.models import Count, F
5+
from django.db.models import Count, F, Q
66
from libcloud.base import DriverType, get_driver
77
from libcloud.storage.types import ContainerDoesNotExistError, ObjectDoesNotExistError
88
from rest_framework import generics, filters, status
@@ -90,9 +90,11 @@ def progress(self, project):
9090
docs = project.documents
9191
annotation_class = project.get_annotation_class()
9292
total = docs.count()
93-
done = annotation_class.objects.filter(document_id__in=docs.all(),
94-
user_id=self.request.user).\
95-
aggregate(Count('document', distinct=True))['document__count']
93+
annotation_filter = Q(document_id__in=docs.all())
94+
if not project.collaborative_annotation:
95+
annotation_filter &= Q(user_id=self.request.user)
96+
done = annotation_class.objects.filter(annotation_filter)\
97+
.aggregate(Count('document', distinct=True))['document__count']
9698
remaining = total - done
9799
return {'total': total, 'remaining': remaining}
98100

0 commit comments

Comments
 (0)