def test_paginate_plain_list(): with pytest.raises(NotFound): Pagination(iterable=range(1, 42), page=0, per_page=10) with pytest.raises(NotFound): Pagination(iterable=range(1, 42), page=6, per_page=10) paginator = Pagination(range(1, 42), 1, 10) _test_paginator(paginator)
def test_queryset_paginator(app, todo): Todo = todo for i in range(42): Todo(title="post: %s" % i).save() with pytest.raises(NotFound): Pagination(iterable=Todo.objects, page=0, per_page=10) with pytest.raises(NotFound): Pagination(iterable=Todo.objects, page=6, per_page=10) paginator = Pagination(Todo.objects, 1, 10) _test_paginator(paginator)
def test_paginate_plain_list(self): self.assertRaises(NotFound, Pagination, range(1, 42), 0, 10) self.assertRaises(NotFound, Pagination, range(1, 42), 6, 10) paginator = Pagination(range(1, 42), 1, 10) self._test_paginator(paginator)
def index_v2_page(doc_type): item_per_page = 50 page = request.args.get('p', 1) page = int(page) total = Doc.objects.filter(type=doc_type).count() total_page = math.ceil(total / item_per_page) paginator = Pagination(Doc.objects(type=doc_type).order_by('seq'), page, 50) docs = paginator.items docs_data = [] for doc in docs: item = doc.dump() item['sent_total'] = Sent.objects(doc=doc).count() item['progress'] = Annotation.objects(doc=doc, user=g.user, type='sentence').count() docs_data.append(item) pagination = { 'page': page, 'total_page': total_page, 'left': max(1, page - 5), 'right': min(page + 5, total_page), } return render_template('index.html', type=doc_type, docs=docs_data, g=g, pagination=pagination)
def question_tag(tag, page=1): # paginate = Questions.query.filter(Questions.tags.like('%'+tag+'%')).paginate(page, PER_PAGE) questions = Questions.objects(tags=tag) pagination = Pagination(questions, page=page, per_page=PER_PAGE) return render_template('index.html', questions=pagination.items, pagination=pagination)
def get_users(self, name, page, per_page): users = [] if not name: users = User.objects() else: users = User.objects(name__icontains=name) return Pagination(users, page, per_page).items
def question_search(keyword='', page=1): # keyword = request.form['key'] # keyword = request.form['key'] print(keyword) questions = Questions.objects(title__icontains=keyword) pagination = Pagination(questions, page=page, per_page=PER_PAGE) return render_template('index.html', questions=pagination.items, pagination=pagination)
def get_products_by_user_id(self, user_id, name, page, per_page): products = [] if not name: products = Product.objects(user_id=user_id) else: products = Product.objects(user_id=user_id, name__icontains=name) return Pagination(products, page, per_page).items
def home(): params = request.args.to_dict() # user pagination upage = int(params.get('user-page', 1)) users = Pagination(umodels.User.objects.all(), upage, 20) return render_template('admin/home.html', **{ 'users': users, 'resources': Resources })
def get_journals_paginated(title_query, is_public=True, query_filter="", order_by="title_slug", page=1, per_page=20): """ Retorna um objeto Pagination (flask-mongoengine) com a lista de periódicos filtrados pelo titulo (title_query) e pelo parametro ``is_public``, ordenado pelo campo indicado pelo parametro ``order_by``. Os parametros: - ``page`` indica o número da pagina; - ``per_page`` indica o tamanho da pagina. """ journals = get_journals(title_query, is_public, query_filter, order_by) return Pagination(journals, page, per_page)
def paginate_list(content_list, from_index=0, per_page=10): paginator = Pagination(content_list, int(from_index / per_page + 1), per_page) content_list = [ item.to_api() if isinstance(item, (ApiDocument, User)) else item for item in paginator.items ] return { 'total': paginator.total, 'from': (paginator.page - 1) * paginator.per_page, 'list': [item for item in content_list] }
def getAudios(contests_id, page): try: contest = Contest.objects(name=contests_id).first() if contest is None: contest = Contest.objects(id=contests_id).all() if contest and current_user.first().is_authenticated: is_authenticated = True audios = Audio.objects( contest_id=contest.first().id).order_by("-creation_date") list = Pagination(audios, page, per_page) except Audio.DoesNotExist: return render_template('consultarconcurso.html', contest=contest.first(), contests_id=contest.first().id, is_authenticated=is_authenticated) except AttributeError as err: is_authenticated = False try: audios = Audio.objects( contest_id=contest.first().id).order_by("-creation_date") list = Pagination(audios, page, per_page) except Audio.DoesNotExist: return render_template('consultarconcurso.html', contest=contest.first(), contests_id=contest.first().id, is_authenticated=is_authenticated) except Exception as e: return "Error al recuperar el concurso" if audios.count() == 0: return render_template('consultarconcurso.html', contest=contest.first(), contests_id=contest.first().id, is_authenticated=is_authenticated) else: return render_template('consultarconcurso.html', audios=list, contest=contest.first(), contests_id=contest.first().id, is_authenticated=is_authenticated)
def get(self, page_no): users = User.objects() # The pagination method returns non json pagination object users = Pagination(users, page=int(page_no), per_page=2) # this gives list of user object users = users.items # using marshmallow to serialize obj = SudoUser(many=True) users = obj.dump(users) # using dumps to convert to json object users = json.dumps(users) # print(users) return Response(users, mimetype="application/json", status=200)
def test_queryset_paginator(self): with self.app.test_request_context('/'): db = self.db class Post(db.Document): title = db.StringField(required=True, max_length=200) for i in range(42): Post(title="post: %s" % i).save() self.assertRaises(NotFound, Pagination, Post.objects, 0, 10) self.assertRaises(NotFound, Pagination, Post.objects, 6, 10) paginator = Pagination(Post.objects, 1, 10) self._test_paginator(paginator)
def search(country=None, mru=None, cru=None, hru=None, sru=None, farmer=None, **kwargs): """ search based on country and minimum resource unit available :param country: if set, search for capacity in the specified country, defaults to None :param country: str, optional :param mru: minimal memory resource unit, defaults to None :param mru: int, optional :param cru: minimal CPU resource unit, defaults to None :param cru: int, optional :param hru: minimal HDD resource unit, defaults to None :param hru: int, optional :param sru: minimal SSD resource unit defaults to None :param sru: int, optional :return: sequence of Capacity object matching the query :rtype: sequence """ query = {} if country: query['location__country'] = country if farmer: query['farmer'] = farmer if mru: query['total_resources__mru__gte'] = mru if cru: query['total_resources__cru__gte'] = cru if hru: query['total_resources__hru__gte'] = hru if sru: query['total_resources__sru__gte'] = sru nodes = Capacity.objects(**query) if kwargs.get('order'): nodes = nodes.order_by(kwargs.get('order')) page = kwargs.get('page') per_page = kwargs.get('per_page', 50) if page: return Pagination(nodes, page, per_page) return nodes
def paginate(results): '''Returns a dict with returned items of requested page from the model and necessary params of Pagination''' obj = {} page = int(request.args.get('page', 1)) per_page = int(request.args.get('per_page', 10)) paginator = Pagination(results, page, per_page) # TODO Return obj['items']=[] if page does not exist obj['items'] = [json.loads(item.to_json()) for item in paginator.items] obj['has_next'] = paginator.has_next obj['has_prev'] = paginator.has_prev obj['next_num'] = paginator.next_num obj['page'] = paginator.page obj['pages'] = paginator.pages obj['per_page'] = paginator.per_page obj['prev_num'] = paginator.prev_num obj['total'] = paginator.total return obj
def get_articles_by_date_range(begin_date, end_date, page=1, per_page=100): """ Retorna artigos criados ou atualizados durante o período entre start_date e end_date. """ articles = Article.objects(Q(updated__gte=begin_date) & Q(updated__lte=end_date)).order_by('pid') return Pagination(articles, page, per_page)
def get_orders_by_user_id(self, user_id, page, per_page): orders = Order.objects(user_id=user_id) return Pagination(orders, page, per_page).items
def dispatch_request(self): if request.method == 'POST': # create action action_name = request.form['action_name'] if action_name.startswith('custom_action__'): _, custom_action_method_name, custom_action_target = action_name.split( '__') custom_methods_defined = [ c_actions['method_name'] for c_actions in self.custom_actions ] if custom_action_method_name not in custom_methods_defined: flash( u'Ação inválida: %s. Nenhum registro foi alterado.' % custom_action_method_name, 'error') elif not hasattr(self, custom_action_method_name): flash( u'O método: %s não foi implementado, na ListView. Nenhum registro foi alterado.' % custom_action_method_name, 'error') else: if custom_action_target == 'selected': try: ids = self.get_selected_ids() if ids: concrete_method = getattr( self, custom_action_method_name) concrete_method(ids) else: flash( u'Seleção inválida de registros. Nenhum registro foi alterado.', 'error') except Exception as e: flash(u'ERRO: %s' % str(e), 'error') else: try: concrete_method = getattr( self, custom_action_method_name) concrete_method() except Exception as e: flash(u'ERRO: %s' % str(e), 'error') elif action_name in self._allowed_POST_action_names: if action_name == 'process_all': if self.can_process: try: self.do_process_all() except Exception as e: flash(u'ERRO: %s' % str(e), 'error') else: flash( u'Esta ação não esta habilitada. Nenhum registro foi alterado.', 'error') elif action_name == 'process_selected': if self.can_process: try: ids = self.get_selected_ids( self.convert_pk_to_uuid) if ids: self.do_process_selected(ids) else: flash( u'Seleção inválida de registros. Nenhum registro foi alterado.', 'error') except Exception as e: flash(u'ERRO: %s' % str(e), 'error') else: flash( u'Esta ação não esta habilitada. Nenhum registro foi alterado.', 'error') elif action_name == 'delete_all': if self.can_delete: try: self.do_delete_all() except Exception as e: flash(u'ERRO: %s' % str(e), 'error') else: flash( u'Esta ação não esta habilitada. Nenhum registro foi alterado.', 'error') elif action_name == 'delete_selected': if self.can_delete: try: ids = self.get_selected_ids( self.convert_pk_to_uuid) if ids: self.do_delete_selected(ids) else: flash(u'Seleção inválida de registros', 'error') except Exception as e: flash(u'ERRO: %s' % str(e), 'error') else: flash( u'Esta ação não esta habilitada. Nenhum registro foi alterado.', 'error') else: flash(u'Ação inválida: %s. Nenhum registro foi alterado.' % action_name) # listamos os registros page = request.args.get('page', 1, type=int) new_per_page = request.args.get('per_page', self.per_page, type=int) if self.per_page != new_per_page: if new_per_page in [10, 20, 50, 100, 500, 1000]: self.per_page = new_per_page objects = Pagination(self.get_objects(), page, self.per_page) context = { # stage: 'extract' || 'transform' || 'load' || 'opac' 'stage': self.stage, # filters: 'list_filters': self.list_filters, 'filter_string_options': self.filter_string_options, # actions: 'can_process': self.can_process, 'can_delete': self.can_delete, # meta: 'page_title': self.page_title, 'page_subtitle': self.page_subtitle, 'panel_title': self.panel_title, 'list_columns': self.list_columns, # objetos: 'objects': objects.items, # paginas: 'pager_range': self._pager_range(page, objects.pages), 'current_page': page, 'per_page': self.per_page, 'total_pages': objects.pages, 'total_records': objects.total, 'has_prev': objects.has_prev, 'prev_num': objects.prev_num, 'has_next': objects.has_next, 'next_num': objects.next_num, # custom actions: 'custom_actions': self.custom_actions } return self.render_template(context)
def query_paginate(self, page): cities = Pagination(self.objects, int(page), 10) return cities.items, cities.pages
def tasks(): scantypes = ['-sT', '-sT', '-sS', '-sA', '-sW', '-sM', '-sN', '-sF', '-sX', '-sU'] if request.method == 'GET': skip = int(request.args['skip']) limit = int(request.args['limit']) sort_by = request.args['sort_by'] sort_context = int(request.args['sort_context']) search = request.args['search'] if sort_context == -1: sort_by = '-' + sort_by query = Reports.objects(user_id=session['current_user']).all().order_by(sort_by) if search != '': query = Reports.objects(user_id=session['current_user']).filter(targets__contains=search).all().order_by( sort_by) _nmap_tasks = [] paginator = Pagination(query, skip / limit + 1, 10) _dbreports = paginator.items for _dbreport in _dbreports: _nmap_task = celery_pipe.AsyncResult(_dbreport['task_id']) _report = { 'id': _nmap_task.id, 'targets': _dbreport['targets'], 'options': _dbreport['options'], 'create_date': _dbreport['create_date'], 'status': _nmap_task.status, 'ready': 0 } if _nmap_task.result and 'done' in _nmap_task.result: _report.update({'progress': float(_nmap_task.result['done'])}) elif _nmap_task.result and 'report' in _nmap_task.result: _report.update({'progress': 100}) else: _report.update({'progress': 0}) if _nmap_task.status in READY_STATES: _report.update({'ready': 1}) _nmap_tasks.append(_report) response_content = { 'recordsTotal': Reports.objects.count(), 'recordsFiltered': paginator.total, 'data': _nmap_tasks } return jsonify(response_content), 200 elif request.method == 'POST': data = request.get_json() if data['targets'] == '': response_content = { 'code': '403', 'message': 'The targets is not correct!' } return jsonify(response_content), 200 scani = int(data.get('scanTechniques', 0)) if data.get('ports', '') != '': portlist = '-p ' + data.get('ports') else: portlist = '' noping = '-Pn' if data.get('noping', False) else '' osdetect = '-O' if data.get('osDetection', False) else '' bannerdetect = '-sV' if data.get('bannerDetection', False) else '' nse_script = '' if data.get('scripts', '') != '': nse_script = '--script={0}'.format(data.get('scripts')) options = '{0} {1} {2} {3} {4} {5}'.format(scantypes[scani], portlist, noping, osdetect, bannerdetect, nse_script) _celery_task = celery_nmap_scan.delay(targets=str(data['targets']), options=str(options)) report = Reports(user_id=session['current_user'], task_id=_celery_task.id, targets=data['targets'], options=options) report.save() response_content = { 'code': '200', 'message': 'Successful!' } return jsonify(response_content), 200