helper.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:bucket_api 作者: jokamjohn 项目源码 文件源码
def paginate_buckets(user_id, page, q, user):
    """
    Get a user by Id, then get hold of their buckets and also paginate the results.
    There is also an option to search for a bucket name if the query param is set.
    Generate previous and next pagination urls
    :param q: Query parameter
    :param user_id: User Id
    :param user: Current User
    :param page: Page number
    :return: Pagination next url, previous url and the user buckets.
    """
    if q:
        pagination = Bucket.query.filter(Bucket.name.like("%" + q.lower().strip() + "%")).filter_by(user_id=user_id) \
            .paginate(page=page, per_page=app.config['BUCKET_AND_ITEMS_PER_PAGE'], error_out=False)
    else:
        pagination = user.buckets.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('bucket.bucketlist', q=q, page=page - 1, _external=True)
        else:
            previous = url_for('bucket.bucketlist', page=page - 1, _external=True)
    nex = None
    if pagination.has_next:
        if q:
            nex = url_for('bucket.bucketlist', q=q, page=page + 1, _external=True)
        else:
            nex = url_for('bucket.bucketlist', page=page + 1, _external=True)
    items = pagination.items
    return items, nex, pagination, previous
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号