def get_paginated_items(bucket, bucket_id, page, q):
"""
Get the items from the bucket and then paginate the results.
Items can also be search when the query parameter is set.
Construct the previous and next urls.
:param q: Query parameter
:param bucket: Bucket
:param bucket_id: Bucket Id
:param page: Page number
:return:
"""
if q:
pagination = BucketItem.query.filter(BucketItem.name.like("%" + q.lower().strip() + "%")) \
.order_by(BucketItem.create_at.desc()) \
.filter_by(bucket_id=bucket_id) \
.paginate(page=page, per_page=app.config['BUCKET_AND_ITEMS_PER_PAGE'], error_out=False)
else:
pagination = bucket.items.order_by(BucketItem.create_at.desc()).paginate(page=page, per_page=app.config[
'BUCKET_AND_ITEMS_PER_PAGE'], error_out=False)
previous = None
if pagination.has_prev:
if q:
previous = url_for('items.get_items', q=q, bucket_id=bucket_id, page=page - 1, _external=True)
else:
previous = url_for('items.get_items', bucket_id=bucket_id, page=page - 1, _external=True)
nex = None
if pagination.has_next:
if q:
nex = url_for('items.get_items', q=q, bucket_id=bucket_id, page=page + 1, _external=True)
else:
nex = url_for('items.get_items', bucket_id=bucket_id, page=page + 1, _external=True)
return pagination.items, nex, pagination, previous
评论列表
文章目录