def paginate_faste(self, page=1, per_page=50, max_page=None, step=5, count_query=None): if page < 1: abort(404) if max_page and page > max_page: abort(404) # 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 items = self.limit(per_page).offset((page - 1) * per_page).all() if not items and page != 1: abort(404) return Pagination(self, page, per_page, total_query_count, items)
def new_search(cls, query, page, order_by=None, per_page=PER_PAGE): """用于在搜索栏搜搜, query是输入的搜索内容, 字符串""" s = cls.search() # multi_match: 多个字段匹配, SEARCH_FIELDS设置了字段权重 s = s.query('multi_match', query=query, fields=SEARCH_FIELDS) if page < 1: page = 1 start = (page - 1) * PER_PAGE s = s.extra(**{'from': start, 'size': per_page}) if order_by is not None: s = s.sort(order_by) rs = s.execute() dct = defaultdict(list) # 这里考虑了多种kind的情况, 不过我们目前只会用到Post for i in rs: dct[i.kind].append(i.id) items = [] for kind, ids in dct.items(): target_cls = TARGET_MAPPER.get(kind) # 最终还是依靠orm来获取数据 if target_cls: items_ = target_cls.get_multi(ids) items.extend(items_) return Pagination(query, page, per_page, rs.hits.total, items)
def sessions_search_with_pagination(self, sessions_hint, typ, search_parameter, page=1, per_page=20, sort_type='mostRecent'): """ Search sessions with defined parameters and return them as a SearchResults object which contains a Pagination object. :param current_user The user who made the research :param sessions_hint: A hint gave by the user to search various sessions :param typ: str containing the type of sessions_hint (search filter) :param search_parameter: str containing the name of the search container :param current_page: The current page number :param per_page: The number of users shown on a search results page :return: A SearchResults with a Pagination object. """ results = self.sessions_search(sessions_hint, typ, search_parameter, sort_type) page_elements = results.items.slice( (page - 1) * per_page, page * per_page) # the sessions that will be displayed on the page results.pagination = Pagination(None, page, per_page, results.items.count(), page_elements) results.items = None return results
def new_search(cls, query, page, order_by=None, per_page=PER_PAGE): """ 创建一个查询实例,查询结果并分页返回,这里的query就是查询参数 """ s = cls.search() s = s.query('multi_match', query=query, fields=SEARCH_FIELDS) # 设置搜索规则,SEARCH_FIELDS为匹配的权重 if page < 1: page = 1 start = (page - 1) * PER_PAGE s = s.extra(**{'from': start, 'size': per_page}) if order_by is not None: s = s.sort(order_by) # 这里是rs是搜索的Response对象,结果存在hits中,可以迭代对象 rs = s.execute() dct = defaultdict(list) # 使用标准库来创建字典,可以实现get不存在的key的时候,返回空list for i in rs: dct[i.kind].append(i.id) # 分类数据,(由于当前只有一种类型数据,即post,不是很明显) items = [] for kind, ids in dct.items(): target_cls = TARGET_MAPPER.get(kind) # 通过TARGET_MAPPER决定要操作的模型对象,上面已经通过kind进行分类了 if target_cls: items_ = target_cls.get_multi(ids) items.extend(items_) return Pagination(query, page, per_page, rs.hits.total, items)
def games_search_with_pagination(self, games_hint, typ, search_parameter, page=1, per_page=20, sort_type='alphabetical'): """ Search games with defined parameters and return them as a SearchResults object which contains a Pagination object. :param current_user The user who made the research :param games_hint: A hint gave by the user to search various games :param typ: str containing the type of games_hint (search filter) :param search_parameter: str containing the name of the search container :param current_page: The current page number :param per_page: The number of users shown on a search results page :return: A SearchResults with a Pagination object. """ results = self.games_search(games_hint, typ, search_parameter, sort_type) # We want to remove already owned and wished games from the page if search_parameter is None or search_parameter == "None": results.items = results.items.filter( Game.id.notin_(self.get_wished_games(True))) results.items = results.items.filter( Game.id.notin_(self.get_owned_games(True))) page_elements = results.items.slice( (page - 1) * per_page, page * per_page) # the games that will be displayed on the page results.pagination = Pagination(None, page, per_page, results.items.count(), page_elements) results.items = None return results
def index(page=1, page_size=20): """优惠券列表""" g.page_title = _(u'优惠券') args = request.args tab_status = toint(args.get('tab_status', '0')) cb_name = args.get('cb_name', '').strip() current_time = current_timestamp() q = CouponBatch.query if tab_status == 1: q = q.filter(CouponBatch.is_valid == 1).\ filter(or_(CouponBatch.begin_time == 0, CouponBatch.begin_time <= current_time)).\ filter(or_(CouponBatch.end_time == 0, CouponBatch.end_time >= current_time)) elif tab_status == 2: q = q.filter( and_(CouponBatch.end_time > 0, CouponBatch.end_time < current_time)) if cb_name: q = q.filter(CouponBatch.cb_name.like('%%%s%%' % cb_name)) batches = q.order_by(CouponBatch.cb_id.desc()).offset( (page - 1) * page_size).limit(page_size).all() pagination = Pagination(None, page, page_size, q.count(), None) return render_template('admin/coupon/index.html.j2', pagination=pagination, batches=batches)
def get(self, time_range): if time_range not in ["week", "month"]: return error(ErrorCode.INVALID_PARAMS, 400) parser = reqparse.RequestParser() parser.add_argument("page", default=1, type=inputs.positive, location="args") parser.add_argument("per_page", default=20, type=inputs.positive, location="args") args = parser.parse_args() if time_range == "week": days = 7 else: days = 30 movie_ids, total = get_rank_movie_ids_with_range( days, args.page, args.per_page) movies = MovieModel.query.filter(MovieModel.id.in_(movie_ids)).all() pagination = Pagination("", args.page, args.per_page, total, movies) p = get_item_pagination(pagination, "api.LeaderBoard", time_range=time_range) return ok( "ok", data=marshal( p, get_pagination_resource_fields(movie_summary_resource_fields)), )
def statistics_event(): position = request.args.get('position') page = int(request.args.get('page', 1)) size = int(request.args.get('size', 20)) unique = int(request.args.get('unique', 0)) start_time, end_time = _parse_start_end_time() query = Event.query.filter(Event.company_id == g.user.company_id, Event.timestamp > start_time, Event.timestamp <= end_time) if position: screen = Screen.query.filter_by(camera_position=position).first() if screen is None: return error_result(ErrorCode.ERROR_SCREEN_NOT_EXIST) query = query.filter(Event.screen_id == screen.id) events = query.order_by(Event.timestamp.desc()).all() result = [ event.get_json() for event in _filter_events(events, unique=bool(unique)) ] page_info = page_format(Pagination(None, page, size, len(result), None)) result = result[(page - 1) * size:page * size] return success_result(result, page_info)
def users_search_with_pagination(self, username_hint, fav_only, hidden, current_page=1, per_page=20, sort_type='alphabetical'): """ Search users with defined parameters and return them as a SearchResults object which contains a Pagination object. :param current_user The user who made the research :param username_hint: A hint gave by the user to search similar usernames :param fav_only: containing "True" to show only bookmarked users else None :param hidden: containing "True" to show hidden users else None :param current_page: The current page number :param per_page: The number of users shown on a search results page :param sort_type: The sort type to use, using the ResultsSortType class constants :return: A SearchResults with a Pagination object. """ results = self.users_search(username_hint, fav_only, hidden, sort_type) page_elements = results.items.slice( (current_page - 1) * per_page, current_page * per_page) # the users that will be displayed on the page results.pagination = Pagination(None, current_page, per_page, results.items.count(), page_elements) results.items = None return results
def new_search(cls, query, page, order_by=None, per_page=PER_PAGE): s = cls.search() s = s.query('multi_match', query=query, fields=SEARCH_FIELDS) if page < 1: page = 1 start = (page - 1) * per_page s = s.extra(**{'from': start, 'size': per_page}) if order_by is not None: s = s.sort(order_by) rs = s.execute() dct = defaultdict(list) for i in rs: dct[i.kind].append( i.id ) # `Response` object allows you access to any key from the response dict via attribute access. items = [] for kind, ids in dct.items(): target_cls = TARGET_MAPPER.get(kind) if target_cls: items_ = target_cls.get_multi(ids) items.extend(items_) return Pagination(query, page, per_page, rs.hits.total, items)
def get_course_learners_with_results(self, course_id, instrument_ids=None, page=1, max_per_page=20): results = [] try: related_courses_ids = self._get_contained_course_ids(course_id) query = self.db.session.query(Request.tesla_id).filter( CourseActivity.course_id.in_(related_courses_ids), Request.activity_id == CourseActivity.activity_id, RequestResult.request_id == Request.id).group_by( Request.tesla_id) if instrument_ids is not None: query.filter(RequestResult.instrument_id.in_(instrument_ids)) results = query.group_by(Request.tesla_id).order_by( Request.tesla_id).paginate(page=page, max_per_page=max_per_page) except Exception: self.logger.exception( "Error getting list of learners with requests for a certain course" ) results = Pagination(None, page, max_per_page, 0, []) return results
def paginate(self, *args, **kwargs): pagination = super(QueryBooster, self).paginate(*args, **kwargs) distinct_total = self.order_by(None).distinct().count() items = self.distinct().limit(pagination.per_page).offset( (pagination.page - 1) * pagination.per_page).all() return Pagination(self, pagination.page, pagination.per_page, distinct_total, items)
def manage(): if request.method=='POST': pass path=urllib.unquote(request.args.get('path','/')) if path=='': path='/' if path!='/' and path.startswith('/'): path=re.sub('^/+','',path) page=request.args.get('page',1,type=int) image_mode=request.args.get('image_mode') sortby=request.args.get('sortby') order=request.args.get('order') if sortby: sortby=request.args.get('sortby') else: sortby=request.cookies.get('admin_sortby') if request.cookies.get('admin_sortby') is not None else 'lastModtime' sortby=sortby if order: order=request.args.get('order') else: order=request.cookies.get('admin_order') if request.cookies.get('admin_order') is not None else 'desc' order=order resp,total = FetchData(path=path,page=page,per_page=50,sortby=sortby,order=order) pagination=Pagination(query=None,page=page, per_page=50, total=total, items=None) resp=make_response(render_template('admin/manage.html',pagination=pagination,items=resp,path=path,sortby=sortby,order=order,endpoint='admin.manage')) resp.set_cookie('admin_sortby',str(sortby)) resp.set_cookie('admin_order',str(order)) return resp
def qualifications(page): if request.method == 'POST': if 'submit' in request.form: command = request.form['submit'].split('-')[0] id = request.form['submit'].split('-')[1] if command == 'edit': redirect(url_for('qualifications')) elif command == 'delete': item = Qualification.query.filter_by(id=id).first() db.session.delete(item) db.session.commit() query = Qualification.query.order_by(Qualification.name, Qualification.year).all count = len(query()) qualifications = utilities.get_items_for_page(query, page) table_header = [ 'ID', 'Name', 'Locale', 'Year', 'Number of Students', 'Actions' ] table_data = [[str(q.id), q.name, q.locale, q.year, q.num_students] for q in qualifications] if not qualifications and page != 1: abort(404) pagination = Pagination(query(), page, app.config['PER_PAGE'], count, qualifications) return render_template('data_display.html', user=current_user.name, user_role=current_user.role.name, table_header=table_header, table_data=table_data, pagination=pagination)
def paginate(self, page=1, per_page=25, show_all=False): """Paginate a query object. This behaves almost like the default `paginate` method from Flask-SQLAlchemy but allows showing all results on a single page. :param page: Number of the page to return. :param per_page: Number of items per page. :param show_all: Whether to show all the elements on one page. :return: a :class:`Pagination` object """ if page < 1 or show_all: page = 1 if show_all: items = self.all() per_page = total = len(items) else: items = self.limit(per_page).offset((page - 1) * per_page).all() if page == 1 and len(items) < per_page: total = len(items) else: total = self.order_by(None).count() return Pagination(self, page, per_page, total, items)
def optimised_pagination(query, per_page, page): ''' A more efficient pagination for SQLAlchemy Fetch one item before offset (to know if there's a previous page) Fetch one item after limit (to know if there's a next page) The trade-off is that the total items are not available, but if you don't need them there's no need for an extra COUNT query ''' offset_start = (page - 1) * per_page query_offset = max(offset_start - 1, 0) optimistic_items = query.limit(per_page + 1).offset(query_offset).all() if page == 1: if len(optimistic_items) == per_page + 1: # On first page, there's no optimistic item for previous page items = optimistic_items[:-1] else: # The number of items on the first page is fewer than per_page items = optimistic_items elif len(optimistic_items) == per_page + 2: # We fetched an extra item on both ends items = optimistic_items[1:-1] else: # An extra item only on the head # This is the last page items = optimistic_items[1:] # This total is at least the number of items for the query, could be more total = offset_start + len(optimistic_items) return Pagination(query, page, per_page, total, items)
def manage(): if request.method=='POST': pass path=urllib.unquote(request.args.get('path','A:/')) user,n_path=path.split(':') if n_path=='': path=':'.join([user,'/']) page=request.args.get('page',1,type=int) image_mode=request.args.get('image_mode') sortby=request.args.get('sortby') order=request.args.get('order') if sortby: sortby=request.args.get('sortby') else: sortby=request.cookies.get('admin_sortby') if request.cookies.get('admin_sortby') is not None else 'lastModtime' sortby=sortby if order: order=request.args.get('order') else: order=request.cookies.get('admin_order') if request.cookies.get('admin_order') is not None else 'desc' order=order resp,total = FetchData(path=path,page=page,per_page=50,sortby=sortby,order=order) pagination=Pagination(query=None,page=page, per_page=50, total=total, items=None) if path.split(':',1)[-1]=='/': path=':'.join([path.split(':',1)[0],'']) resp=make_response(render_template('admin/manage.html',pagination=pagination,items=resp,path=path,sortby=sortby,order=order,cur_user=user,endpoint='admin.manage')) resp.set_cookie('admin_sortby',str(sortby)) resp.set_cookie('admin_order',str(order)) return resp
def find(key_word): page = request.args.get('page', 1, type=int) image_mode = request.args.get('image_mode') sortby = request.args.get('sortby') order = request.args.get('order') action = request.args.get('action', 'download') resp, 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) resp = make_response( render_template('find.html', pagination=pagination, items=resp, 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
def admin_users(page): if request.method == 'POST': if 'submit' in request.form: command = request.form['submit'].split('-')[0] email = request.form['submit'].split('-')[1] if command == 'disable': User.query.filter_by(email=email).first().is_live = False db.session.commit() elif command == 'enable': User.query.filter_by(email=email).first().is_live = True db.session.commit() query = User.query.all count = len(query()) users = utilities.get_items_for_page(query, page) table_header = [ 'ID', 'Name', 'Email Address', 'Role', 'Is Live?', 'Enable/Disable' ] table_data = [[ str(user.id), user.name, user.email, user.role.name, user.is_live ] for user in users] if not users and page != 1: abort(404) pagination = Pagination(query(), page, app.config['PER_PAGE'], count, users) return render_template('admin_users.html', user=current_user.name, user_role=current_user.role.name, all_users_header=table_header, all_users_list=table_data, pagination=pagination)
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)
def paginate(self, page=None, per_page=None, error_out=True, max_per_page=None): if request: if page is None: try: page = int(request.args.get('page', 1)) except (TypeError, ValueError): if error_out: raise NotFound() page = 1 if per_page is None: try: per_page = int(request.args.get('per_page', 20)) except (TypeError, ValueError): if error_out: raise NotFound() per_page = 20 else: if page is None: page = 1 if per_page is None: per_page = 20 if max_per_page is not None: per_page = min(per_page, max_per_page) if page < 1: if error_out: raise NotFound(0) else: page = 1 if per_page < 0: if error_out: raise NotFound() else: per_page = 20 items = self.limit(per_page).offset((page - 1) * per_page).all() if not items and page != 1 and error_out: raise NotFound() # 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 = self.order_by(None).count() return Pagination(self, page, per_page, total, items)
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 = make_response( 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)) resp.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' resp.headers['Pragma'] = 'no-cache' resp.headers['Expires'] = '0' return resp
def shows_orm(page: int, filterby: str, mode: str, form: FlaskForm, search_term: str) -> dict: """ List all shows :param page: requested page of search results :param filterby: results filter; one of 'all', 'previous' or 'upcoming' :param mode: one of 'basic', 'advanced' or 'all' :param form: form data for advanced search :param search_term: search_term for basic search """ shows_list = [] pagination = Pagination(None, page, SHOWS_PER_PAGE, 0, shows_list) # advanced search on Venue & Artist, joining class clauses with 'and' and the result of those with 'or' # e.g. if have 'name' do same search on Venue & Artist and 'or' their results search = SearchParams([_ARTIST_, _VENUE_], conjunction=[OR_CONJUNC, AND_CONJUNC]).load_form(form) search.simple_search_term = search_term try: shows_list = Show.query\ .join(Venue, Show.venue_id == Venue.id) \ .join(Artist, Show.artist_id == Artist.id) \ .with_entities(Show.venue_id, Show.artist_id, Show.start_time, Venue.name, Artist.name, Artist.image_link) if filterby == FILTER_PREVIOUS: shows_list = shows_list.filter(Show.start_time < datetime.today()) elif filterby == FILTER_UPCOMING: shows_list = shows_list.filter(Show.start_time > datetime.today()) # get search terms and clauses for both Venue & Artist ncsg_search_clauses(mode, search) if len(search.clauses) > 0: shows_list = entity_search_clauses(shows_list, search, entity_search_expression) pagination = shows_list \ .order_by(Show.start_time) \ .paginate(page=page, per_page=SHOWS_PER_PAGE) shows_list = pagination.items except: print_exc_info() abort(HTTPStatus.INTERNAL_SERVER_ERROR.value) # [{'venue_id': ?, 'artist_id' ?, ...}, {}, ...] } data = [{ k: show[v] if k != 'start_time' else show[v].isoformat() for k, v in SHOWS_DICT.items() } for show in shows_list] return { "count": pagination.total, "data": data, "search_term": ', '.join(search.search_terms), "mode": mode, "pagination": pagination }
def get_search_by_keywords(): q = request.args.get('q', '', type=str) page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', RECOMMENDED_PER_PAGE_MIN, type=int), RECOMMENDED_PER_PAGE_MAX) movies_pagination = search_movie(q, per_page=per_page, page=page) if q != ' ' and q != '' \ else Pagination(None, 1, 0, 0, []) return jsonify(Movie.get_collection_dict(movies_pagination, 'api.get_search_by_keywords'))
def viewformals(page): formals = Formalities.query.order_by(-Formalities.id).all() page = int(page) #获取当前页面页数 total = int(len(formals)) formals = get_obj_for_page(page, POSTS_PER_PAGE, formals) pagination = Pagination('search', page, POSTS_PER_PAGE, total, formals) return render_template('project/viewfor.html', pagination=pagination, formals=formals)
def viewallot(page): allot = EquipmentAllot.query.order_by(-EquipmentAllot.id).all() page = int(page) #获取当前页面页数 total = int(len(allot)) allot = get_obj_for_page(page, POSTS_PER_PAGE, allot) pagination = Pagination('search', page, POSTS_PER_PAGE, total, allot) return render_template('equipment/viewallot.html', allot=allot, pagination=pagination)
def viewproject(page): project = Project.query.order_by(-Project.id).all() page = int(page) #获取当前页面页数 total = int(len(project)) project = get_obj_for_page(page, POSTS_PER_PAGE, project) pagination = Pagination('search', page, POSTS_PER_PAGE, total, project) return render_template('/project/viewproject.html', project=project, pagination=pagination)
def viewequipment(page): equipment = Equipment.query.order_by(-Equipment.id).all() page = int(page) #获取当前页面页数 total = int(len(equipment)) equipment = get_obj_for_page(page, POSTS_PER_PAGE, equipment) pagination = Pagination('search', page, POSTS_PER_PAGE, total, equipment) return render_template('equipment/viewequipments.html', equipment=equipment, pagination=pagination)
def paginate(query, page=None, per_page=None, error_out=True): """Clone from flask_sqlachemy.Pagination Returns `per_page` items from page `page`. By default it will abort with 404 if no items were found and the page was larger than 1. This behavor can be disabled by setting `error_out` to `False`. If page or per_page are None, they will be retrieved from the request query. If the values are not ints and ``error_out`` is true, it will abort with 404. If there is no request or they aren't in the query, they default to page 1 and 20 respectively. Returns an :class:`Pagination` object. """ if has_request_context(): if page is None: try: page = int(request.args.get('page', 1)) except (TypeError, ValueError): if error_out: abort(404) page = 1 if per_page is None: try: per_page = int(request.args.get('per_page', 20)) except (TypeError, ValueError): if error_out: abort(404) per_page = 20 else: if page is None: page = 1 if per_page is None: per_page = 20 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)
def viewcomsumable(page): page = int(page) #获取当前页面页数 comsumables = Comsumable.query.order_by(-Comsumable.id).all() total = int(len(comsumables)) comsumables = get_obj_for_page(page, POSTS_PER_PAGE, comsumables) pagination = Pagination('search', page, POSTS_PER_PAGE, total, comsumables) return render_template('comsumable/viewcom.html', pagination=pagination, comsumables=comsumables)