2017-05-29 18:27:34 +03:00
|
|
|
import sqlalchemy
|
2017-05-12 20:51:49 +02:00
|
|
|
from flask_sqlalchemy import Pagination, BaseQuery
|
|
|
|
from flask import abort
|
|
|
|
|
2017-05-14 09:25:01 +03:00
|
|
|
|
2017-05-29 18:27:34 +03:00
|
|
|
def paginate_faste(self, page=1, per_page=50, max_page=None, step=5, count_query=None):
|
2017-05-12 20:51:49 +02:00
|
|
|
if page < 1:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if max_page and page > max_page:
|
|
|
|
abort(404)
|
|
|
|
|
2017-05-29 18:27:34 +03:00
|
|
|
# Count all items
|
|
|
|
if count_query is not None:
|
|
|
|
total_query_count = count_query.scalar()
|
|
|
|
else:
|
|
|
|
total_query_count = self.count()
|
|
|
|
|
|
|
|
# Grab items on current page
|
2017-05-12 20:51:49 +02:00
|
|
|
items = self.limit(per_page).offset((page - 1) * per_page).all()
|
|
|
|
|
|
|
|
if not items and page != 1:
|
|
|
|
abort(404)
|
|
|
|
|
2017-05-29 18:27:34 +03:00
|
|
|
return Pagination(self, page, per_page, total_query_count, items)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-14 09:25:01 +03:00
|
|
|
|
2017-05-12 20:51:49 +02:00
|
|
|
BaseQuery.paginate_faste = paginate_faste
|