sachet/server/views_common.py: pagination now has a total pages field

This commit is contained in:
dogeystamp 2023-06-09 19:45:28 -04:00
parent 25980be6a3
commit 404393859b
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
3 changed files with 11 additions and 0 deletions

View File

@ -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.

View File

@ -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,
)
)

View File

@ -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