|
2 | 2 | Pagination serializers determine the structure of the output that should |
3 | 3 | be used for paginated responses. |
4 | 4 | """ |
| 5 | + |
| 6 | +import contextlib |
5 | 7 | from base64 import b64decode, b64encode |
6 | 8 | from collections import OrderedDict, namedtuple |
7 | 9 | from urllib import parse |
@@ -257,15 +259,12 @@ def get_paginated_response_schema(self, schema): |
257 | 259 |
|
258 | 260 | def get_page_size(self, request): |
259 | 261 | if self.page_size_query_param: |
260 | | - try: |
| 262 | + with contextlib.suppress(KeyError, ValueError): |
261 | 263 | return _positive_int( |
262 | 264 | request.query_params[self.page_size_query_param], |
263 | 265 | strict=True, |
264 | 266 | cutoff=self.max_page_size |
265 | 267 | ) |
266 | | - except (KeyError, ValueError): |
267 | | - pass |
268 | | - |
269 | 268 | return self.page_size |
270 | 269 |
|
271 | 270 | def get_next_link(self): |
@@ -430,15 +429,12 @@ def get_paginated_response_schema(self, schema): |
430 | 429 |
|
431 | 430 | def get_limit(self, request): |
432 | 431 | if self.limit_query_param: |
433 | | - try: |
| 432 | + with contextlib.suppress(KeyError, ValueError): |
434 | 433 | return _positive_int( |
435 | 434 | request.query_params[self.limit_query_param], |
436 | 435 | strict=True, |
437 | 436 | cutoff=self.max_limit |
438 | 437 | ) |
439 | | - except (KeyError, ValueError): |
440 | | - pass |
441 | | - |
442 | 438 | return self.default_limit |
443 | 439 |
|
444 | 440 | def get_offset(self, request): |
@@ -680,15 +676,12 @@ def paginate_queryset(self, queryset, request, view=None): |
680 | 676 |
|
681 | 677 | def get_page_size(self, request): |
682 | 678 | if self.page_size_query_param: |
683 | | - try: |
| 679 | + with contextlib.suppress(KeyError, ValueError): |
684 | 680 | return _positive_int( |
685 | 681 | request.query_params[self.page_size_query_param], |
686 | 682 | strict=True, |
687 | 683 | cutoff=self.max_page_size |
688 | 684 | ) |
689 | | - except (KeyError, ValueError): |
690 | | - pass |
691 | | - |
692 | 685 | return self.page_size |
693 | 686 |
|
694 | 687 | def get_next_link(self): |
|
0 commit comments