Skip to content
Snippets Groups Projects
Verified Commit f6a827cd authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fixed some pagination issues

parent e9e60e76
Branches
Tags
No related merge requests found
......@@ -188,7 +188,7 @@ def get_url_param(url, name):
def get_pagination_data(payload):
data = {"current_page": 1, "next_page": None, "page_size": None}
data = {"next_page": None, "page_size": None}
if payload.get("next"):
next_page = get_url_param(payload["next"], "page")
data["next_page"] = int(next_page)
......@@ -197,14 +197,15 @@ def get_pagination_data(payload):
data["page_size"] = len(payload["results"])
if payload.get("previous"):
previous_page = get_url_param(payload["previous"], "page") or 1
data["current_page"] = int(previous_page) + 1
if not data.get("total_pages"):
data["total_pages"] = data["current_page"]
if not data["page_size"]:
previous_page = get_url_param(payload["previous"], "page") or 0
data.setdefault('current_page', int(previous_page) + 1)
data.setdefault('total_pages', data["current_page"])
if not data["page_size"] and payload["count"] - len(payload["results"]) > 0 and data["total_pages"] > 1:
data["page_size"] = int(payload["count"] - len(payload["results"])) / (
data["total_pages"] - 1
)
data.setdefault('current_page', 1)
data.setdefault('total_pages', 1)
return data
......@@ -243,12 +244,15 @@ def get_ls_command(group, endpoint, output_conf):
)
)
pagination_data = get_pagination_data(payload)
if pagination_data["page_size"]:
start = (
int(
(pagination_data["current_page"] - 1) * pagination_data["page_size"]
)
+ 1
)
else:
start = 1
end = min(start + len(payload["results"]) - 1, payload["count"])
click.echo(
"\nObjects {start}-{end} on {total} (page {current_page}/{total_pages})".format(
......@@ -256,7 +260,7 @@ def get_ls_command(group, endpoint, output_conf):
end=end,
total=payload["count"],
current_page=pagination_data["current_page"],
total_pages=pagination_data["total_pages"],
total_pages=pagination_data["total_pages"] or 1,
)
)
......@@ -320,6 +324,19 @@ async def libraries_create(ctx, raw, name, visibility):
)
@cli.group()
@click.pass_context
def artists(ctx):
pass
artists_ls = get_ls_command(
artists,
"api/v1/artists/",
output_conf={"labels": ["ID", "Name", "Created"], "type": "ARTIST"},
)
@cli.group()
@click.pass_context
def tracks(ctx):
......@@ -333,28 +350,6 @@ tracks_ls = get_ls_command(
)
@tracks.command("search")
@click.argument("query")
@click.option("--raw", is_flag=True)
@click.pass_context
@async_command
async def track_search(ctx, raw, query):
async with ctx.obj["remote"]:
result = await ctx.obj["remote"].request(
"get", "api/v1/tracks/", params={"q": query}
)
result.raise_for_status()
payload = await result.json()
if raw:
click.echo(json.dumps(payload, sort_keys=True, indent=4))
else:
click.echo(
output.table(
payload["results"], ["ID", "Title", "Artist", "Album"], type="TRACK"
)
)
async def get_track_download_url(id, remote):
result = await remote.request("get", "api/v1/tracks/{}/".format(id))
result.raise_for_status()
......
......@@ -16,13 +16,13 @@ FIELDS = {
"Mimetype": {"field": "mimetype"},
},
"LIBRARY": {
"Name": {"field": "name"},
"Visibility": {"field": "privacy_level"},
"Description": {"field": "description"},
"Uploads": {"field": "uploads_count"},
},
"*": {
"Created": {"field": "creation_date"},
"Name": {"field": "name"},
"ID": {"field": "id", "truncate": 0},
"UUID": {"field": "uuid", "truncate": 0},
},
......
......@@ -53,6 +53,10 @@ def test_delete_command(group, cli_ctx, session, responses):
{"previous": "http://test/?page=6", "count": 13, "results": [1]},
{"current_page": 7, "next_page": None, "total_pages": 7, "page_size": 2},
),
(
{"previous": "http://test/", "count": 4, "results": [1, 2, 3, 4]},
{"current_page": 1, "next_page": None, "total_pages": 1, "page_size": None},
),
],
)
def test_get_pagination_data(input, output):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment