diff --git a/server/app/database/__init__.py b/server/app/database/__init__.py index b3b99a5ca1eaccffd4c4860504a3f719051c3aaa..a84f9082374bd5c3d2794817f9f8c1a270065c52 100644 --- a/server/app/database/__init__.py +++ b/server/app/database/__init__.py @@ -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): diff --git a/server/app/database/controller/search.py b/server/app/database/controller/search.py index 2ea1a811a00f44cf8c0e52a38eafb1c585568274..e6e486f8473f46932bdddd2506e4db75e0932f81 100644 --- a/server/app/database/controller/search.py +++ b/server/app/database/controller/search.py @@ -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(