Skip to content
Snippets Groups Projects
Commit 03022b31 authored by Victor Löfgren's avatar Victor Löfgren
Browse files

Refactor paginate

parent b0bf59fb
No related branches found
No related tags found
1 merge request!161Resolve "replace-restx-with-smorest"
......@@ -50,34 +50,28 @@ class ExtendedQuery(BaseQuery):
return item
def paginate_api(self, pagination_parameters, order_column=None, order=1):
"""
When looking for lists of items this is used to only return a few of
them to allow for pagination.
:param page: Offset of the result
:type page: int
:param page_size: Amount of rows that will be retrieved from the query
:type page_size: int
:param order_column: Field of a DbModel in which the query shall order by
:type order_column: sqlalchemy.sql.schema.Column
:param order: If equals 1 then order by ascending otherwise order by descending
:type order: int
:return: A page/list of items with offset page*page_size and the total count of all rows ignoring page and page_size
:rtype: list, int
"""
if order_column:
self = self.order_by(order_column if order == 1 else order_column.desc())
# def pagination(self, page=0, page_size=15, order_column=None, order=1):
# """
# When looking for lists of items this is used to only return a few of
# them to allow for pagination.
# :param page: Offset of the result
# :type page: int
# :param page_size: Amount of rows that will be retrieved from the query
# :type page_size: int
# :param order_column: Field of a DbModel in which the query shall order by
# :type order_column: sqlalchemy.sql.schema.Column
# :param order: If equals 1 then order by ascending otherwise order by descending
# :type order: int
# :return: A page/list of items with offset page*page_size and the total count of all rows ignoring page and page_size
# :rtype: list, int
# """
# query = self
# if order_column:
# if order == 1:
# query = query.order_by(order_column)
# else:
# query = query.order_by(order_column.desc())
# total = query.count()
# query = query.limit(page_size).offset(page * page_size)
# items = query.all()
# return items, total
pagination = self.paginate(page=pagination_parameters.page, per_page=pagination_parameters.page_size)
pagination_parameters.item_count = pagination.total
return pagination.items
# class Dictionary(TypeDecorator):
......
......@@ -34,9 +34,7 @@ def user(
if role_id:
query = query.filter(User.role_id == role_id)
pagination = query.paginate(page=pagination_parameters.page, per_page=pagination_parameters.page_size)
pagination_parameters.item_count = pagination.total
return pagination.items
return query.paginate_api(pagination_parameters)
def competition(
......@@ -55,9 +53,7 @@ def competition(
if city_id:
query = query.filter(Competition.city_id == city_id)
pagination = query.paginate(page=pagination_parameters.page, per_page=pagination_parameters.page_size)
pagination_parameters.item_count = pagination.total
return pagination.items
return query.paginate_api(pagination_parameters)
def slide(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment