Skip to content

Commit

Permalink
Merge pull request #1859 from alexandermendes/fix-1849
Browse files Browse the repository at this point in the history
Add stats param to project endpoints
  • Loading branch information
teleyinex committed Jun 5, 2018
2 parents 5860e14 + 983b6bb commit 9fd650b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pybossa/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pybossa/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions test/test_api/test_project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions test/test_api/test_task_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand All @@ -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"""
Expand Down

0 comments on commit 9fd650b

Please sign in to comment.