diff --git a/docs/pagination.rst b/docs/pagination.rst index 15cbfa1..8cf62c6 100644 --- a/docs/pagination.rst +++ b/docs/pagination.rst @@ -35,6 +35,7 @@ For our example, the server might respond like this (fields removed for brevity) } ], "next": 2, + "pages": 3, "prev": null } @@ -46,3 +47,6 @@ which help us navigate to other pages. Since we're on the first page, there is no previous page, which is why ``prev`` is empty. If we wished to go to the next page, we'd make the same request with the new page number. + +The ``pages`` field is the total number of pages there is in this query. +That is, page 3 is the last page in this example. diff --git a/sachet/server/views_common.py b/sachet/server/views_common.py index fb7dfff..165897e 100644 --- a/sachet/server/views_common.py +++ b/sachet/server/views_common.py @@ -264,6 +264,8 @@ class ModelListAPI(MethodView): Number of previous page (if this is not the first). next : int or None Number of next page (if this is not the last). + pages : int + Total number of pages. """ try: per_page = int(request.args.get("per_page", 15)) @@ -287,5 +289,6 @@ class ModelListAPI(MethodView): data=data, prev=page_data.prev_num, next=page_data.next_num, + pages=page_data.pages, ) ) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 1791e66..1af4ea9 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -54,6 +54,8 @@ def test_files(client, users, auth): data = resp.get_json().get("data") assert len(data) == per_page or len(data) == share_count % per_page + assert resp.get_json().get("pages") == ceil(share_count / per_page) + for share in data: share_id = share.get("share_id") assert share_id in shares @@ -137,6 +139,8 @@ def test_users(client, users, auth): data = resp.get_json().get("data") assert len(data) == per_page or len(data) == user_count % per_page + assert resp.get_json().get("pages") == ceil(user_count / per_page) + for user in data: username = user.get("username") assert username in total_users