diff --git a/pybossa/api/api_base.py b/pybossa/api/api_base.py index bb95a82024..94414b0a5e 100644 --- a/pybossa/api/api_base.py +++ b/pybossa/api/api_base.py @@ -198,6 +198,12 @@ def _add_hateoas_links(self, item): for tr in task_runs: obj['task_runs'].append(tr.dictize()) + stats = request.args.get('stats') + if stats: + if item.__class__.__name__ == 'Project': + stats = project_stats_repo.filter_by(project_id=item.id, limit=1) + obj['stats'] = stats[0].dictize() if stats else {} + links, link = self.hateoas.create_links(item) if links: obj['links'] = links @@ -229,7 +235,7 @@ def _filter_query(self, repo_info, limit, offset, orderby): for k in request.args.keys(): if k not in ['limit', 'offset', 'api_key', 'last_id', 'all', 'fulltextsearch', 'desc', 'orderby', 'related', - 'participated', 'full']: + 'participated', 'full', 'stats']: # Raise an error if the k arg is not a column if self.__class__ == Task and k == 'external_uid': pass diff --git a/pybossa/api/project.py b/pybossa/api/project.py index 87d7469990..b2f7d1d570 100644 --- a/pybossa/api/project.py +++ b/pybossa/api/project.py @@ -83,6 +83,7 @@ def _filter_private_data(self, data): public = Project().public_attributes() public.append('link') public.append('links') + public.append('stats') for key in tmp.keys(): if key not in public: del tmp[key] diff --git a/test/test_api/test_project_api.py b/test/test_api/test_project_api.py index f011f03af9..875f308455 100644 --- a/test/test_api/test_project_api.py +++ b/test/test_api/test_project_api.py @@ -100,6 +100,13 @@ def test_project_query(self): for key in keys: assert key not in data[0].keys() + # Stats + res = self.app.get("/api/project?limit=1&stats=True") + data = json.loads(res.data) + assert len(data) == 1, data + assert 'stats' in data[0].keys() + assert data[0]['stats']['overall_progress'] == 0 + # Keyset pagination url = "/api/project?limit=5&last_id=%s" % (projects[4].id) res = self.app.get(url) diff --git a/test/test_api/test_task_api.py b/test/test_api/test_task_api.py index d553330f66..16af508e3a 100644 --- a/test/test_api/test_task_api.py +++ b/test/test_api/test_task_api.py @@ -188,7 +188,7 @@ def test_task_query_participated_external_uid(self): ExternalUidTaskRunFactory.create(task=tasks[1]) ExternalUidTaskRunFactory.create(task=tasks[2]) - url = '/api/task?participated=1&all=1&external_uid=1xa' + url = '/api/task?participated=1&all=1&external_uid=1xa' res = self.app.get(url) data = json.loads(res.data) @@ -415,7 +415,6 @@ def test_task_query_without_params(self): err_msg = "It should get the last item first." assert data[0]['id'] == tasks[1]['id'], err_msg - # Related taskruns = TaskRunFactory.create_batch(8, project=project, task=t2) res = self.app.get('/api/task?id=' + str(t2.id) + '&related=True') @@ -426,6 +425,12 @@ def test_task_query_without_params(self): assert len(task['task_runs']) == len(taskruns), task assert task['result'] == None, task + # Stats + res = self.app.get("/api/task?limit=1&stats=True") + data = json.loads(res.data) + assert len(data) == 1, data + assert 'stats' not in data[0].keys() + @with_context def test_task_query_without_params_with_context(self): """ Test API Task query with context"""