-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IO-1746] Introduction of Pagination queries and changes to item_ids …
…endpoint (#707) * basic pagination * changes for meta pagination * paginated id query * WIP changes for pagination * pagination object [untested] * pagination objects completed * test fixes * sensible defaults + test changes * base pagination collects all test * meta pagination tests * tweaks to useage * removal of no longer needed exception * linting changes * reverting 'sensible' defaults * len changes
- Loading branch information
1 parent
7845410
commit d102e24
Showing
28 changed files
with
579 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
from math import floor | ||
|
||
from pydantic import NonNegativeInt, PositiveInt | ||
|
||
from darwin.future.core.types.common import QueryString | ||
from darwin.future.data_objects.pydantic_base import DefaultDarwin | ||
|
||
|
||
def must_be_positive(v: int) -> int: | ||
if v is not None and v < 0: | ||
raise ValueError("Value must be positive") | ||
return v | ||
|
||
|
||
class Page(DefaultDarwin): | ||
offset: NonNegativeInt = 0 | ||
size: PositiveInt = 500 | ||
|
||
def get_required_page(self, item_index: int) -> Page: | ||
""" | ||
Get the page that contains the item at the specified index | ||
Args: | ||
item_index (int): The index of the item | ||
Returns: | ||
Page: The page that contains the item | ||
""" | ||
assert self.size is not None | ||
required_offset = floor(item_index / self.size) * self.size | ||
return Page(offset=required_offset, size=self.size) | ||
|
||
def to_query_string(self) -> QueryString: | ||
""" | ||
Generate a query string from the page object, some fields are not included if they are None, | ||
and certain fields are renamed. Outgoing and incoming query strings are different and require | ||
dropping certain fields | ||
Returns: | ||
QueryString: Outgoing query string | ||
""" | ||
qs_dict = {"page[offset]": str(self.offset), "page[size]": str(self.size)} | ||
return QueryString(qs_dict) | ||
|
||
def increment(self) -> None: | ||
""" | ||
Increment the page offset by the page size | ||
""" | ||
self.offset += self.size |
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
Oops, something went wrong.