From 454eaf92c2e6ba098968ff70d16088e88b1ad4c9 Mon Sep 17 00:00:00 2001 From: Nikita Manovich <40690625+nmanovic@users.noreply.github.com> Date: Mon, 2 Dec 2019 14:40:15 +0300 Subject: [PATCH] page_size parameter for all REST API methods (#884) * Added page_size parameter for all REST API methods which returns list of objects. Also it is possible to specify page_size=all to return all elements. * Updated changelog.md --- CHANGELOG.md | 2 +- cvat/apps/engine/pagination.py | 22 ++++++++++++++++++++++ cvat/settings/base.py | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 cvat/apps/engine/pagination.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 07b6882a2715..2dc20d6e16f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ https://github.com/opencv/cvat/issues/750). - Text Detection Auto Annoation Script in OpenVINO format for version 4 ### Changed -- +- page_size parameter for all REST API methods ### Deprecated - diff --git a/cvat/apps/engine/pagination.py b/cvat/apps/engine/pagination.py new file mode 100644 index 000000000000..e18eeaf2ecd7 --- /dev/null +++ b/cvat/apps/engine/pagination.py @@ -0,0 +1,22 @@ +# Copyright (C) 2019 Intel Corporation +# +# SPDX-License-Identifier: MIT + +import sys +from rest_framework.pagination import PageNumberPagination + +class CustomPagination(PageNumberPagination): + page_size_query_param = "page_size" + + def get_page_size(self, request): + page_size = 0 + try: + value = request.query_params[self.page_size_query_param] + if value == "all": + page_size = sys.maxsize + else: + page_size = int(value) + except (KeyError, ValueError): + pass + + return page_size if page_size > 0 else self.page_size diff --git a/cvat/settings/base.py b/cvat/settings/base.py index d6356464f248..54d2e281aa56 100644 --- a/cvat/settings/base.py +++ b/cvat/settings/base.py @@ -137,7 +137,7 @@ def generate_ssh_keys(): # Need to add 'api-docs' here as a workaround for include_docs_urls. 'ALLOWED_VERSIONS': ('v1', 'api-docs'), 'DEFAULT_PAGINATION_CLASS': - 'rest_framework.pagination.PageNumberPagination', + 'cvat.apps.engine.pagination.CustomPagination', 'PAGE_SIZE': 10, 'DEFAULT_FILTER_BACKENDS': ( 'rest_framework.filters.SearchFilter',