Beispiel #1
0
    def to_df(query_set: QuerySet) -> pd.DataFrame:
        if query_set.count() == 0:
            return pd.DataFrame()

        return pd.DataFrame(
            ((d, *vs) for d, vs in query_set.values_list('datetime', 'datas')),
            columns=['datetime', 'macd', 'macdsignal',
                     'macdhist']).set_index('datetime')
Beispiel #2
0
    def to_df(query_set: QuerySet) -> pd.DataFrame:
        if query_set.count() == 0:
            return pd.DataFrame()

        return pd.DataFrame(
            ((d, vs) for d, vs in query_set.values_list('datetime', 'datas')),
            columns=['datetime',
                     f'ma{query_set[0].param}']).set_index('datetime')
Beispiel #3
0
def get_paginated_items_from_qs(qs: QuerySet,
                                mapping_fn=default_mapping_fn,
                                *args,
                                **kwargs):
    page = request.args.get('page', default=0)
    limit = request.args.get('limit', default=MAX_PAGINATED_LIMIT)

    try:
        page = int(page)
    except ValueError:
        raise PaginationPageInvalid()

    try:
        limit = int(limit)
    except ValueError:
        raise PaginationLimitInvalid()

    limit = min(limit, MAX_PAGINATED_LIMIT)
    skip = page * limit

    qs = qs.skip(skip).limit(limit)

    no_total_items = qs.count()
    no_items = qs.count(with_limit_and_skip=True)
    no_items_before = max(skip, 0)
    no_items_after = max(no_total_items - skip - no_items, 0)

    return {
        'limit': limit,
        'skip': skip,
        'no_items': no_items,
        'no_total_items': no_total_items,
        'no_items_before': no_items_before,
        'no_items_after': no_items_after,
        'page': page,
        'no_pages': get_page_count(no_total_items, limit),
        'no_pages_before': get_page_count(no_items_before, limit),
        'no_pages_after': get_page_count(no_items_after, limit),
        'items': [mapping_fn(item, *args, **kwargs) for item in qs],
    }
Beispiel #4
0
def paginate(
    query_set: QuerySet,
    page_size: int,
    page_index: int,
    response: Response,
) -> QuerySet:
    """
    Paginates a query set, sets the ``X-Pages`` header in the response. Raises
    a 422 if the page index is invalid.
    """
    max_page = ceil(query_set.count() / page_size)
    if not 1 <= page_index <= max_page:
        raise HTTPException(
            detail="Invalid page index",
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        )
    response.headers["X-Pages"] = str(max_page)
    return query_set[(page_index - 1) * page_size:page_index * page_size]