Exemplo n.º 1
0
def list_corpsums():
    query = CorpSum.select().order_by(CorpSum.id)
    pg = PaginatedQuery(query, paginate_by=10, page_var='page', check_bounds=True)
    page = pg.get_page()
    page_count = pg.get_page_count()
    corpsums = pg.get_object_list()
    return render_template('corpsum/list_corpsums.html', corpsums=corpsums, page=page, page_count=page_count)
Exemplo n.º 2
0
def posts(url):
    """Получить список постов для блога"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()
    has_access = Blog.has_access(blog, user)
    if not has_access:
        return errors.no_access()

    query = Post.get_posts_for_blog(blog)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, user) for p in posts]

    return jsonify(
        {
            "success": 1,
            "posts": posts,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Exemplo n.º 3
0
def attachment_search():
    """
    Search the index for attachments matching the given query.
    """
    phrase = request.args.get('q', '') or None
    ranking, _ = validate_ranking()
    ordering = request.args.getlist('ordering')
    filters = extract_metadata_filters()

    # Allow filtering by index.
    idx_list = request.args.getlist('index')
    if idx_list:
        indexes = Index.select(Index.id).where(Index.name << idx_list)
    else:
        indexes = None

    query = Attachment.search(phrase or '*',
                              indexes,
                              ranking if phrase else None,
                              ordering,
                              force_star_all=True if not phrase else False,
                              **filters)
    pq = PaginatedQuery(query.naive(),
                        paginate_by=app.config['PAGINATE_BY'],
                        page_var=app.config['PAGE_VAR'],
                        check_bounds=False)

    response = []
    for attachment in pq.get_object_list():
        data = {
            'document_id': attachment.document_id,
            'filename': attachment.filename,
            'hash': attachment.hash,
            'id': attachment.id,
            'identifier': attachment.identifier,
            'mimetype': attachment.mimetype,
            'timestamp': attachment.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
        }
        if phrase:
            data['score'] = attachment.score

        url_params = {
            'document_id': data['document_id'],
            'pk': data['filename']
        }
        if app.config['AUTHENTICATION']:
            url_params['key'] = app.config['AUTHENTICATION']
        data['data'] = url_for('attachment_download', **url_params)
        response.append(data)

    return jsonify({
        'attachment_count': Attachment.select().count(),
        'attachments': response,
        'filters': filters,
        'ordering': ordering,
        'page': pq.get_page(),
        'pages': pq.get_page_count(),
        'ranking': ranking,
        'search_term': phrase,
    })
Exemplo n.º 4
0
def search(index_name):
    """
    Search the index for documents matching the given query.
    """
    if not request.args.get('q'):
        error('Missing required search parameter "q".')

    search_query = request.args['q']
    ranking = request.args.get('ranking', Index.RANK_SIMPLE)
    if ranking and ranking not in (Index.RANK_SIMPLE, Index.RANK_BM25):
        error('Unrecognized "ranking" type.')

    filters = dict(
        (key, value) for key, value in request.args.items()
        if key not in ('page', 'q', 'key', 'ranking'))

    index = get_object_or_404(Index, Index.name == index_name)
    query = index.search(search_query, ranking, **filters)
    pq = PaginatedQuery(
        query,
        paginate_by=app.config['PAGINATE_BY'],
        page_var=app.config['PAGE_VAR'],
        check_bounds=False)

    return jsonify({
        'documents': _serialize_documents(
            pq.get_object_list(),
            include_score=ranking is not None),
        'page': pq.get_page(),
        'pages': pq.get_page_count()})
Exemplo n.º 5
0
def list_users():
    query = User.select().order_by(User.username)
    pg = PaginatedQuery(query, paginate_by=10, page_var='page', check_bounds=True)
    page = pg.get_page()
    page_count = pg.get_page_count()
    users = pg.get_object_list()
    return render_template('user/list_users.html', users=users, page=page, page_count=page_count)
Exemplo n.º 6
0
def category(input_category):
    """
    PaginatedQuery 会通过 GET 请求的 page 参数返回该页的内容
    """

    menu_url = quote(request.path.encode('utf-8'))
    contents = Content.select(Category, Content)\
                      .join(Category)\
                      .where(Category.name == input_category,
                             Content.status == 1)
    # 分页
    page_object = PaginatedQuery(contents,
                                 paginate_by=PAGE_MAX,
                                 check_bounds=True)
    page_count = page_object.get_page_count()
    current_page = page_object.get_page()
    projects = page_object.get_object_list()

    for project in projects:
        collected, collected_project_id = get_collected_project_status(
            project.project_url)
        if collected:
            project.collected = collected
            project.collected_project_id = collected_project_id

        project.description = markdown2html(project.description)
        project.image_url = make_image_url(project.image_path)
    return render_template('home/category.html',
                           projects=projects,
                           menu_url=menu_url,
                           page_title=input_category,
                           category_url=quote(input_category.encode('utf-8')),
                           page_count=page_count,
                           current_page=current_page)
Exemplo n.º 7
0
def index():
    page_no = request.args.get('page', type=int) or 1
    query_type = request.args.get('qt', type=str)
    query_param = request.args.get('qp', type=str) or ''
    file_type = file_type_mapping.get(request.args.get('ft', type=str),
                                      'docker_files')

    if not query_param:
        query_type = "default"

    file_items_query = query_type_mapping.get(query_type,
                                              query_for_default)(query_param,
                                                                 file_type)

    paginated_query = PaginatedQuery(file_items_query,
                                     paginate_by=PAGE_SIZE,
                                     page=page_no)

    paginated_query_iter = paginated_query.get_object_list()

    return render_template('pages/placeholder.main.html',
                           page_count=paginated_query.get_page_count(),
                           page=paginated_query.get_page(),
                           file_with_tags=paginated_query_iter,
                           qt=query_type,
                           qp=query_param,
                           ft=file_type)
Exemplo n.º 8
0
def search(index_name):
    """
    Search the index for documents matching the given query.
    """
    if not request.args.get("q"):
        error('Missing required search parameter "q".')

    search_query = request.args["q"]
    ranking = request.args.get("ranking", Index.RANK_SIMPLE)
    if ranking and ranking not in (Index.RANK_SIMPLE, Index.RANK_BM25):
        error('Unrecognized "ranking" type.')

    filters = dict((key, request.args.getlist(key)) for key in request.args if key not in _PROTECTED_KEYS)

    index = get_object_or_404(Index, Index.name == index_name)
    query = index.search(search_query, ranking, True, **filters)
    pq = PaginatedQuery(
        query, paginate_by=app.config["PAGINATE_BY"], page_var=app.config["PAGE_VAR"], check_bounds=False
    )

    return jsonify(
        {
            "documents": _serialize_documents(pq.get_object_list(), include_score=ranking is not None),
            "page": pq.get_page(),
            "pages": pq.get_page_count(),
        }
    )
Exemplo n.º 9
0
def adminses_get(chat_id=None, user_id=None, from_id=None):
    data = {"items": []}
    t = dict(**request.args)
    if "page" in t:
        del t["page"]
    query = Admin_List.filter(**t) if t else Admin_List.select()
    pg = PaginatedQuery(query, 20)
    for admin in convert(pg.get_object_list()):
        data["items"].append(admin)
    return jsonify(data)
Exemplo n.º 10
0
 def __init__(self, page_queryset: PaginatedQuery, schema: Schema):
     self.page = page_queryset.get_page()
     self.count = page_queryset.query.count()
     self.page_nums = page_queryset.get_page_count()
     self.data = schema.dump(obj=page_queryset.get_object_list(),
                             many=True).data
     self.result = OrderedDict([('count', self.count),
                                ('page_nums', self.page_nums),
                                ('next', self.get_next_url()),
                                ('previous', self.get_previous_url()),
                                ('results', self.data)])
Exemplo n.º 11
0
def paginate(select_query, next_url, page, page_amount=50, **url_params):
    page_query = PaginatedQuery(select_query, paginate_by=paginate_amount(page_amount))
    next_page = False
    if not page:
        page = 1
    if page < page_query.get_page_count():
        next_page = url_for(
            'resources.{}'.format(next_url),
            page_amount=page_amount,
            page=page + 1 if page > 0 else 2, **url_params
        )
    return page_query, next_page
Exemplo n.º 12
0
def get_owned():
    query = Content.get_user_files(get_user_from_request())
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated = PaginatedQuery(query, paginate_by=limit)

    return jsonify({
        "success": 1,
        "files": [p.to_json() for p in paginated.get_object_list()],
        "meta": {
            "page_count": paginated.get_page_count()
        },
    })
Exemplo n.º 13
0
def list_cvinfos():
    query = CVInfo.select().order_by(CVInfo.name)
    pg = PaginatedQuery(query,
                        paginate_by=10,
                        page_var='page',
                        check_bounds=True)
    page = pg.get_page()
    page_count = pg.get_page_count()
    cvinfos = pg.get_object_list()
    return render_template('cvinfo/list_cvinfos.html',
                           cvinfos=cvinfos,
                           page=page,
                           page_count=page_count)
Exemplo n.º 14
0
    def test_bounds_checking(self):
        paginated_query = PaginatedQuery(User, 3, 'p', False)
        with self.app.test_request_context('/?p=5'):
            results = paginated_query.get_object_list()
            self.assertEqual(list(results), [])

        paginated_query = PaginatedQuery(User, 3, 'p', True)
        with self.app.test_request_context('/?p=2'):
            self.assertEqual(len(list(paginated_query.get_object_list())), 3)
        with self.app.test_request_context('/?p=4'):
            self.assertEqual(len(list(paginated_query.get_object_list())), 1)
        with self.app.test_request_context('/?p=5'):
            self.assertRaises(Exception, paginated_query.get_object_list)
Exemplo n.º 15
0
def tags():
    """Получить список тегов, отсортированный по популярности"""
    query = Tag.get_tags()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    return jsonify(
        {
            "success": 1,
            "tags": [t.to_json() for t in paginated_query.get_object_list()],
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Exemplo n.º 16
0
def paginate():
    pagination = PaginatedQuery(Video.select().order_by(Video.id.desc()), 50)

    return 1, {
        'pre':
        50,
        'page':
        pagination.get_page(),
        'count':
        Video.select().count(),
        'data': [{
            **model_to_dict(video), 'code': None
        } for video in pagination.get_object_list()]
    }
Exemplo n.º 17
0
def get_new():
    """Получить список новых уведомлений"""
    query = Notification.get_user_unread_notifications(get_user_from_request())
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated = PaginatedQuery(query, paginate_by=limit)

    return jsonify({
        "success":
        1,
        "notifications": [p.to_json() for p in paginated.get_object_list()],
        "meta": {
            "page_count": paginated.get_page_count()
        },
    })
Exemplo n.º 18
0
def tags():
  if 'q' in request.args:
    q = '%%%s%%' % request.args['q']
    return 1, [model_to_dict(tag) for tag in Tag.select().where(Tag.name ** q).limit(10)]

  if 'list' in request.args:
    pagination = PaginatedQuery(Tag.select().order_by(Tag.id.desc()), 50)
    return 1, {
      'pre': 50,
      'page': pagination.get_page(),
      'count': Tag.select().count(),
      'data': [model_to_dict(video) for video in pagination.get_object_list()]
    }

  return render_template('tags.html')
Exemplo n.º 19
0
def get_blogs():
    """Получить список публичных блогов"""
    query = Blog.get_public_blogs()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    blogs = [b.to_json() for b in paginated_query.get_object_list()]
    blogs = [Vote.add_votes_info(b, 2, get_user_from_request()) for b in blogs]

    return jsonify(
        {
            "success": 1,
            "blogs": blogs,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Exemplo n.º 20
0
def by_player(player_name):
    player = Player.select().where(Player.ckey == player_name).first()
    if not player:
        abort(404)

    pages = PaginatedQuery(player.rounds, 10)
    return render_template("rounds/rounds.html", pages=pages, for_player=player_name)
Exemplo n.º 21
0
def get_posts():
    """Получить список публичных постов"""
    query = Post.get_public_posts()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify({
        "success": 1,
        "posts": posts,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 22
0
def users():
    """Получить список пользователей"""
    query = User.get_users_sorted_by_active_date()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    users = [u.to_json() for u in paginated_query.get_object_list()]
    users = [Vote.add_votes_info(u, 1, get_user_from_request()) for u in users]

    return jsonify({
        "success": 1,
        "users": users,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 23
0
def document_list():
    """
    Returns a paginated list of documents.

    Documents can be indexed by `POST`ing content, index(es) and,
    optionally, metadata.
    """
    if request.method == "POST":
        data = parse_post(["content"], ["identifier", "index", "indexes", "metadata"])

        indexes = validate_indexes(data)
        if indexes is None:
            error('You must specify either an "index" or "indexes".')

        document = Document.create(content=data["content"], identifier=data.get("identifier"))

        if data.get("metadata"):
            document.metadata = data["metadata"]

        for index in indexes:
            index.add_to_index(document)

        return jsonify(
            {
                "id": document.get_id(),
                "content": document.content,
                "indexes": [index.name for index in indexes],
                "metadata": document.metadata,
            }
        )

    query = Document.all()

    # Allow filtering by index.
    if request.args.get("index"):
        query = query.join(IndexDocument, JOIN_LEFT_OUTER).join(Index).where(Index.name == request.args["index"])

    if request.args.get("identifier"):
        query = query.where(Document.identifier == request.args["identifier"])

    pq = PaginatedQuery(
        query, paginate_by=app.config["PAGINATE_BY"], page_var=app.config["PAGE_VAR"], check_bounds=False
    )

    return jsonify(
        {"documents": _serialize_documents(pq.get_object_list()), "page": pq.get_page(), "pages": pq.get_page_count()}
    )
Exemplo n.º 24
0
def document_list():
    """
    Returns a paginated list of documents.

    Documents can be indexed by `POST`ing content, index(es) and,
    optionally, metadata.
    """
    if request.method == 'POST':
        data = parse_post(['content'], ['index', 'indexes', 'metadata'])

        indexes = validate_indexes(data)
        if indexes is None:
            error('You must specify either an "index" or "indexes".')

        document = Document.create(content=data['content'])

        if data.get('metadata'):
            document.metadata = data['metadata']

        for index in indexes:
            index.add_to_index(document)

        return jsonify({
            'id': document.rowid,
            'content': document.content,
            'indexes': [index.name for index in indexes],
            'metadata': document.metadata})

    query = Document.all()

    # Allow filtering by index.
    if request.args.get('index'):
        query = (query
                 .join(IndexDocument, JOIN_LEFT_OUTER)
                 .join(Index)
                 .where(Index.name == request.args['index']))

    pq = PaginatedQuery(
        query,
        paginate_by=app.config['PAGINATE_BY'],
        page_var=app.config['PAGE_VAR'],
        check_bounds=False)

    return jsonify({
        'documents': _serialize_documents(pq.get_object_list()),
        'page': pq.get_page(),
        'pages': pq.get_page_count()})
Exemplo n.º 25
0
def all_teas():
    types = TeaType.select().where(TeaType.is_origin == False).order_by(
        TeaType.order)  # noqa
    teas = PaginatedQuery((Tea.select(Tea, TeaVendor).join(TeaVendor)),
                          paginate_by=app.config['ITEMS_PER_PAGE'],
                          page_var='page',
                          check_bounds=True)

    return render_template('tea_types.html',
                           teas=teas,
                           types=types,
                           tea_type=None,
                           all=True,
                           pagination={
                               'page': teas.get_page(),
                               'pages': teas.get_page_count()
                           })
Exemplo n.º 26
0
    def paginated_query(self, query, paginate_by=None):
        if paginate_by is None:
            paginate_by = app.config['PAGINATE_BY']

        return PaginatedQuery(query,
                              paginate_by=paginate_by,
                              page_var=app.config['PAGE_VAR'],
                              check_bounds=False)
Exemplo n.º 27
0
def index(site=None):
    query = Question.select(Question, User, Site, Title, SQL(utils.rating_sql)).join(Site).switch(Question).join(
        User).switch(
        Question).join(
        Title)
    if site:
        query = query.where(Site.url == site)
        try:
            site_element = Site.select().where(Site.url == site).get()
        except DoesNotExist:
            abort(404)
            return
    else:
        site_element = utils.get_fallback_site()
    query = query.order_by(SQL("ci_lower_bound DESC, random"))
    # return jsonify(model_to_dict(query.get()))
    paginated_query = PaginatedQuery(query, paginate_by=10, check_bounds=True)
    pagearray = utils.create_pagination(paginated_query.get_page_count(), paginated_query.get_page())
    return render_template(
        "list.html",
        pagearray=pagearray,
        num_pages=paginated_query.get_page_count(),
        page=paginated_query.get_page(),
        questions=paginated_query.get_object_list(),
        site=site_element,
        voted=session["voted"] if "voted" in session and not config.make_cacheable else None,
        infohidden="hide" in request.cookies
    )
Exemplo n.º 28
0
 def get(self, pk):
     if pk:
         obj = self.get_object(pk)
         serializer = self.schema.dump(obj)
         return serializer.data
     parm = self.get_filter_get_method_parm()
     queryset = self.get_queryset(**parm)
     page_queryset = PaginatedQuery(queryset, self.page_size)
     serializers = NormalPagination(page_queryset, self.schema).result
     return serializers
Exemplo n.º 29
0
def pollmain():
    polls = (Poll_question.select(
        Poll_question.id,
        Poll_question.question,
        Poll_question.adminonly,
        Poll_question.dontshow,
    ).where(Poll_question.adminonly == False, Poll_question.dontshow == False))

    pages = PaginatedQuery(polls, 25, "page")

    return render_template("polls/polls.html", pages=pages)
Exemplo n.º 30
0
def by_type(tea_type_slug):
    tea_type = get_object_or_404(TeaType, TeaType.slug == tea_type_slug)
    types = TeaType.select().where(
        TeaType.is_origin == tea_type.is_origin).order_by(TeaType.order)
    teas = PaginatedQuery(
        (Tea.select(Tea, TeaVendor).join(TypeOfATea).join(TeaType).where(
            TypeOfATea.tea_type == tea_type).switch(Tea).join(TeaVendor)),
        paginate_by=app.config['ITEMS_PER_PAGE'],
        page_var='page',
        check_bounds=True)

    return render_template('tea_types.html',
                           teas=teas,
                           types=types,
                           tea_type=tea_type,
                           all=False,
                           pagination={
                               'page': teas.get_page(),
                               'pages': teas.get_page_count()
                           })
Exemplo n.º 31
0
def tag(title):
    """Получить посты с указанным тегом"""
    tag = Tag.get_or_none(Tag.title == title)
    if tag is None:
        return errors.not_found()

    query = Post.get_public_posts_with_tag(tag)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify(
        {
            "success": 1,
            "posts": posts,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Exemplo n.º 32
0
def user_jam_entries(username):
    """Получить список заявок на джем для данного пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = JamEntry.get_user_entries(user)
    limit = max(1, min(int(request.args.get("limit") or 100), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    entries = [e.to_json() for e in paginated_query.get_object_list()]

    return jsonify({
        "success": 1,
        "entries": entries,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 33
0
def get():
    search_type = request.args.get("type")
    search_query = request.args.get("q")
    limit = max(1, min(int(request.args.get("limit") or 20), 100))

    type = 0

    if not search_query or len(search_query) == 0:
        return errors.wrong_payload("q")

    if search_type == "post":
        query = Post.get_public_posts().where(
            Post.title.contains(search_query)
            | Post.text.contains(search_query))
        type = 3
    elif search_type == "blog":
        query = Blog.get_public_blogs().where(
            Blog.title.contains(search_query)
            | Blog.description.contains(search_query))
        type = 2
    elif search_type == "user":
        query = User.select().where(
            User.username.contains(search_query)
            | User.name.contains(search_query))
        type = 1
    else:
        return errors.wrong_payload("type")

    paginated_query = PaginatedQuery(query, paginate_by=limit)
    items = [item.to_json() for item in paginated_query.get_object_list()]
    items = [
        Vote.add_votes_info(i, type, get_user_from_request()) for i in items
    ]

    return jsonify({
        "success": 1,
        "result": items,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 34
0
def user_posts(username):
    """Получить список постов пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = Post.get_user_posts(user)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify({
        "success": 1,
        "posts": posts,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 35
0
def user_blogs(username):
    """Получить список блогов пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = Blog.get_blogs_for_user(user)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    blogs = [b.to_json() for b in paginated_query.get_object_list()]
    blogs = [Vote.add_votes_info(b, 2, get_user_from_request()) for b in blogs]

    return jsonify({
        "success": 1,
        "blogs": blogs,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Exemplo n.º 36
0
def index_detail(index_name):
    """
    Detail view for an index. This view returns a JSON object with
    the index's id, name, and a paginated list of associated documents.

    Existing indexes can be renamed using this view by `POST`-ing a
    `name`, or deleted by issuing a `DELETE` request.
    """
    index = get_object_or_404(Index, Index.name == index_name)
    if request.method == 'POST':
        data = parse_post(['name'])
        index.name = data['name']
        with database.atomic():
            try:
                index.save()
            except IntegrityError:
                error('"%s" is already in use.' % index.name)
    elif request.method == 'DELETE':
        with database.atomic():
            (IndexDocument
             .delete()
             .where(IndexDocument.index == index)
             .execute())
            index.delete_instance()

        return jsonify({'success': True})

    pq = PaginatedQuery(
        index.documents,
        paginate_by=app.config['PAGINATE_BY'],
        page_var=app.config['PAGE_VAR'],
        check_bounds=False)

    return jsonify({
        'id': index.id,
        'name': index.name,
        'documents': _serialize_documents(pq.get_object_list()),
        'page': pq.get_page(),
        'pages': pq.get_page_count()})
Exemplo n.º 37
0
    def test_paginated_query(self):
        query = User.select().order_by(User.username)
        paginated_query = PaginatedQuery(query, 4)

        with self.app.test_request_context('/?page=2'):
            self.assertEqual(paginated_query.get_page(), 2)
            self.assertEqual(paginated_query.get_page_count(), 3)
            users = paginated_query.get_object_list()

        self.assertEqual(
            [user.username for user in users],
            ['u04', 'u05', 'u06', 'u07'])

        with self.app.test_request_context('/'):
            self.assertEqual(paginated_query.get_page(), 1)

        for value in ['1', '0', '-1', 'xxx']:
            with self.app.test_request_context('/?page=%s' % value):
                self.assertEqual(paginated_query.get_page(), 1)
Exemplo n.º 38
0
def attachment_search():
    """
    Search the index for attachments matching the given query.
    """
    phrase = request.args.get('q', '') or None
    ranking, _ = validate_ranking()
    ordering = request.args.getlist('ordering')
    filters = extract_metadata_filters()

    # Allow filtering by index.
    idx_list = request.args.getlist('index')
    if idx_list:
        indexes = Index.select(Index.id).where(Index.name << idx_list)
    else:
        indexes = None

    query = Attachment.search(
        phrase or '*',
        indexes,
        ranking if phrase else None,
        ordering,
        force_star_all=True if not phrase else False,
        **filters)
    pq = PaginatedQuery(
        query.naive(),
        paginate_by=app.config['PAGINATE_BY'],
        page_var=app.config['PAGE_VAR'],
        check_bounds=False)

    response = []
    for attachment in pq.get_object_list():
        data = {
            'document_id': attachment.document_id,
            'filename': attachment.filename,
            'hash': attachment.hash,
            'id': attachment.id,
            'identifier': attachment.identifier,
            'mimetype': attachment.mimetype,
            'timestamp': attachment.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
        }
        if phrase:
            data['score'] = attachment.score

        url_params = {
            'document_id': data['document_id'],
            'pk': data['filename']}
        if app.config['AUTHENTICATION']:
            url_params['key'] = app.config['AUTHENTICATION']
        data['data'] = url_for('attachment_download', **url_params)
        response.append(data)

    return jsonify({
        'attachment_count': Attachment.select().count(),
        'attachments': response,
        'filters': filters,
        'ordering': ordering,
        'page': pq.get_page(),
        'pages': pq.get_page_count(),
        'ranking': ranking,
        'search_term': phrase,
    })