Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display total number of pages and results in the meta section of an API call #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/rest-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Now if we hit our project at ``/api/message/`` we should get something like the
"model": "message",
"next": "",
"page": 1,
"num_pages" 1,
"num_results": 2,
"previous": ""
},
"objects": [
Expand Down Expand Up @@ -144,6 +146,8 @@ If you access the ``User`` API endpoint, we quickly notice a problem:
"model": "user",
"next": "",
"page": 1,
"num_pages": 1,
"num_results": 2,
"previous": ""
},
"objects": [
Expand Down Expand Up @@ -387,6 +391,8 @@ This call will return only messages by the ``User`` with id=2:
"model": "message",
"next": "",
"page": 1,
"num_pages": 1,
"num_results": 1,
"previous": ""
},
"objects": [
Expand All @@ -412,6 +418,8 @@ Joins can be traversed using the django double-underscore notation:
"model": "message",
"next": "",
"page": 1,
"num_pages": 1,
"num_results": 2,
"previous": ""
},
"objects": [
Expand Down Expand Up @@ -443,6 +451,8 @@ It is also supported to use different comparison operators with the same double-
"model": "user",
"next": "",
"page": 1,
"num_pages": 1,
"num_results": 1,
"previous": ""
},
"objects": [{
Expand Down Expand Up @@ -482,13 +492,15 @@ can specify a ``limit`` in the querystring.
`/api/messages/?limit=2`

In the "meta" section of the response, URIs for the "next" and "previous" sets
of results are available:
of results, the total number of results, and total number of pages are available:

.. code-block:: javascript

meta: {
model: "message"
next: "/api/message/?limit=1&page=3"
page: 2
num_pages: 3,
num_results: 3,
previous: "/api/message/?limit=1&page=1"
}
2 changes: 2 additions & 0 deletions flask_peewee/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ def get_request_metadata(self, paginated_query):

return {
'model': self.get_api_name(),
'num_pages': paginated_query.get_pages(),
'num_results': paginated_query.get_count(),
'page': current_page,
'previous': previous,
'next': next,
Expand Down
14 changes: 10 additions & 4 deletions flask_peewee/tests/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ def test_filtering(self):
'model': 'note',
'previous': '',
'next': '',
'num_pages': 1,
'num_results': 10,
'page': 1,
})
self.assertAPINotes(resp_json, self.normal.note_set.order_by(Note.id))
Expand All @@ -579,6 +581,8 @@ def test_filtering(self):
'model': 'note',
'previous': '',
'next': '',
'num_pages': 1,
'num_results': 10,
'page': 1,
})
self.assertAPINotes(resp_json, self.admin.note_set.order_by(Note.id))
Expand All @@ -599,6 +603,8 @@ def test_filtering(self):
'model': 'note',
'previous': '',
'next': '',
'num_pages': 1,
'num_results': 20,
'page': 1,
})
self.assertAPINotes(resp_json, Note.filter(user__in=[self.admin, self.inactive]).order_by(Note.id))
Expand Down Expand Up @@ -676,7 +682,7 @@ def test_list_get(self):
resp_json = self.response_json(resp)

self.assertAPIResponse(resp_json, [])
self.assertAPIMeta(resp_json, {'model': 'note', 'next': '', 'page': 1, 'previous': ''})
self.assertAPIMeta(resp_json, {'model': 'note', 'next': '', 'page': 1, 'previous': '', 'num_pages': 0, 'num_results': 0})

self.create_notes()

Expand Down Expand Up @@ -816,7 +822,7 @@ def test_list_get(self):
resp_json = self.response_json(resp)

self.assertAPIResponse(resp_json, [])
self.assertAPIMeta(resp_json, {'model': 'message', 'next': '', 'page': 1, 'previous': ''})
self.assertAPIMeta(resp_json, {'model': 'message', 'next': '', 'page': 1, 'previous': '', 'num_pages': 0, 'num_results': 0})

self.create_messages()

Expand Down Expand Up @@ -955,7 +961,7 @@ def test_list_get(self):
resp_json = self.response_json(resp)

self.assertAPIResponse(resp_json, [])
self.assertAPIMeta(resp_json, {'model': 'user', 'next': '', 'page': 1, 'previous': ''})
self.assertAPIMeta(resp_json, {'model': 'user', 'next': '', 'page': 1, 'previous': '', 'num_pages': 0, 'num_results': 0})

self.create_users()

Expand Down Expand Up @@ -1128,7 +1134,7 @@ def test_list_get(self):
self.tm1,
self.tm2,
])
self.assertAPIMeta(resp_json, {'model': 'testmodel', 'next': '', 'page': 1, 'previous': ''})
self.assertAPIMeta(resp_json, {'model': 'testmodel', 'next': '', 'page': 1, 'previous': '', 'num_pages': 1, 'num_results': 2})

def test_auth_headers(self):
with self.flask_app.test_client() as c:
Expand Down
3 changes: 3 additions & 0 deletions flask_peewee/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def get_page(self):
def get_pages(self):
return int(math.ceil(float(self.query.count()) / self.paginate_by))

def get_count(self):
return self.query.count()

def get_list(self):
return self.query.paginate(self.get_page(), self.paginate_by)

Expand Down