Last active
December 19, 2019 13:47
-
-
Save dkgndianko/f2a86009efcdccfcd3c4fd58f1349fbd to your computer and use it in GitHub Desktop.
A type for pydantic paginated elements in response
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
from pydantic import BaseModel | |
class _Pagination: | |
def __init__(self): | |
self.cache = [] | |
def __getitem__(self, klass): | |
target_class = None | |
key = str(klass) | |
try: | |
target_class = self.cache[key] | |
except KeyError: | |
class PaginatorSchema(BaseModel): | |
items: List[klass] | |
previous_page: int = None | |
current_page: int | |
next_page: int = None | |
per_page: int | |
total_pages: int | |
total_items: int | |
target_class = PaginatorSchema | |
self.cache[key] = target_class | |
return target_class | |
Pagination = _Pagination() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment