Esempio n. 1
0
def test_get_articles_by_id(in_memory_repo):
    target_article_ids = [5, 6, 7, 8]
    articles_as_dict = news_services.get_articles_by_id(target_article_ids, in_memory_repo)

    # Check that 2 articles were returned from the query.
    assert len(articles_as_dict) == 2

    # Check that the article ids returned were 5 and 6.
    article_ids = [article['id'] for article in articles_as_dict]
    assert set([5, 6]).issubset(article_ids)
Esempio n. 2
0
def articles_by_tag():
    articles_per_page = 3

    # Read query parameters.
    tag_name = request.args.get('tag')
    cursor = request.args.get('cursor')
    article_to_show_comments = request.args.get('view_comments_for')

    if article_to_show_comments is None:
        # No view-comments query parameter, so set to a non-existent article id.
        article_to_show_comments = -1
    else:
        # Convert article_to_show_comments from string to int.
        article_to_show_comments = int(article_to_show_comments)

    if cursor is None:
        # No cursor query parameter, so initialise cursor to start at the beginning.
        cursor = 0
    else:
        # Convert cursor from string to int.
        cursor = int(cursor)

    # Retrieve article ids for articles that are tagged with tag_name.
    article_ids = services.get_article_ids_for_tag(tag_name, uow.uow_instance)

    # Retrieve the batch of articles to display on the Web page.
    articles = services.get_articles_by_id(
        article_ids[cursor:cursor + articles_per_page], uow.uow_instance)

    first_article_url = None
    last_article_url = None
    next_article_url = None
    prev_article_url = None

    if cursor > 0:
        # There are preceding articles, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=cursor - articles_per_page)
        first_article_url = url_for('news_bp.articles_by_tag', tag=tag_name)

    if cursor + articles_per_page < len(article_ids):
        # There are further articles, so generate URLs for the 'next' and 'last' navigation buttons.
        next_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=cursor + articles_per_page)

        last_cursor = articles_per_page * int(
            len(article_ids) / articles_per_page)
        if len(article_ids) % articles_per_page == 0:
            last_cursor -= articles_per_page
        last_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=last_cursor)

    # Construct urls for viewing article comments and adding comments.
    for article in articles:
        article['view_comment_url'] = url_for('news_bp.articles_by_tag',
                                              tag=tag_name,
                                              cursor=cursor,
                                              view_comments_for=article['id'])
        article['add_comment_url'] = url_for('news_bp.comment_on_article',
                                             article=article['id'])

    # Generate the webpage to display the articles.
    return render_template('news/articles.html',
                           title='Articles',
                           articles_title='Articles tagged by ' + tag_name,
                           articles=articles,
                           selected_articles=utilities.get_selected_articles(
                               len(articles) * 2),
                           tag_urls=utilities.get_tags_and_urls(),
                           first_article_url=first_article_url,
                           last_article_url=last_article_url,
                           prev_article_url=prev_article_url,
                           next_article_url=next_article_url,
                           show_comments_for_article=article_to_show_comments)