diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 4ba980c..f18bd8b 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -5193,7 +5193,8 @@ def set_project_owner(session, project, user, required_groups=None): session.add(project) -def get_pagination_metadata(flask_request, page, per_page, total): +def get_pagination_metadata( + flask_request, page, per_page, total, key_page='page'): """ Returns pagination metadata for an API. The code was inspired by Flask-SQLAlchemy. @@ -5201,6 +5202,7 @@ def get_pagination_metadata(flask_request, page, per_page, total): :param page: int of the current page :param per_page: int of results per page :param total: int of total results + :param key_page: the name of the argument corresponding to the page :return: dictionary of pagination metadata """ pages = int(ceil(total / float(per_page))) @@ -5208,7 +5210,7 @@ def get_pagination_metadata(flask_request, page, per_page, total): # Remove pagination related args because those are handled elsewhere # Also, remove any args that url_for accepts in case the user entered # those in - for key in ['page', 'per_page', 'endpoint']: + for key in [key_page, 'per_page', 'endpoint']: if key in request_args_wo_page: request_args_wo_page.pop(key) for key in flask_request.args: @@ -5219,26 +5221,30 @@ def get_pagination_metadata(flask_request, page, per_page, total): next_page = None if page < pages: + request_args_wo_page.update({key_page: page + 1}) next_page = url_for( - flask_request.endpoint, page=page + 1, per_page=per_page, + flask_request.endpoint, per_page=per_page, _external=True, **request_args_wo_page) prev_page = None if page > 1: + request_args_wo_page.update({key_page: page - 1}) prev_page = url_for( - flask_request.endpoint, page=page - 1, per_page=per_page, + flask_request.endpoint, per_page=per_page, _external=True, **request_args_wo_page) + request_args_wo_page.update({key_page: 1}) first_page = url_for( - flask_request.endpoint, page=1, per_page=per_page, _external=True, + flask_request.endpoint, per_page=per_page, _external=True, **request_args_wo_page) + request_args_wo_page.update({key_page: pages}) last_page = url_for( - flask_request.endpoint, page=pages, per_page=per_page, + flask_request.endpoint, per_page=per_page, _external=True, **request_args_wo_page) return { - 'page': page, + key_page: page, 'pages': pages, 'per_page': per_page, 'prev': prev_page,