-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into jenkins/upgrade-python-requirements-9c03ca5
- Loading branch information
Showing
6 changed files
with
388 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,8 +92,6 @@ private.py | |
|
||
docs/_build/ | ||
|
||
scripts/ | ||
|
||
.vscode/ | ||
|
||
.dev/ | ||
|
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,72 @@ | ||
""" | ||
Defines custom paginators used by subscription viewsets. | ||
""" | ||
from django.core.paginator import Paginator as DjangoPaginator | ||
from django.utils.functional import cached_property | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
|
||
class PageNumberPaginationWithCount(PageNumberPagination): | ||
""" | ||
A PageNumber paginator that adds the total number of pages to the paginated response. | ||
""" | ||
|
||
def get_paginated_response(self, data): | ||
""" Adds a ``num_pages`` field into the paginated response. """ | ||
response = super().get_paginated_response(data) | ||
response.data['num_pages'] = self.page.paginator.num_pages | ||
return response | ||
|
||
|
||
class LicensePagination(PageNumberPaginationWithCount): | ||
""" | ||
A PageNumber paginator that allows the client to specify the page size, up to some maximum. | ||
""" | ||
page_size_query_param = 'page_size' | ||
max_page_size = 500 | ||
|
||
|
||
class EstimatedCountDjangoPaginator(DjangoPaginator): | ||
""" | ||
A lazy paginator that determines it's count from | ||
the upstream `estimated_count` | ||
""" | ||
def __init__(self, *args, estimated_count=None, **kwargs): | ||
self.estimated_count = estimated_count | ||
super().__init__(*args, **kwargs) | ||
|
||
@cached_property | ||
def count(self): | ||
if self.estimated_count is None: | ||
return super().count | ||
return self.estimated_count | ||
|
||
|
||
class EstimatedCountLicensePagination(LicensePagination): | ||
""" | ||
Allows the caller (probably the `paginator()` property | ||
of an upstream Viewset) to provided an `estimated_count`, | ||
which means the downstream django paginator does *not* | ||
perform an additional query to get the count of the queryset. | ||
""" | ||
def __init__(self, *args, estimated_count=None, **kwargs): | ||
""" | ||
Optionally stores an `estimated_count` to pass along | ||
to `EstimatedCountDjangoPaginator`. | ||
""" | ||
self.estimated_count = estimated_count | ||
super().__init__(*args, **kwargs) | ||
|
||
def django_paginator_class(self, queryset, page_size): | ||
""" | ||
This only works because the implementation of `paginate_queryset` | ||
treats `self.django_paginator_class` as if it is simply a callable, | ||
and not necessarily a class, that returns a Django Paginator instance. | ||
It also (safely) relies on `self` having an instance variable called `estimated_count`. | ||
""" | ||
if self.estimated_count is not None: | ||
return EstimatedCountDjangoPaginator( | ||
queryset, page_size, estimated_count=self.estimated_count, | ||
) | ||
return DjangoPaginator(queryset, page_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
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.