def search(): es = pyes.ES('127.0.0.1:9200') page_no = request.GET.get('page_no') query_string = request.GET.get('query') query = pyes.StringQuery(query_string) result = es.search(query=query, search_fields="_all") bill_list = [] session = models.DBSession() for res in result['hits']['hits']: id = res['_source']['id'] bill = session.query(models.BillRevision).get(id) if not bill: log.info('bill_revs record with %r not found.' % id) continue bill_list.append(bill) if bill_list: pages = utils.Pagination(settings.ITEM_PER_PAGE, settings.PAGE_DISPLAYED, len(bill_list), page_no) else: pages = None if pages: bill_list = bill_list[pages.first:pages.last] return {'bill': bill_list, 'query': query_string, 'pages': pages}
def score_list(): args = request.args # <-- receive parameters --> page = args.get('page') # <-- validate parameters and set fallback values --> page = page or 1 # equals 1 if None; else: stays the same try: page = int(page) except ValueError: page = 1 users = User.top_user_dicts(db) # <-- pagination --> per_page = 50 user_count = len(users) page_range = utils.page_range(total_count=user_count, per_page=per_page) page = (page if page in page_range else 1) pagination = utils.Pagination(parent=users, page=page, per_page=per_page, total_count=user_count) # <-- standard values--> kwargs = {key: value for key, value in args.items()} kwargs.update(page=page) # <-- creation of dynamic content --> pag_kwargs = dict(pagination=pagination, endpoint='score_list', prev='<', next='>', ellipses='...', size='lg', args=kwargs) table = ScoreTable(pagination.items) your_score = dict(score=current_user.get_score(db), rank=current_user.get_rank(db)) return render_template('scoreboard.html', table=table, your_score=your_score, pagination_kwargs=pag_kwargs, active='community')
def list_all(): page_no = request.GET.get('page_no') # conn = engine.connect() session = models.DBSession() base_bills = (session.query(models.BillRevision).order_by( models.BillRevision.year.desc())) ongoing_bill = (base_bills.filter( ~models.BillRevision.status.in_(["Accepted", "Withdrawn"])).all()) done_bill = (base_bills.filter( models.BillRevision.status.in_(["Accepted", "Withdrawn"])).all()) bills = (ongoing_bill + done_bill) pages = utils.Pagination(settings.ITEM_PER_PAGE, settings.PAGE_DISPLAYED, len(bills), page_no) bills = bills[pages.first:pages.last] return {'bills': bills, 'pages': pages}
def challenge_user(_id): if _id == current_user._id: flash('You can not even challenge yourself. Nice try.', 'error') return redirect(url_for('user_list')) args = request.args # <-- receive parameters --> sort_key = args.get('sort') sort_direction = args.get('direction') page = args.get('page') filter_option = args.get('foption') filter_term = args.get('fterm') # <-- validate parameters and set fallback values --> sort_key_possible = ('name', 'owner', 'rating') sort_key = sort_key if sort_key in sort_key_possible else 'rating' sort_direction_possible = ('desc', 'asc') sort_direction = (sort_direction if sort_direction in sort_direction_possible else 'desc') sort_direction_bool = sort_direction == 'desc' filter_option_possible = ('name', 'owner') filter_option = (filter_option if filter_option in filter_option_possible else 'name') # filter_term: filter only applied if non-empty string given (None case) # TODO: filter_term validation analogous to tags/user input sanitization page = page or 1 # equals 1 if None; else: stays the same try: page = int(page) except ValueError: page = 1 # <-- filter form --> form = ChallengeFilterForm() if form.validate_on_submit(): filter_option = form.option.data filter_term = form.term.data kwargs = {key: value for key, value in args.items()} kwargs.update(sort=sort_key, direction=sort_direction, foption=filter_option, fterm=filter_term, page=1) return redirect(url_for('challenge_user', _id=_id, **kwargs)) form.term.data = filter_term form.option.data = filter_option cardboxes = CardBox.fetch_all(db) # <-- filter process --> # checks for filter_option = 'name', 'owner' if term is part of string if filter_term: if filter_option == 'name' or filter_option == 'owner': cardboxes = [ box for box in cardboxes if filter_term.lower() in getattr(box, filter_option).lower() ] else: cardboxes = [ box for box in cardboxes if filter_term in getattr(box, filter_option) ] # <-- sort process --> def _sort_key_of(box): if sort_key == 'name': return box.name.lower() if sort_key == 'owner': return box.owner.lower() return getattr(box, sort_key) if cardboxes: cardboxes.sort(key=_sort_key_of, reverse=sort_direction_bool) # <-- pagination --> per_page = 50 cardbox_count = len(cardboxes) page_range = utils.page_range(total_count=cardbox_count, per_page=per_page) page = (page if page in page_range else 1) pagination = utils.Pagination(parent=cardboxes, page=page, per_page=per_page, total_count=cardbox_count) # <-- standard values--> kwargs = {key: value for key, value in args.items()} kwargs.update(sort=sort_key, direction=sort_direction, foption=filter_option, fterm=filter_term, page=page, _id=_id) # <-- creation of dynamic content --> pag_kwargs = dict(pagination=pagination, endpoint='challenge_user', prev='<', next='>', ellipses='...', size='lg', args=kwargs) def partner_id_producer(item): return _id wrapper = utils.TableItemWrapper(dict(partner_id=partner_id_producer)) table = ChooseBoxTable(wrapper(pagination.items), _id, sort_reverse=sort_direction_bool, sort_by=sort_key) return render_template('challenge.html', _id=_id, table=table, filter_form=form, pagination_kwargs=pag_kwargs, active='versus')
def user_list(): args = request.args # <-- receive parameters --> sort_key = args.get('sort') sort_direction = args.get('direction') page = args.get('page') filter_term = args.get('fterm') following = args.get('show') # <-- validate parameters and set fallback values --> sort_key_possible = ('username', 'score') sort_key = sort_key if sort_key in sort_key_possible else 'score' sort_direction_possible = ('desc', 'asc') sort_direction = (sort_direction if sort_direction in sort_direction_possible else 'desc') sort_direction_bool = sort_direction == 'desc' # filter_term: filter only applied if non-empty string given (None case) # TODO: filter_term validation analogous to tags/user input sanitization following_possible = ('following', 'all') following = following if following in following_possible else 'following' following_bool = following == 'following' page = page or 1 # equals 1 if None; else: stays the same try: page = int(page) except ValueError: page = 1 # <-- search form --> form = CommunityForm() if form.validate_on_submit(): filter_term = form.term.data kwargs = {key: value for key, value in args.items()} kwargs.update(sort=sort_key, direction=sort_direction, fterm=filter_term, show='all', page=1) return redirect(url_for('user_list', **kwargs)) form.term.data = filter_term # <-- distinction: followed users - all users --> if following_bool: if not current_user.following: return render_template('community.html', search_form=form, following_bool=following_bool, active='community', no_table=True) users = User.fetch_multiple(db, current_user.following) else: users = User.fetch_all(db) # <-- filter process --> if filter_term: users = [ user for user in users if filter_term.lower() in getattr(user, '_id').lower() ] # <-- create wrapper objects --> def follow_label_producer(item): return 'Unfollow' if current_user.is_following(item._id) else 'Follow' def score_value_producer(item): return item.raw.get_score(db) wrapper = utils.TableItemWrapper( dict(follow_label=follow_label_producer, score=score_value_producer)) users = wrapper(users) # <-- sort process --> def _sort_key_of(user): if sort_key == 'username': return user._id.lower() return getattr(user, sort_key) if users: users.sort(key=_sort_key_of, reverse=sort_direction_bool) # <-- pagination --> per_page = 50 user_count = len(users) page_range = utils.page_range(total_count=user_count, per_page=per_page) page = (page if page in page_range else 1) pagination = utils.Pagination(parent=users, page=page, per_page=per_page, total_count=user_count) # <-- standard values--> kwargs = {key: value for key, value in args.items()} kwargs.update(sort=sort_key, direction=sort_direction, fterm=filter_term, show=following, page=page) # <-- creation of dynamic content --> pag_kwargs = dict(pagination=pagination, endpoint='user_list', prev='<', next='>', ellipses='...', size='lg', args=kwargs) table = UserTable(pagination.items, sort_reverse=sort_direction_bool, sort_by=sort_key) return render_template('community.html', table=table, following_bool=following_bool, search_form=form, pagination_kwargs=pag_kwargs, active='community')
def huge_list(): args = request.args sort_key = args.get('sort') or 'rating' sort_direction = args.get('direction') page = int(args.get('page')) if args.get('page') else 1 if not sort_direction: sort_direction = True else: sort_direction = sort_direction == 'desc' filter_option = args.get('foption') filter_term = args.get('fterm') form = FilterForm() if form.validate_on_submit(): filter_option = form.option.data filter_term = form.term.data kwargs = {key: value for key, value in args.items()} kwargs.update(foption=filter_option, fterm=filter_term, page=1) return redirect(url_for('huge_list', **kwargs)) form.term.data = filter_term filter_option = filter_option or 'tags' form.option.data = filter_option def sort_key_of(box): if sort_key == 'name': return box.name.lower() if sort_key == 'owner': return box.owner.lower() return getattr(box, sort_key) cardboxes = CardBox.fetch_all(db) if filter_term: cardboxes = [ c for c in cardboxes if filter_term in getattr(c, filter_option) ] if sort_key and cardboxes and hasattr(cardboxes[0], sort_key): cardboxes.sort(key=sort_key_of, reverse=sort_direction) else: sort_key = None pagination = utils.Pagination(parent=cardboxes, page=page, per_page=50, total_count=len(cardboxes)) kwargs = {key: value for key, value in args.items()} kwargs.update(page=page, foption=filter_option, direction=sort_direction) pag_kwargs = dict(pagination=pagination, endpoint='huge_list', prev='<', next='>', ellipses='...', size='lg', args=kwargs) table = CardBoxTable(pagination.items, sort_reverse=sort_direction, sort_by=sort_key) return render_template('huge_list.html', table=table, filter_form=form, pagination_kwargs=pag_kwargs)