예제 #1
0
async def view_messages_list(request):
    """Messages list view with filters"""

    try:
        find_filters = await get_filters(request)
    except ValueError as error:
        raise HTTPBadRequest(body=error.args[0])

    (
        pagination_page,
        pagination_per_page,
        pagination_skip,
    ) = Pagination.get_pagination_params(request)
    if pagination_per_page is None:
        pagination_per_page = 0
    if pagination_skip is None:
        pagination_skip = 0

    messages = [
        msg async for msg in Message.collection.find(
            find_filters,
            limit=pagination_per_page,
            skip=pagination_skip,
            sort=[("time", int(request.query.get("sort_order", "-1")))],
        )
    ]

    context = {"messages": messages}

    if pagination_per_page is not None:
        if len(find_filters.keys()):
            total_msgs = await Message.collection.count_documents(find_filters)
        else:
            total_msgs = await Message.collection.estimated_document_count()

        query_string = request.query_string
        pagination = Pagination(
            pagination_page,
            pagination_per_page,
            total_msgs,
            url_base="/messages/posts/page/",
            query_string=query_string,
        )

        context.update({
            "pagination": pagination,
            "pagination_page": pagination_page,
            "pagination_total": total_msgs,
            "pagination_per_page": pagination_per_page,
            "pagination_item": "messages",
        })

    return cond_output(request, context, "TODO.html")
예제 #2
0
async def view_messages_list(request):
    """ Messages list view with filters
    """

    find_filters = await get_filters(request)

    pagination_page, pagination_per_page, pagination_skip = \
        Pagination.get_pagination_params(request)
    if pagination_per_page is None:
        pagination_per_page = 0
    if pagination_skip is None:
        pagination_skip = 0

    messages = [
        msg async for msg in Message.collection.find(
            find_filters,
            limit=pagination_per_page,
            skip=pagination_skip,
            sort=[('time', int(request.query.get('sort_order', '-1')))])
    ]

    context = {'messages': messages}

    if pagination_per_page is not None:
        if len(find_filters.keys()):
            total_msgs = await Message.collection.count_documents(find_filters)
        else:
            total_msgs = await Message.collection.estimated_document_count()

        query_string = request.query_string
        pagination = Pagination(pagination_page,
                                pagination_per_page,
                                total_msgs,
                                url_base='/messages/posts/page/',
                                query_string=query_string)

        context.update({
            'pagination': pagination,
            'pagination_page': pagination_page,
            'pagination_total': total_msgs,
            'pagination_per_page': pagination_per_page,
            'pagination_item': 'messages'
        })

    return cond_output(request, context, 'TODO.html')
예제 #3
0
async def view_posts_list(request):
    """ Posts list view with filters
    TODO: return state with amended posts
    """

    find_filters = {}
    filters = [
        # {'type': request.query.get('msgType', 'POST')}
    ]

    query_string = request.query_string
    addresses = request.query.get('addresses', None)
    if addresses is not None:
        addresses = addresses.split(',')

    refs = request.query.get('refs', None)
    if refs is not None:
        refs = refs.split(',')

    post_types = request.query.get('types', None)
    if post_types is not None:
        post_types = post_types.split(',')

    tags = request.query.get('tags', None)
    if tags is not None:
        tags = tags.split(',')

    hashes = request.query.get('hashes', None)
    if hashes is not None:
        hashes = hashes.split(',')

    date_filters = prepare_date_filters(request, 'time')
    block_height_filters = prepare_block_height_filters(request, 'blockHeight')

    if addresses is not None:
        filters.append({'content.address': {'$in': addresses}})

    if post_types is not None:
        filters.append({'content.type': {'$in': post_types}})

    if refs is not None:
        filters.append({'content.ref': {'$in': refs}})

    if tags is not None:
        filters.append({'content.content.tags': {'$elemMatch': {'$in': tags}}})

    if hashes is not None:
        filters.append({
            '$or': [{
                'item_hash': {
                    '$in': hashes
                }
            }, {
                'tx_hash': {
                    '$in': hashes
                }
            }]
        })

    if date_filters is not None:
        filters.append(date_filters)

    if block_height_filters is not None:
        filters.append(block_height_filters)

    if len(filters) > 0:
        find_filters = {'$and': filters} if len(filters) > 1 else filters[0]

    pagination_page, pagination_per_page, pagination_skip = \
        Pagination.get_pagination_params(request)
    if pagination_per_page is None:
        pagination_per_page = 0
    if pagination_skip is None:
        pagination_skip = 0

    posts = [
        msg async for msg in await get_merged_posts(
            find_filters, limit=pagination_per_page, skip=pagination_skip)
    ]

    context = {'posts': posts}

    if pagination_per_page is not None:
        total_msgs = await Message.collection.count(find_filters)

        pagination = Pagination(pagination_page,
                                pagination_per_page,
                                total_msgs,
                                url_base='/messages/posts/page/',
                                query_string=query_string)

        context.update({
            'pagination': pagination,
            'pagination_page': pagination_page,
            'pagination_total': total_msgs,
            'pagination_per_page': pagination_per_page,
            'pagination_item': 'posts'
        })

    return cond_output(request, context, 'TODO.html')
예제 #4
0
async def view_messages_list(request):
    """ Messages list view with filters
    """

    find_filters = {}

    query_string = request.query_string
    msg_type = request.query.get('msgType', None)

    filters = []
    addresses = request.query.get('addresses', None)
    if addresses is not None:
        addresses = addresses.split(',')

    refs = request.query.get('refs', None)
    if refs is not None:
        refs = refs.split(',')

    content_types = request.query.get('contentTypes', None)
    if content_types is not None:
        content_types = content_types.split(',')

    channels = request.query.get('channels', None)
    if channels is not None:
        channels = channels.split(',')

    tags = request.query.get('tags', None)
    if tags is not None:
        tags = tags.split(',')

    hashes = request.query.get('hashes', None)
    if hashes is not None:
        hashes = hashes.split(',')

    date_filters = prepare_date_filters(request, 'time')

    if msg_type is not None:
        filters.append({'type': msg_type})

    if addresses is not None:
        filters.append({
            '$or': [
                {
                    'content.address': {
                        '$in': addresses
                    }
                },
                {
                    'sender': {
                        '$in': addresses
                    }
                },
            ]
        })

    if content_types is not None:
        filters.append({'content.type': {'$in': content_types}})

    if refs is not None:
        filters.append({'content.ref': {'$in': refs}})

    if tags is not None:
        filters.append({'content.tags': {'$elemMatch': {'$in': tags}}})

    if channels is not None:
        filters.append({'channel': {'$in': channels}})

    if hashes is not None:
        filters.append({
            '$or': [{
                'item_hash': {
                    '$in': hashes
                }
            }, {
                'tx_hash': {
                    '$in': hashes
                }
            }]
        })

    if date_filters is not None:
        filters.append(date_filters)

    if len(filters) > 0:
        find_filters = {'$and': filters} if len(filters) > 1 else filters[0]

    pagination_page, pagination_per_page, pagination_skip = \
        Pagination.get_pagination_params(request)
    if pagination_per_page is None:
        pagination_per_page = 0
    if pagination_skip is None:
        pagination_skip = 0

    messages = [
        msg async for msg in Message.collection.find(
            find_filters,
            limit=pagination_per_page,
            skip=pagination_skip,
            sort=[('time', int(request.query.get('sort_order', '-1')))])
    ]

    context = {'messages': messages}

    if pagination_per_page is not None:
        if len(find_filters.keys()):
            total_msgs = await Message.collection.count_documents(find_filters)
        else:
            total_msgs = await Message.collection.estimated_document_count()

        pagination = Pagination(pagination_page,
                                pagination_per_page,
                                total_msgs,
                                url_base='/messages/posts/page/',
                                query_string=query_string)

        context.update({
            'pagination': pagination,
            'pagination_page': pagination_page,
            'pagination_total': total_msgs,
            'pagination_per_page': pagination_per_page,
            'pagination_item': 'posts'
        })

    return cond_output(request, context, 'TODO.html')