Skip to content

Commit

Permalink
Merge pull request #24 from rolepoint/feature/exta-args-in-pagination…
Browse files Browse the repository at this point in the history
…-response

Add page and size to response meta info for Pagination
  • Loading branch information
carlos-alberto committed May 11, 2016
2 parents 609e251 + dc7c50d commit 7b03c75
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 8 additions & 1 deletion flump/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def _make_get_many_response(self, entity_data, **kwargs):
"""
Returns a `schemas.ManyResponseData` with the links replaced with
those returned by `get_pagination_links`.
Also adds the `max_results` and `page` args to the meta.
"""
response = super()._make_get_many_response(entity_data, **kwargs)
return response._replace(links=self.get_pagination_links(**kwargs))
response = response._replace(links=self.get_pagination_links(**kwargs))
pagination_args = self.get_pagination_args()
meta = response.meta
meta['extra'] = {'size': pagination_args.size,
'page': pagination_args.page}
return response._replace(meta=meta)
4 changes: 4 additions & 0 deletions flump/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class LinkSchema(Schema):

class MetaSchema(Schema):
total_count = fields.Integer()
# This may contain extra data depending on the context. For instance
# the PageSizePagination mixin makes use of this field to include the
# current page and size in the response.
extra = fields.Dict()

class JsonApiResponseSchema(Schema):
data = fields.Nested(data_schema, many=many)
Expand Down
6 changes: 3 additions & 3 deletions test/methods/test_get_many.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_get_many(self, flask_client):

assert response.status_code == 200
assert response.json == {
'meta': {'total_count': 3},
'meta': {'total_count': 3, 'extra': {'size': 2, 'page': 1}},
'data': [
{
'attributes': {'name': 'Carl', 'age': 26},
Expand All @@ -100,7 +100,7 @@ def test_get_many(self, flask_client):

assert response.status_code == 200
assert response.json == {
'meta': {'total_count': 3},
'meta': {'total_count': 3, 'extra': {'size': 2, 'page': 2}},
'data': [
{
'attributes': {'name': 'Carl', 'age': 26},
Expand Down Expand Up @@ -129,7 +129,7 @@ def test_get_many_uses_query_string(self, flask_client):

assert response.status_code == 200
assert response.json == {
'meta': {'total_count': 10},
'meta': {'total_count': 10, 'extra': {'size': 3, 'page': 2}},
'data': [
{
'attributes': {'name': 'Carl', 'age': 26},
Expand Down

0 comments on commit 7b03c75

Please sign in to comment.