Python flask_sqlalchemy.Pagination() Examples

The following are 5 code examples of flask_sqlalchemy.Pagination(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_sqlalchemy , or try the search function .
Example #1
Source File: base.py    From koschei with GNU General Public License v2.0 6 votes vote down vote up
def paginate(self, items_per_page):
        """
        Sets up pagination based on request query arguments.
        Raises 404 is the page is out-of-bounds.

        :return: Flask-SQLAlchemy's Pagination wrapper.
        """
        try:
            page = int(request.args.get('page', 1))
        except ValueError:
            abort(400)
        if page < 1:
            abort(404)
        items = self.limit(items_per_page)\
                    .offset((page - 1) * items_per_page).all()
        if not items and page != 1:
            abort(404)
        if page == 1 and len(items) < items_per_page:
            total = len(items)
        else:
            total = self.order_by(None).count()
        return Pagination(self, page, items_per_page, total, items)


# Thread-local database session 
Example #2
Source File: views.py    From PyOne with Mozilla Public License 2.0 5 votes vote down vote up
def find(key_word):
    page=request.args.get('page',1,type=int)
    ajax=request.args.get('ajax','no')
    image_mode=request.args.get('image_mode')
    sortby=request.args.get('sortby')
    order=request.args.get('order')
    action=request.args.get('action','download')
    data,total=FetchData(path=key_word,page=page,per_page=50,sortby=sortby,order=order,dismiss=True,search_mode=True)
    pagination=Pagination(query=None,page=page, per_page=50, total=total, items=None)
    if ajax=='yes':
        retdata={}
        retdata['code']=0
        retdata['msg']=""
        retdata['total']=total
        retdata['data']=[]
        for d in data:
            info={}
            if d['type']=='folder':
                info['name']='<a href="'+url_for('.index',path=d['path'])+'">'+d['name']+'</a>'
            else:
                info['name']='<a href="'+url_for('.index',path=d['path'],action='share')+'" target="_blank">'+d['name']+'</a>'
            info['type']=d['type']
            info['lastModtime']=d['lastModtime']
            info['size']=d['size']
            info['path']=d['path']
            info['id']=d['id']
            retdata['data'].append(info)
        return jsonify(retdata)
    resp=MakeResponse(render_template('theme/{}/find.html'.format(GetConfig('theme'))
                    ,pagination=pagination
                    ,items=data
                    ,path='/'
                    ,sortby=sortby
                    ,order=order
                    ,key_word=key_word
                    ,cur_user='搜索:"{}"'.format(key_word)
                    ,endpoint='.find'))
    resp.set_cookie('image_mode',str(image_mode))
    resp.set_cookie('sortby',str(sortby))
    resp.set_cookie('order',str(order))
    return resp 
Example #3
Source File: utilities.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def paginate(query, page, per_page=20, error_out=True):
	if error_out and page < 1:
		abort(404)
	items = query.limit(per_page).offset((page - 1) * per_page).all()
	if not items and page != 1 and error_out:
		abort(404)

	# No need to count if we're on the first page and there are fewer
	# items than we expected.
	if page == 1 and len(items) < per_page:
		total = len(items)
	else:
		total = query.order_by(None).count()

	return Pagination(query, page, per_page, total, items) 
Example #4
Source File: model.py    From beibq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def paginate(query, page, per_page=20, error_out=True):
    if error_out and page < 1:
        abort(404)
    items = query.limit(per_page).offset((page - 1) * per_page).all()
    if not items and page != 1 and error_out:
        abort(404)
    if page == 1 and len(items) < per_page:
        total = len(items)
    else:
        total = query.order_by(None).count()
    return Pagination(query, page, per_page, total, items) 
Example #5
Source File: search.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def new_search(cls, query, page, order_by=None, per_page=16):
        s = cls.search()
        s = s.query("multi_match", query=query, fields=SERACH_FIELDS)
        start = (page - 1) * per_page
        s = s.extra(**{"from": start, "size": per_page})
        s = s if order_by is None else s.sort(order_by)
        rs = s.execute()
        return Pagination(query, page, per_page, rs.hits.total, rs)