Skip to content

Commit

Permalink
return statistics of a contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jellegerbrandy committed Apr 22, 2016
1 parent 97161d8 commit 1bfc4db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ WebOb==1.6.0
WebTest==2.0.21
zope.deprecation==4.1.2
zope.interface==4.1.3
colander==1.2
colander==1.2
bs4
2 changes: 1 addition & 1 deletion restapi/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def reset_database():


def get_contract(contract):
return backfeed_protocol.get_contract(contract)
return backfeed_protocol.utils.get_contract(contract)
17 changes: 16 additions & 1 deletion restapi/tests/test_contributions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import types

from common import APITestCase
from ..views.config import URL_CONTRIBUTION_RESOURCE, URL_CONTRIBUTION_COLLECTION

Expand Down Expand Up @@ -49,6 +51,8 @@ def test_data(self):
# check the data returned by POST request
response = self.app.post(self.url_collection, {'contributor_id': user.id})
data = response.json
contribution_id = data['id']
contribution = self.contract.get_contribution(contribution_id)

self.assertTrue(data['id'])
self.assertEqual(data['score'], 0.0)
Expand All @@ -57,7 +61,18 @@ def test_data(self):
self.assertEqual(data['contributor']['tokens'], 49.0)
self.assertEqual(data['contributor']['reputation'], 1.0)
self.assertEqual(data['type'], 'article')
self.assertEqual(data['stats']['evaluations'], {})

# they should be the same as those returned by the GET request
data_get = self.app.get(self.url_resource(data['id'])).json
data_get = self.app.get(self.url_resource(contribution_id)).json
self.assertEqual(data, data_get)

# now check if we get the right statistics
evaluator = self.contract.create_user()
self.contract.create_evaluation(contribution=contribution, user=evaluator, value=1)
evaluator = self.contract.create_user()
self.contract.create_evaluation(contribution=contribution, user=evaluator, value=0)

data = self.app.get(self.url_resource(contribution_id)).json
self.assertEqual(type(data['stats']['evaluations']['0']['reputation']), types.FloatType)
self.assertEqual(type(data['stats']['evaluations']['1']['reputation']), types.FloatType)
2 changes: 2 additions & 0 deletions restapi/views/contributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get(request):
def contribution_to_dict(contribution, request):
"""return a dictionary with information about this contribution"""
user = contribution.user
stats = contribution.get_statistics()
return {
'id': contribution.id,
'contributor': {
Expand All @@ -54,4 +55,5 @@ def contribution_to_dict(contribution, request):
'score': request.contract.contribution_score(contribution),
'engaged_reputation': contribution.engaged_reputation(),
'type': contribution.contribution_type,
'stats': stats,
}

0 comments on commit 1bfc4db

Please sign in to comment.