return_styles.py 文件源码

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

项目:airbnb_clone 作者: bennettbuchanan 项目源码 文件源码
def list(select, request):
        '''Returns a styled list from the data passed. The pagination style
        is defined in the request data. The default values of `number` and
        `page` will be 10 and 1, respectively. If these parameters are passed
        as data in the request, then the values will be updated accordingly.

        Keyword arguments:
        select -- A database query of data.
        request -- A request of some type.
        '''
        number = 10
        page = 1

        for key in request.values:
            if key == 'number':
                number = int(request.values.get('number'))
            elif key == 'page':
                page = int(request.values.get('page'))

        '''Call peewee paginate method on the query.'''
        arr = []
        for i in select.paginate(page, number):
            arr.append(i.to_dict())

        '''By default, `next_page_path` and `prev_page_path` are None.'''
        next_page_path = None
        prev_page_path = None
        base_path = request.base_url + "?page="
        end_path = "&number=" + str(number)

        '''Update `next_page_path` and `prev_page_path` if there is data on
        either a next or previous page from the pagination.
        '''
        if len(arr) == number:
            next_page_path = base_path + str(page + 1) + end_path
        if page > 1:
            prev_page_path = base_path + str(page - 1) + end_path

        '''Return an array of dicts, containing the data and pagination.'''
        data = [dict(data=arr)]
        data.append(dict(paging=dict(next=next_page_path,
                                     previous=prev_page_path)))
        return data
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号