Skip to content

Commit

Permalink
add ordering options to GET contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
jellegerbrandy committed Apr 26, 2016
1 parent 154f9b5 commit 10fb48b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
60 changes: 53 additions & 7 deletions restapi/tests/test_contributions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import types

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

Expand Down Expand Up @@ -37,12 +37,7 @@ def test_workflow(self):

# get the contribution collection
response = app.get(self.url_collection)
self.assertEqual(response.json.get('count'), 1)

# delete a contribution (this actually never happens)
# response = app.delete(url_resource(contribution_id))
# response = app.get(url_collection)
# self.assertEqual(response.json.get('count'), 0)
self.assertEqual(response.json['_meta']['total'], 1)

def test_data(self):
user = self.contract.create_user()
Expand Down Expand Up @@ -82,3 +77,54 @@ def test_data(self):
self.assertLess(data['stats']['engaged_reputation'], 1.0)
self.assertLess(data['stats']['evaluations']['0']['reputation'], 1.0)
self.assertLess(data['stats']['evaluations']['1']['reputation'], 1.0)

def test_collection_get(self):
# have some data to test with
contract = self.contract
user0 = contract.create_user()
user1 = contract.create_user()

contribution0 = contract.create_contribution(user=user0)
contribution1 = contract.create_contribution(user=user0)
contribution2 = contract.create_contribution(user=user1)
contribution0.time = datetime(2010, 1, 1)
contribution1.time = datetime(2011, 1, 1)
contribution2.time = datetime(2012, 1, 1)

# add some evaluations to determine the score
contract.create_evaluation(contribution=contribution1, user=user0, value=1)
contract.create_evaluation(contribution=contribution2, user=user0, value=1)
contract.create_evaluation(contribution=contribution2, user=user1, value=1)

app = self.app
url = self.url_collection

result = app.get(url).json
self.assertEqual(result['_meta']['total'], 3)
self.assertEqual(result['_meta']['start'], 0)
self.assertEqual(len(result['items']), 3)

result = app.get(url, {'limit': 1}).json
self.assertEqual(len(result['items']), 1)

result = app.get(url, {'start': 2}).json
self.assertEqual(result['_meta']['start'], 2)
self.assertEqual(len(result['items']), 1)

# check ordering - we have set up things such that
# the contribution2 has the highest score, and contribution0 the lowest
result = app.get(url).json
self.assertEqual(result['items'][0]['id'], contribution2.id)
self.assertEqual(result['items'][2]['id'], contribution0.id)

result = app.get(url, {'order_by': '-score'}).json
self.assertEqual(result['items'][0]['id'], contribution2.id)
self.assertEqual(result['items'][2]['id'], contribution0.id)

result = app.get(url, {'order_by': 'time'}).json
self.assertEqual(result['items'][0]['id'], contribution0.id)
self.assertEqual(result['items'][2]['id'], contribution2.id)

result = app.get(url, {'order_by': '-time'}).json
self.assertEqual(result['items'][0]['id'], contribution2.id)
self.assertEqual(result['items'][2]['id'], contribution0.id)
36 changes: 30 additions & 6 deletions restapi/views/contributions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cornice import Service
from colander import MappingSchema, SchemaNode, Integer, String

import config
from utils import get_contract
Expand All @@ -8,17 +9,40 @@
contribution_resource_service = Service(name='Contribution Resource', path=config.URL_CONTRIBUTION_RESOURCE, description="Contributions")


@contribution_collection_service.get(validators=(get_contract,))
class ContributionQuerySchema(MappingSchema):
contributor_id = SchemaNode(Integer(), location='querystring', type='int', missing=None)
order_by = SchemaNode(String(), location='querystring', type='str', missing='-score')
limit = SchemaNode(Integer(), location='querystring', type='int', missing=100)
start = SchemaNode(Integer(), location='querystring', type='int', missing=0)


@contribution_collection_service.get(validators=(get_contract,), schema=ContributionQuerySchema)
def collection_get(request):
"""Get a list of contributions"""
contributions = request.contract.get_contributions()
"""Get a list of contributions.
The parameter 'order_by' can take as its values:
- *score* order by score
- *-score* order descendingly, by score
- *time*: the time the contribution was added
- *-time*: last-added first
"""
contributions = request.contract.get_contributions(**request.validated)
return {
'count': len(contributions),
'_meta': {
'total': request.contract.contributions_count(),
'limit': request.validated['limit'],
'start': request.validated['start'],
},
'items': [contribution_to_dict(contribution, request) for contribution in contributions],
}


@contribution_collection_service.post(validators=(get_contract,))
class ContributionSchema(MappingSchema):
contributor_id = SchemaNode(Integer(), location='body', type='int')


@contribution_collection_service.post(validators=(get_contract,), schema=ContributionSchema)
def collection_post(request):
"""Create a new contribution
Expand All @@ -27,7 +51,7 @@ def collection_post(request):
:returns: information about the new contribution
"""
user_id = request.POST['contributor_id']
user_id = request.validated['contributor_id']
user = request.contract.get_user(user_id)
contribution = request.contract.create_contribution(user=user)
return contribution_to_dict(contribution, request)
Expand Down
8 changes: 4 additions & 4 deletions restapi/views/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
evaluation_resource_service = Service(name='Evaluation Resource', path=config.URL_EVALUATION_RESOURCE, description="Evaluations")


class EvaluationCollectionGetSchema(MappingSchema):
class EvaluationQuerySchema(MappingSchema):
contribution_id = SchemaNode(Integer(), location='querystring', type='int', missing=None)
evaluator_id = SchemaNode(Integer(), location='querystring', type='int', missing=None)


@evaluation_collection_service.get(validators=(get_contract,), schema=EvaluationCollectionGetSchema)
@evaluation_collection_service.get(validators=(get_contract,), schema=EvaluationQuerySchema)
def collection_get(request):
"""Get a list of users"""
evaluations = request.contract.get_evaluations(**request.validated)
Expand All @@ -23,13 +23,13 @@ def collection_get(request):
}


class Evaluationschema(MappingSchema):
class EvaluationSchema(MappingSchema):
value = SchemaNode(Float(), location='body', type='int')
evaluator_id = SchemaNode(Integer(), location='body', type='int')
contribution_id = SchemaNode(Integer(), location='body', type='int')


@evaluation_collection_service.post(validators=(get_contract,), schema=Evaluationschema)
@evaluation_collection_service.post(validators=(get_contract,), schema=EvaluationSchema)
def collection_post(request):
"""Create a new evaluation.
Expand Down

0 comments on commit 10fb48b

Please sign in to comment.