-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASP-3966] Implement pagination (#430)
* Add pynput dependency * Add render paginated list results * Add keyboard listener to list_all applications * Add prototype 2: navigation using inquirer * Add pagination module * Add pagination to list all endpoints * Remove pynput dependency * ASP-3966 Move pagination module to subapps folder * ASP-3966 Add named params to pagination calls * ASP-3966 Add fixtures for pagination tests * ASP-3966 Update unit tests * ASP-3966 Update imports for handle_pagination * ASP-3966 Fix issue with one page results * ASP-3966 Sort imports * ASP-3966 Add pagination unit tests * ASP-3966 Lint code * ASP-3966 Fix merge conflict leftover * ASP-3966 Address code review requested changes * ASP-3966 Revert changes in poetry.lock * ASP-3966 Add validation when answer is None * ASP-3966 Add sorting by descending id as default * ASP-3966 Add entry to CHANGELOG
- Loading branch information
1 parent
e87dc39
commit f366dc1
Showing
12 changed files
with
389 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from typing import Any, Dict, List, Optional, cast | ||
|
||
import inquirer | ||
|
||
from jobbergate_cli.constants import PaginationChoices | ||
from jobbergate_cli.render import StyleMapper, render_paginated_list_results | ||
from jobbergate_cli.requests import make_request | ||
from jobbergate_cli.schemas import JobbergateContext, ListResponseEnvelope | ||
|
||
|
||
def handle_pagination( | ||
jg_ctx: JobbergateContext, | ||
url_path: str, | ||
abort_message: str = "There was an error communicating with the API", | ||
params: Optional[Dict[str, Any]] = None, | ||
title: str = "Results List", | ||
style_mapper: StyleMapper = None, | ||
hidden_fields: Optional[List[str]] = None, | ||
): | ||
assert jg_ctx is not None | ||
assert jg_ctx.client is not None | ||
|
||
current_page = 1 | ||
|
||
while True: | ||
if params is None: | ||
params = {} | ||
params["page"] = current_page | ||
|
||
envelope = cast( | ||
ListResponseEnvelope, | ||
make_request( | ||
jg_ctx.client, | ||
url_path, | ||
"GET", | ||
expected_status=200, | ||
abort_message=abort_message, | ||
support=True, | ||
response_model_cls=ListResponseEnvelope, | ||
params=params, | ||
), | ||
) | ||
|
||
render_paginated_list_results( | ||
jg_ctx, | ||
envelope, | ||
title=title, | ||
style_mapper=style_mapper, | ||
hidden_fields=hidden_fields, | ||
) | ||
|
||
if envelope.pages <= 1: | ||
return | ||
|
||
current_page = envelope.page | ||
|
||
message = "Which page would you like to view?" | ||
choices = [PaginationChoices.PREVIOUS_PAGE, PaginationChoices.NEXT_PAGE, PaginationChoices.EXIT] | ||
|
||
if current_page == 1: | ||
answer = inquirer.prompt( | ||
[ | ||
inquirer.List( | ||
"navigation", | ||
message=message, | ||
choices=choices[1:], # remove previous page option | ||
default=PaginationChoices.NEXT_PAGE, | ||
) | ||
] | ||
) | ||
elif current_page == envelope.pages: | ||
answer = inquirer.prompt( | ||
[ | ||
inquirer.List( | ||
"navigation", | ||
message=message, | ||
choices=choices[::2], # remove next page option | ||
default=PaginationChoices.EXIT, | ||
) | ||
] | ||
) | ||
else: | ||
answer = inquirer.prompt( | ||
[ | ||
inquirer.List( | ||
"navigation", | ||
message=message, | ||
choices=choices, | ||
default=PaginationChoices.NEXT_PAGE, | ||
) | ||
] | ||
) | ||
|
||
if not answer: | ||
return | ||
|
||
if answer["navigation"] == PaginationChoices.NEXT_PAGE: | ||
current_page += 1 | ||
elif answer["navigation"] == PaginationChoices.PREVIOUS_PAGE: | ||
current_page -= 1 | ||
else: | ||
return |
Oops, something went wrong.